|
| 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