-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path0975-OddEvenJump.cs
52 lines (46 loc) · 1.54 KB
/
0975-OddEvenJump.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//-----------------------------------------------------------------------------
// Runtime: 204ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0975_OddEvenJump
{
public int OddEvenJumps(int[] A)
{
var length = A.Length;
var oddPosibility = new bool[length];
var evenPosibility = new bool[length];
oddPosibility[length - 1] = true;
evenPosibility[length - 1] = true;
var map = new SortedList<int, int>
{
{ A[length - 1], length - 1 }
};
var result = 1;
for (int i = length - 2; i >= 0; i--)
{
var existed = map.TryGetValue(A[i], out int index);
map[A[i]] = i;
if (existed)
{
oddPosibility[i] = evenPosibility[index];
evenPosibility[i] = oddPosibility[index];
}
else
{
index = map.IndexOfKey(A[i]);
if (index != map.Count - 1)
oddPosibility[i] = evenPosibility[map.Values[index + 1]];
if (index != 0)
evenPosibility[i] = oddPosibility[map.Values[index - 1]];
}
if (oddPosibility[i])
result++;
}
return result;
}
}
}