-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
49 lines (47 loc) · 1.42 KB
/
Program.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
//使用单调栈,将T中每个数字的index压栈。在当前数字比栈顶大的时候就一直弹栈,并记录结果。
//暴力求解,不会超时,但速度很慢。
using System;
using System.Collections;
using System.Collections.Generic;
namespace DailyTemperatures
{
class Program
{
static void Main(string[] args)
{
int[] T = { 73, 74, 75, 71, 69, 72, 76, 73 };
Console.WriteLine(DailyTemperatures_MonoStack(T));
}
static int[] DailyTemperatures_MonoStack(int[] T)
{
int[] res = new int[T.Length];
var stack = new Stack<int>();
for (int i = 0; i < T.Length; i++)
{
while (stack.Count != 0 && T[stack.Peek()] < T[i])
{
var index = stack.Pop();
res[index] = i - index;
}
stack.Push(i);
}
return res;
}
static int[] DailyTemperatures_BruteForce(int[] T)
{
int[] res = new int[T.Length];
for (int i = 0; i < T.Length; i++)
{
for (int j = i + 1; j < T.Length; j++)
{
if (T[j] > T[i])
{
res[i] = j - i;
break;
}
}
}
return res;
}
}
}