-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocksLaundry.java
69 lines (50 loc) · 1.74 KB
/
SocksLaundry.java
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
67
68
69
import java.util.*;
public class SocksLaundry {
public int getMaximumPairOfSocks(int noOfWash, int[] cleanPile, int[] dirtyPile){
int noCleanPairs = 0;
int noAllDirtyPairs = 0;
int noOfWatchInClean_Dirty = 0;
Set<Integer> a = new HashSet<Integer>();
Set<Integer> b = new HashSet<Integer>();
List<Integer> noRemainingDirtySocks = new ArrayList<>();
Arrays.sort(cleanPile);
// get number of clean pairs
for (int value : cleanPile) {
if (a.isEmpty())
a.add(value);
else if (!a.contains(value)) {
a.add(value);
} else if (a.contains(value)) {
noCleanPairs++;
a.remove(value);
}
}
if(noOfWash == 0){
return noCleanPairs;
}
Arrays.sort(dirtyPile);
if(noOfWash > 0){
for (int i : dirtyPile) {
if (a.contains(i)) {
noOfWash--;
noOfWatchInClean_Dirty++;
a.remove(i);
} else noRemainingDirtySocks.add(i);
//ensure no of washes no exceeded
if(noOfWash == 0) break;
}
for (int i : noRemainingDirtySocks) {
//ensure no of washes no exceeded
if(noOfWash == 0) break;
if (b.isEmpty()) b.add(i);
else if (!b.contains(i)) b.add(i);
else if (b.contains(i) && noOfWash >= 2) {
noOfWash-=2;
noAllDirtyPairs++;
b.remove(i);
}
}
}
return noCleanPairs + noOfWatchInClean_Dirty + noAllDirtyPairs ;
}
}