Skip to content

Commit cf996fa

Browse files
day 17 q3 added in cpp and python #382
1 parent 6623491 commit cf996fa

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Cpp Code
2+
<br /> 1. Update the variable maxreach with the next highest integer. It is the highest index it can jump upto at that point.
3+
<br /> 2. If index is greater than the index it can reach upto, return false.
4+
5+
```
6+
class Solution {
7+
public:
8+
bool canJump(vector<int>& nums) {
9+
int n = nums.size();
10+
int maxReach = 0;
11+
for (int i = 0; i < n; i++) {
12+
if (i > maxReach) return false;
13+
maxReach = max(maxReach, i + nums[i]);
14+
}
15+
return true;
16+
}
17+
};
18+
```
19+
# Python Code
20+
```
21+
class Solution:
22+
def canJump(self, nums: List[int]) -> bool:
23+
jump = 0
24+
for n in nums:
25+
if jump < 0:
26+
return False
27+
elif n > jump:
28+
jump = n
29+
jump -= 1
30+
31+
return True
32+
```

0 commit comments

Comments
 (0)