Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@
#### Name: Debaditya Banerji
- GitHub: https://github.com/devAdityaa/
```

```markdown
#### Name: Aritra Dey
- GitHub: https://github.com/aritradey-CS/
```
30 changes: 17 additions & 13 deletions Python Projects/Frog_Jump.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
# Title: 403. Frog Jump
# Difficulty: Hard
# Problem: https://leetcode.com/problems/frog-jump/description/
from typing import List

# using stack (highest efficiency)
class Solution:
def canCross(self, stones: List[int]) -> bool:
stone_set = set(stones)
visited_set = set()
stack = [(0, 0)]
while stack:
stone, jump = stack.pop()
for j in [jump-1, jump, jump+1]:
s = stone + j
if j > 0 and s in stone_set and (s, j) not in visited_set:
if s == stones[-1]:
return True
stack.append((s, j))
visited_set.add((stone, jump))
return False
n = len(stones)
dp = {} # Use a dictionary to store valid jumps for each stone

for stone in stones:
dp[stone] = set()

dp[0].add(0) # The first stone can only be reached with a jump of size 0

for i in range(n):
for j in dp[stones[i]]:
for step in range(j - 1, j + 2): # Next jump size could be j-1, j, or j+1
if step > 0 and stones[i] + step in dp:
dp[stones[i] + step].add(step)

return len(dp[stones[-1]]) > 0


# using dp (low efficiency)
class Solution:
Expand Down