-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution975.cs
64 lines (49 loc) · 1.71 KB
/
Solution975.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
53
54
55
56
57
58
59
60
61
62
63
64
using System.Numerics;
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution975
{
/// <summary>
/// 975. Odd Even Jump - Hard
/// <a href="https://leetcode.com/problems/odd-even-jump">See the problem</a>
/// </summary>
public int OddEvenJumps(int[] arr)
{
int n = arr.Length;
if (n == 1) return 1;
var oddNext = new int[n];
var evenNext = new int[n];
BuildJumpIndices(arr, oddNext, true);
BuildJumpIndices(arr, evenNext, false);
var odd = new bool[n];
var even = new bool[n];
odd[n - 1] = even[n - 1] = true; // Last index is always reachable
int goodStartingIndices = 1; // Last index is always good
for (int i = n - 2; i >= 0; i--)
{
if (oddNext[i] != -1) odd[i] = even[oddNext[i]];
if (evenNext[i] != -1) even[i] = odd[evenNext[i]];
if (odd[i]) goodStartingIndices++;
}
return goodStartingIndices;
}
private void BuildJumpIndices(int[] arr, int[] nextJump, bool isOddJump)
{
var stack = new Stack<int>();
var map = new SortedDictionary<int, int>();
int n = arr.Length;
Array.Fill(nextJump, -1);
int sign = isOddJump ? 1 : -1;
var sortedIndices = new List<int>();
for (int i = 0; i < n; i++)
sortedIndices.Add(i);
sortedIndices.Sort((a, b) => sign * arr[a].CompareTo(arr[b]) != 0 ? sign * arr[a].CompareTo(arr[b]) : a.CompareTo(b));
foreach (int i in sortedIndices)
{
while (stack.Count > 0 && stack.Peek() < i)
nextJump[stack.Pop()] = i;
stack.Push(i);
}
}
}