-
Notifications
You must be signed in to change notification settings - Fork 115
/
0904-FruitIntoBaskets.cs
51 lines (48 loc) · 1.25 KB
/
0904-FruitIntoBaskets.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
//-----------------------------------------------------------------------------
// Runtime: 212ms
// Memory Usage:
// Link:
//-----------------------------------------------------------------------------
using System;
namespace LeetCode
{
public class _0904_FruitIntoBaskets
{
public int TotalFruit(int[] tree)
{
var b1 = -1;
var b1Size = 0;
var b2 = -1;
var b2Size = 0;
var max = 0;
foreach (var treeType in tree)
{
if (b2 == -1)
{
b2 = treeType;
b2Size = 1;
}
else if (b2 == treeType)
{
b2Size++;
}
else if (b1 == treeType)
{
b1 = b2;
b1Size += b2Size;
b2 = treeType;
b2Size = 1;
}
else
{
b1 = b2;
b1Size = b2Size;
b2 = treeType;
b2Size = 1;
}
max = Math.Max(max, b1Size + b2Size);
}
return max;
}
}
}