-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path037_jump_game.sio
More file actions
70 lines (62 loc) · 1.61 KB
/
037_jump_game.sio
File metadata and controls
70 lines (62 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//@ run-pass
// HumanEval 037: Jump Game
//
// Given an array where each element represents the maximum jump length
// from that position, determine if you can reach the last index.
// Greedy approach: track the farthest reachable index.
// Returns 1 if reachable, 0 otherwise.
fn can_jump(nums: [i64; 256], n: i64) -> i64 with Mut, Panic, Div {
var farthest: i64 = 0
var i: i64 = 0
while i < n {
if i > farthest {
return 0
}
let reach = i + nums[i]
if reach > farthest {
farthest = reach
}
i = i + 1
}
1
}
fn main() -> i64 with IO, Mut, Panic, Div {
// Test 1: [2, 3, 1, 1, 4] -> can reach end
var a1: [i64; 256] = [0; 256]
a1[0] = 2
a1[1] = 3
a1[2] = 1
a1[3] = 1
a1[4] = 4
assert(can_jump(a1, 5) == 1)
// Test 2: [3, 2, 1, 0, 4] -> stuck at index 3
var a2: [i64; 256] = [0; 256]
a2[0] = 3
a2[1] = 2
a2[2] = 1
a2[3] = 0
a2[4] = 4
assert(can_jump(a2, 5) == 0)
// Test 3: single element [0] -> already at end
var a3: [i64; 256] = [0; 256]
a3[0] = 0
assert(can_jump(a3, 1) == 1)
// Test 4: [1, 1, 1, 1] -> step by step
var a4: [i64; 256] = [0; 256]
a4[0] = 1
a4[1] = 1
a4[2] = 1
a4[3] = 1
assert(can_jump(a4, 4) == 1)
// Test 5: [0, 1] -> stuck at index 0
var a5: [i64; 256] = [0; 256]
a5[0] = 0
a5[1] = 1
assert(can_jump(a5, 2) == 0)
// Test 6: [5, 0, 0, 0, 0, 0] -> big first jump
var a6: [i64; 256] = [0; 256]
a6[0] = 5
assert(can_jump(a6, 6) == 1)
println("037_jump_game: ALL TESTS PASSED")
0
}