-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
66 lines (65 loc) · 1.93 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//双指针加状态机。
using System;
namespace LongestMountainInArray
{
class Program
{
static void Main(string[] args)
{
int[] A = { 0, 0, 1, 0, 0, 1, 1, 1, 1, 1 };
Console.WriteLine(LongestMountain(A));
}
static int LongestMountain(int[] A)
{
if (A.Length < 3)
return 0;
const string up = "up";
const string down = "down";
const string start = "start";
string status = start;
int li = 0;
int hi = 1;
int len = 0;
bool flag = false;
for (; hi < A.Length; hi++)
{
if (A[hi] == A[hi - 1])
{
if(flag)
len = Math.Max(len, hi - li);
li = hi;
status = start;
continue;
}
switch (status)
{
case start:
status = A[hi] > A[hi - 1] ? up : down;
break;
case up:
if (A[hi] < A[hi - 1])
{
flag = true;
status = down;
}
break;
case down:
if (A[hi] > A[hi - 1])
{
status = up;
if (flag)
{
len = Math.Max(len, hi - li);
flag = false;
}
li = hi - 1;
}
break;
}
}
if (status == down && flag)
len = Math.Max(len, hi - li);
return len;
}
}
}