Skip to content

Commit 7efeaa8

Browse files
Merge branch 'Achal-Hingrajiya:main' into main
2 parents 0bee318 + 2fc84d1 commit 7efeaa8

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution:
2+
def minJumps(self, arr: List[int]) -> int:
3+
if len(arr) == 1:
4+
return 0
5+
6+
adj = defaultdict(list)
7+
for i, n in enumerate(arr):
8+
adj[n].append(i)
9+
10+
ans = [0] * len(arr)
11+
dq = deque([len(arr) - 1])
12+
13+
while dq:
14+
i = dq.popleft()
15+
16+
if (i < len(arr) - 1) and (ans[i + 1] == 0) and (i + 1 < len(arr) - 1):
17+
ans[i + 1] = ans[i] + 1
18+
dq.append(i + 1)
19+
20+
if (i > 0) and (ans[i - 1] == 0):
21+
ans[i - 1] = ans[i] + 1
22+
if i - 1 == 0:
23+
return ans[0]
24+
dq.append(i - 1)
25+
26+
for j in adj[arr[i]]:
27+
if (ans[j] == 0) and (j < len(arr) - 1):
28+
ans[j] = ans[i] + 1
29+
if j == 0:
30+
return ans[0]
31+
dq.append(j)
32+
adj.pop(arr[i])
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def wordPattern(self, pattern: str, s: str) -> bool:
3+
arr = s.split()
4+
if len(arr) != len(pattern):
5+
return False
6+
7+
d = dict()
8+
9+
for i in range(len(pattern)):
10+
item = d.get(pattern[i])
11+
if not item:
12+
if arr[i] in d.values():
13+
return False
14+
d[pattern[i]] = arr[i]
15+
else:
16+
if item != arr[i]:
17+
return False
18+
19+
return True
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution:
2+
def maxDistToClosest(self, arr: List[int]) -> int:
3+
4+
n = len(arr)
5+
6+
def closest_left(index):
7+
for i in range(index-1, -1, -1):
8+
if arr[i] == 1:
9+
return i
10+
return -1
11+
12+
def closest_right(index):
13+
for i in range(index + 1, n):
14+
if arr[i] == 1:
15+
return i
16+
return -1
17+
18+
pos = -float("inf")
19+
for i in range(n):
20+
if not arr[i]:
21+
left = closest_left(i)
22+
right = closest_right(i)
23+
d_right = float("inf")
24+
d_left = float("inf")
25+
if left != -1:
26+
d_left = i - left
27+
if right != -1:
28+
d_right = right - i
29+
30+
distance = min(d_left, d_right)
31+
pos = max(pos, distance)
32+
33+
return pos
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# 8. String to Integer (atoi)
2+
#### Problem link: https://leetcode.com/problems/string-to-integer-atoi/

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ This repo contains solutions of programming problems of January Challenge
77
| 11th Jan, 2022 | [1022. Sum of Root To Leaf Binary Numbers](https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/) | [Here](https://github.com/Achal-Hingrajiya/Leetcode_January_Challenge/tree/main/1022_Sum%20of%20Root%20To%20Leaf%20Binary%20Numbers_(11th%20Jan%202022)) |
88
| 12th Jan, 2022 | [701. Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Here](701_Insert%20into%20a%20Binary%20Search%20Tree_(12th%20Jan%202022)) |
99
| 13th Jan, 2022 | [452. Minimum Number of Arrows to Burst Balloons](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/) | [Here](452_Minimum%20Number%20of%20Arrows%20to%20Burst%20Balloons_(13th%20Jan%2C%202022)) |
10+
| 14th Jan, 2022 | [8. String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) | [Here](8_String%20to%20Integer%20(atoi)_(14th%20Jan%202022)) |
11+
| 15th Jan, 2022 | [1345. Jump Game IV](https://leetcode.com/problems/jump-game-iv/) | [Here](1345_Jump%20Game%20IV_(15th%20Jan%202022)) |
12+
| 16th Jan, 2022 | [849. Maximize Distance to Closest Person](https://leetcode.com/problems/maximize-distance-to-closest-person/) | [Here](849_Maximize%20Distance%20to%20Closest%20Person_(16th%20Jan%202022)) |
13+
| 17th Jan, 2022 | [290. Word Pattern](https://leetcode.com/problems/word-pattern/) | [Here](290_Word%20Pattern_(17th%20Jan%202022)) |

0 commit comments

Comments
 (0)