Skip to content

Commit ecb3bc8

Browse files
add solution for Fruit in Basket.
1 parent ad4412a commit ecb3bc8

File tree

1 file changed

+47
-0
lines changed
  • C#/LeetCodeResolves/LeetCodeResolves/FruitInBaskets

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace LeetCodeResolves.FruitInBaskets
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
var fruits = new int[] {3,3,3,1,2,1,1,2,3,3,4};
12+
var result = TotalFruit(fruits);
13+
14+
Console.ReadLine();
15+
}
16+
17+
static int TotalFruit(int[] fruits)
18+
{
19+
int left = 0, right = 0, cnt = 0, res = 0;
20+
var dic = new Dictionary<int,int>();
21+
while(right < fruits.Length)
22+
{
23+
if(dic.ContainsKey(fruits[right]))
24+
dic[fruits[right]]++;
25+
else
26+
dic.Add(fruits[right], 1);
27+
28+
if(dic[fruits[right]]== 1)
29+
cnt++;
30+
31+
right++;
32+
33+
while(cnt > 2)
34+
{
35+
dic[fruits[left]]--;
36+
if(dic[fruits[left]] == 0)
37+
cnt--;
38+
left++;
39+
}
40+
41+
res = Math.Max(res, right - left);
42+
}
43+
44+
return res;
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)