-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathMinMaxRiddle.cs
93 lines (77 loc) · 2.55 KB
/
MinMaxRiddle.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Min Max Riddle
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Solution {
// Complete the riddle function below.
static long[] riddle(long[] arr)
{
int N = arr.Length;
long[] result = new long[N];
long[] span = new long[N];
var deckValues = new Stack<long>();
var deckIndexes = new Stack<long>();
deckIndexes.Push(-1L);
// count number of ge elements to the left
for (int index = 0; index < N; index++)
{
while (TryPeek(deckValues, out var val) && val >= arr[index])
{
deckValues.Pop();
deckIndexes.Pop();
}
span[index] = index - deckIndexes.Peek() - 1;
deckValues.Push(arr[index]);
deckIndexes.Push(index);
}
// count number of ge elements to the right
deckValues.Clear();
deckIndexes.Clear();
deckIndexes.Push(N);
for (int index = N - 1; index >= 0; index--)
{
while (TryPeek(deckValues, out var val) && val >= arr[index])
{
deckValues.Pop();
deckIndexes.Pop();
}
span[index] += deckIndexes.Peek() - index - 1;
deckValues.Push(arr[index]);
deckIndexes.Push(index);
}
// fill results
for (int i = 0; i < N; i++)
result[(int)span[i]] = Math.Max(result[(int)span[i]], arr[i]);
// fill the gaps
for (int i = N - 2; i >= 0; i--)
result[i] = Math.Max(result[i], result[i + 1]);
return result;
}
static bool TryPeek<T>(Stack<T> stack, out T value)
{
value = default(T);
if (stack.Count == 0) return false;
value = stack.Peek();
return true;
}
static void Main(string[] args) {
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
int n = Convert.ToInt32(Console.ReadLine());
long[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), arrTemp => Convert.ToInt64(arrTemp))
;
long[] res = riddle(arr);
textWriter.WriteLine(string.Join(" ", res));
textWriter.Flush();
textWriter.Close();
}
}