-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPickingNumbers.java
More file actions
34 lines (29 loc) ยท 1.31 KB
/
PickingNumbers.java
File metadata and controls
34 lines (29 loc) ยท 1.31 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
package hackerrank;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class PickingNumbers {
//absolute difference ์ ๋ํธ์ฐจ <= 1 ์ผ๋์ ๊ฐ์ฅ ๊ธด list์ ๊ธธ์ด๋ฅผ ์ถ๋ ฅํ๋ ๋ฎจ์ ์ด๋ค
public static int pickingNumbers(List<Integer> a) {
// ๋ฌธ์ ์ ์กฐ๊ฑด์์ 0< a[i] <100์ด๋ฏ๋ก maxIndex๋ 100์ด๋ค.
int maxIndex = 100;
int[] temp = new int[maxIndex];
//0๋ถํฐ 99๊น์ง์ ์ซ์๋ฐฐ์ด์ a์ ์์์ ๋์ผํ ์ซ์๊ฐ ์์ผ๋ฉด 1์ฉ ์นด์ดํธ๋ฃฐ ์ฆ๊ฐ์ํจ๋ค.
//์๋ฅผ ๋ค์ด a = [1,2,1]์ธ๊ฒฝ์ฐ temp[1]=2์ด๊ณ temp[2]=1์ด๋ค.
for (int number : a) {
temp[number]++;
}
int result = 0;
for (int i = 0; i < maxIndex - 1; i++) {
result = Math.max(result, temp[i] + temp[i + 1]);
}
return result;
}
public static void main(String[] args) {
System.out.println(pickingNumbers(new ArrayList<>(Arrays.asList(1, 1, 2, 2, 4, 4, 5, 5, 5)))+", ans: 5");
System.out.println(pickingNumbers(new ArrayList<>(Arrays.asList(4, 6, 5, 3, 3, 1)))+", ans: 3");
System.out.println(pickingNumbers(new ArrayList<>(Arrays.asList(1, 2, 2, 3, 1, 2)))+", ans: 5");
System.out.println(pickingNumbers(Arrays.asList(98, 3, 99, 1, 97, 2)) == 2);
System.out.println(pickingNumbers(Arrays.asList(1, 1, 1)) == 3);
}
}