Skip to content

Commit ba3622d

Browse files
committed
55. Jump Game
1 parent 9e2277f commit ba3622d

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

jump-game.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//Runtime: 13 ms
2+
class Solution {
3+
public:
4+
bool canJump(vector<int>& nums) {
5+
int j = 0;
6+
for(int i=0;i<nums.size() && i<=j;i++){
7+
j = max(j, i+nums[i]);
8+
}
9+
return j >= (nums.size()-1);
10+
}
11+
};

jump-game.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//Runtime: 392 ms
2+
object Solution {
3+
def canJump(nums: Array[Int]): Boolean = {
4+
nums.zipWithIndex
5+
.foldLeft((0, 0))((a, b) =>
6+
if (a._1 < b._2 + b._1 && b._2 <= a._2) (b._2 + b._1, b._2 + b._1)
7+
else a)
8+
._1 >= (nums.size - 1)
9+
}
10+
}

0 commit comments

Comments
 (0)