Skip to content

Commit 6d27285

Browse files
committed
Time: 527 ms (50%), Space: 18 MB (0%) - LeetHub
1 parent 624ce11 commit 6d27285

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# time complexity: O(n^2)
2+
# space complexity: O(1)
3+
class Solution:
4+
def totalWaviness(self, num1: int, num2: int) -> int:
5+
def helper(x: int) -> int:
6+
s = str(x)
7+
if len(s) < 3:
8+
return 0
9+
w = 0
10+
for i in range(1, len(s)-1):
11+
a, b, c = int(s[i-1]), int(s[i]), int(s[i+1])
12+
if b > a and b > c:
13+
w += 1
14+
elif b < a and b < c:
15+
w += 1
16+
return w
17+
total = 0
18+
for n in range(num1, num2 + 1):
19+
total += helper(n)
20+
return total
21+
22+
23+
num1 = 120
24+
num2 = 130
25+
print(Solution().totalWaviness(num1, num2))
26+
num1 = 198
27+
num2 = 202
28+
print(Solution().totalWaviness(num1, num2))
29+
num1 = 4848
30+
num2 = 4848
31+
print(Solution().totalWaviness(num1, num2))

0 commit comments

Comments
 (0)