-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFruit_Into_Baskets.java
More file actions
36 lines (36 loc) · 965 Bytes
/
Fruit_Into_Baskets.java
File metadata and controls
36 lines (36 loc) · 965 Bytes
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
import java.util.*;
public class Fruit_Into_Baskets {
class Solution {
public int totalFruit(int[] fruits) {
int n = fruits.length;
int l = 0;
int r = 0;
int maxlen = 0;
int[] hashing = new int[n];
int type_count = 0;
while(r<n)
{
hashing[fruits[r]]++;
if(hashing[fruits[r]] == 1)
{
type_count++;
}
while(type_count>2)
{
hashing[fruits[l]]--;
if(hashing[fruits[l]] == 0)
{
type_count--;
}
l++;
}
if(type_count<=2)
{
maxlen = Math.max(maxlen , r-l+1);
}
r++;
}
return maxlen;
}
}
}