Skip to content

Commit c1af8ba

Browse files
Jeehay28Jeehay28
authored andcommitted
Add jump-game solution in TS
1 parent e6b8cf7 commit c1af8ba

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

jump-game/Jeehay28.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
function canJump(nums: number[]): boolean {
4+
// nums = [3, 2, 1, 0, 4]
5+
// farthest 3, 3, 3, 3
6+
let farthest = 0;
7+
8+
for (let i = 0; i < nums.length; i++) {
9+
if (i > farthest) return false;
10+
farthest = Math.max(farthest, nums[i] + i);
11+
}
12+
return true;
13+
}
14+
15+
16+
// ❌ TC: O(n^2)
17+
// SC: O(n)
18+
19+
// function canJump(nums: number[]): boolean {
20+
// const lastIndex = nums.length - 1;
21+
// const visited = new Map<number, boolean>();
22+
23+
// const canReach = (current: number): boolean => {
24+
// if (current === lastIndex) return true;
25+
26+
// if (visited.has(current)) return visited.get(current) as boolean;
27+
28+
// for (let i = 1; i <= nums[current]; i++) {
29+
// if (current + i <= lastIndex) {
30+
// if (canReach(current + i)) return true;
31+
// }
32+
// }
33+
34+
// visited.set(current, false);
35+
36+
// return false;
37+
// };
38+
39+
// return canReach(0);
40+
// }

0 commit comments

Comments
 (0)