-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path575.java
More file actions
46 lines (45 loc) · 1.45 KB
/
575.java
File metadata and controls
46 lines (45 loc) · 1.45 KB
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
__________________________________________________________________________________________________
sample 5 ms submission
class Solution {
public int distributeCandies(int[] candies) {
int maxNum = candies.length >> 1;
boolean[] exists = new boolean[200001];
int uniqueNum = 0;
for(int candy : candies){
if(!exists[candy + 100000]){
exists[candy + 100000] = true;
if(++uniqueNum == maxNum){
return maxNum;
}
}
}
return uniqueNum;
}
}
__________________________________________________________________________________________________
sample 39816 kb submission
class Solution {
public int distributeCandies(int[] candies) {
int candyCount = 1;
int ptr=0;
for (int x=0; x<candies.length-1; x++) {
int min = x;
for (int y=x+1; y<candies.length; y++) {
if (candies[y]<candies[min]) {
min = y;
}
}
int temp= candies[x];
candies[x] = candies[min];
candies[min] = temp;
}
for (int k=0; k<candies.length-1; k++) {
if (candies[k] != candies[k+1]) {
ptr=k+1;
candyCount++;
}
}
return Math.min(candyCount, candies.length/2);
}
}
__________________________________________________________________________________________________