Skip to content

Commit 3a2ecbe

Browse files
committed
Arrays
1 parent d6acdcf commit 3a2ecbe

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

Arrays/can-jump-II-45.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Problem source: https://leetcode.com/problems/jump-game-ii/?envType=study-plan-v2&envId=top-interview-150
2+
from typing import List
3+
4+
5+
class Solution:
6+
def jump(self, nums: List[int]) -> int:
7+
# variable storing the required answer
8+
n_jumps = 0
9+
10+
# 2 pointers
11+
start = 0
12+
end = 0
13+
14+
while end < len(nums) - 1:
15+
max_jump_possible = 0
16+
17+
for i in range(start, end + 1):
18+
max_jump_possible = max(max_jump_possible, i + nums[i])
19+
20+
# update the pointers
21+
start = end + 1
22+
end = max_jump_possible
23+
24+
# update the number of jumps
25+
n_jumps += 1
26+
27+
return n_jumps

Arrays/h-index-274.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Problem source: https://leetcode.com/problems/h-index/description/
2+
from typing import List
3+
4+
5+
class Solution:
6+
def hIndex(self, citations: List[int]) -> int:
7+
citations.sort(reverse=True)
8+
l = len(citations)
9+
h = 0
10+
11+
for i in range(l):
12+
if citations[i] >= i + 1:
13+
h = i + 1
14+
15+
return h

0 commit comments

Comments
 (0)