generated from eyamenko/dotnet-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem11.cs
30 lines (27 loc) · 969 Bytes
/
Problem11.cs
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
namespace LeetCode;
/// <summary>
/// <see href="https://leetcode.com/problems/jump-game/">Jump Game</see>.
/// </summary>
public static class Problem11
{
/// <summary>
/// You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.
/// Return true if you can reach the last index, or false otherwise.
/// Time complexity: O(n).
/// Space complexity: O(1).
/// </summary>
/// <param name="nums">Array to traverse.</param>
/// <returns>True, if the last index can be reached.</returns>
public static bool CanJump(int[] nums)
{
for (int i = 0, maxIndex = 0, length = nums.Length - 1; maxIndex < length; i++)
{
maxIndex = Math.Max(maxIndex, nums[i] + i);
if (maxIndex == i)
{
return false;
}
}
return true;
}
}