Skip to content

Commit 138745c

Browse files
committed
2020-02-11
1 parent 03f1a5d commit 138745c

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution(object):
2+
def numberOfSteps (self, num):
3+
"""
4+
:type num: int
5+
:rtype: int
6+
"""
7+
cnt = 0
8+
while num:
9+
if num % 2:
10+
num -= 1
11+
else:
12+
num >>= 1
13+
cnt += 1
14+
return cnt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def numOfSubarrays(self, arr, k, threshold):
3+
"""
4+
:type arr: List[int]
5+
:type k: int
6+
:type threshold: int
7+
:rtype: int
8+
"""
9+
res = 0
10+
target_sum = k * threshold
11+
subs = 0
12+
for i in range(0, k):
13+
subs += arr[i]
14+
15+
for i in range(0, len(arr) - k + 1):
16+
if i:
17+
subs = subs - arr[i - 1] + arr[i + k - 1]
18+
if subs >= target_sum:
19+
res += 1
20+
21+
return res
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution(object):
2+
def minJumps(self, arr):
3+
"""
4+
:type arr: List[int]
5+
:rtype: int
6+
"""
7+
from collections import defaultdict, deque
8+
dic = defaultdict(list)
9+
for i, x in enumerate(arr):
10+
if (i and arr[i] != arr[i - 1]) or (i < len(arr) - 1 and arr[i] != arr[i + 1]):
11+
dic[x].append(i)
12+
13+
queue = deque([(0, 0)]) #pos, step
14+
visited = set([0])
15+
16+
while queue:
17+
pos, step = queue.popleft()
18+
19+
if pos == len(arr) - 1:
20+
return step
21+
22+
for p in [pos - 1, pos + 1] + dic[arr[pos]]:
23+
if 0 <= p < len(arr) and p not in visited:
24+
queue.append((p, step + 1))
25+
visited.add(p)

0 commit comments

Comments
 (0)