-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClumsyFactorial_Day52.py
More file actions
56 lines (45 loc) · 1.26 KB
/
Copy pathClumsyFactorial_Day52.py
File metadata and controls
56 lines (45 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#Brute Approach
class Solution:
def clumsy(self, n: int) -> int:
ops = ['*', '/', '+', '-']
res = str(n)
idx = 0
for i in range(n - 1, 0, -1):
res += ops[idx] + str(i)
idx = (idx + 1) % 4
return eval(res) # brute-force evaluation
# TC - O(n)
# SC - O(n)
#Better Approach
class Solution:
def clumsy(self, n: int) -> int:
stack = [n]
n -= 1
idx = 0 # operation index
while n > 0:
if idx % 4 == 0: # multiplication
stack[-1] *= n
elif idx % 4 == 1: # division
stack[-1] = int(stack[-1] / n) # truncate toward zero
elif idx % 4 == 2: # addition
stack.append(n)
else: # subtraction
stack.append(-n)
idx += 1
n -= 1
return sum(stack)
# TC - O(n)
# SC - O(n)
#Optimal Approach
class Solution:
def clumsy(self, n: int) -> int:
if n == 1: return 1
if n == 2: return 2
if n == 3: return 6
if n == 4: return 7
if n % 4 == 0: return n + 1
if n % 4 == 1: return n + 2
if n % 4 == 2: return n + 2
return n - 1
# TC - O(1)
# SC - O(1)