Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9157096

Browse files
Add Jump Game solution and tests; implement various edge and complex cases
1 parent f04a169 commit 9157096

File tree

2 files changed

+136
-1
lines changed

2 files changed

+136
-1
lines changed

‎README.md‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
- Factorial Trailing Zeroes: https://leetcode.com/problems/factorial-trailing-zeroes/submissions/
3434
- https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
3535
- https://leetcode.com/problems/rotate-array/description/
36+
- https://leetcode.com/problems/jump-game/description/
37+
3638

3739

3840
### Topics
@@ -42,4 +44,9 @@
4244
- Palindrome Number
4345
- Factorial
4446
- Hash Map Map (Hash Map ---- Linked Hash Map ---- Splay Tree Map)
45-
- 2 Pointers (Two Pointers) -> left and right pointers
47+
- 2 Pointers (Two Pointers) -> left and right pointers
48+
49+
50+
### Algorithms
51+
- Greedy Algorithm
52+
-

‎leetcode/55.jump_game.dart‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// https://leetcode.com/problems/jump-game/description/
2+
3+
import 'package:test/test.dart';
4+
5+
void main() {
6+
final stopwatch = Stopwatch()..start();
7+
runTests();
8+
print(Solution().canJump([2, 2, 0, 1, 4]));
9+
10+
stopwatch.stop();
11+
print('Function Execution Time : ${stopwatch.elapsedMicroseconds} micro sec');
12+
}
13+
14+
class Solution {
15+
bool canJump(List<int> nums) {
16+
/// only one index
17+
if (nums.length == 1) return true;
18+
int maxReachableIndex = nums[0];
19+
for (int i = 1; i < nums.length; i++) {
20+
if (i > maxReachableIndex) return false;
21+
if (i + nums[i] > maxReachableIndex) {
22+
maxReachableIndex = i + nums[i];
23+
}
24+
}
25+
return true;
26+
}
27+
28+
// bool canJump(List<int> nums) {
29+
// int reachable = 0;
30+
31+
// /// this will iterate only the items that are reachable
32+
// for (int i = 0; i < nums.length && i <= reachable; i++) {
33+
// reachable = max(reachable, nums[i] + i);
34+
// if (reachable >= nums.length - 1) return true;
35+
// }
36+
// return false;
37+
// }
38+
}
39+
40+
void runTests() {
41+
final Solution s = Solution();
42+
43+
group('Jump Game', () {
44+
// Basic examples from problem statement
45+
test('Example 1: [2,3,1,1,4] → true', () {
46+
expect(s.canJump([2, 3, 1, 1, 4]), isTrue);
47+
});
48+
49+
test('Example : [1,2,3] → true', () {
50+
expect(s.canJump([1, 2, 3]), isTrue);
51+
});
52+
53+
test('Example 2: [3,2,1,0,4] → false', () {
54+
expect(s.canJump([3, 2, 1, 0, 4]), isFalse);
55+
});
56+
57+
// Edge cases
58+
test('Single element: [0] → true', () {
59+
expect(s.canJump([0]), isTrue);
60+
});
61+
62+
test('Single element: [5] → true', () {
63+
expect(s.canJump([5]), isTrue);
64+
});
65+
66+
test('Two elements reachable: [1,0] → true', () {
67+
expect(s.canJump([1, 0]), isTrue);
68+
});
69+
70+
test('Two elements unreachable: [0,1] → false', () {
71+
expect(s.canJump([0, 1]), isFalse);
72+
});
73+
74+
// All zeros
75+
test('All zeros reachable: [0,0,0,0] → false', () {
76+
expect(s.canJump([0, 0, 0, 0]), isFalse);
77+
});
78+
79+
test('All zeros except last: [0,0,0,1] → false', () {
80+
expect(s.canJump([0, 0, 0, 1]), isFalse);
81+
});
82+
83+
// Large jumps
84+
test('Large jump at start: [10,0,0,0,0] → true', () {
85+
expect(s.canJump([10, 0, 0, 0, 0]), isTrue);
86+
});
87+
88+
test('Large jump in middle: [2,5,0,0] → true', () {
89+
expect(s.canJump([2, 5, 0, 0]), isTrue);
90+
});
91+
92+
// Complex cases
93+
test('Complex reachable: [2,0,1,1,4] → true', () {
94+
expect(s.canJump([2, 0, 1, 1, 4]), isTrue);
95+
});
96+
97+
test('Complex unreachable: [1,1,0,2,4] → false', () {
98+
expect(s.canJump([1, 1, 0, 2, 4]), isFalse);
99+
});
100+
101+
// Maximum constraints
102+
test('Maximum length reachable', () {
103+
final nums = List.filled(10000, 1);
104+
expect(s.canJump(nums), isTrue);
105+
});
106+
107+
test('Maximum jump value', () {
108+
expect(s.canJump([100000, 0]), isTrue);
109+
});
110+
111+
test('Alternating unreachable: [1,0,2,0,1] → false', () {
112+
expect(s.canJump([1, 0, 2, 0, 1]), isFalse);
113+
});
114+
115+
// Random cases
116+
test('Random reachable case', () {
117+
expect(s.canJump([2, 2, 0, 1, 4]), isTrue);
118+
});
119+
120+
test('Random unreachable case', () {
121+
expect(s.canJump([1, 2, 0, 0, 1]), isFalse);
122+
});
123+
124+
test('Random unreachable case', () {
125+
expect(s.canJump([4, 2, 0, 0, 1, 1, 4, 4, 4, 0, 4, 0]), isTrue);
126+
});
127+
});
128+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /