|
| 1 | +package hackerrank; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +public class PickingNumbers { |
| 8 | + //absolute difference 절대편차 <= 1 일때의 가장 긴 list의 길이를 출력하는 뮨제이다 |
| 9 | + public static int pickingNumbers(List<Integer> a) { |
| 10 | + // 문제의 조건에서 0< a[i] <100이므로 maxIndex는 100이다. |
| 11 | + int maxIndex = 100; |
| 12 | + int[] temp = new int[maxIndex]; |
| 13 | + |
| 14 | + //0부터 99까지의 숫자배열에 a의 요소와 동일한 숫자가 있으면 1씩 카운트룰 증가시킨다. |
| 15 | + //예를 들어 a = [1,2,1]인경우 temp[1]=2이고 temp[2]=1이다. |
| 16 | + for (int number : a) { |
| 17 | + temp[number]++; |
| 18 | + } |
| 19 | + |
| 20 | + int result = 0; |
| 21 | + |
| 22 | + for (int i = 0; i < maxIndex - 1; i++) { |
| 23 | + result = Math.max(result, temp[i] + temp[i + 1]); |
| 24 | + } |
| 25 | + return result; |
| 26 | + } |
| 27 | + public static void main(String[] args) { |
| 28 | + System.out.println(pickingNumbers(new ArrayList<>(Arrays.asList(1, 1, 2, 2, 4, 4, 5, 5, 5)))+", ans: 5"); |
| 29 | + System.out.println(pickingNumbers(new ArrayList<>(Arrays.asList(4, 6, 5, 3, 3, 1)))+", ans: 3"); |
| 30 | + System.out.println(pickingNumbers(new ArrayList<>(Arrays.asList(1, 2, 2, 3, 1, 2)))+", ans: 5"); |
| 31 | + System.out.println(pickingNumbers(Arrays.asList(98, 3, 99, 1, 97, 2)) == 2); |
| 32 | + System.out.println(pickingNumbers(Arrays.asList(1, 1, 1)) == 3); |
| 33 | + } |
| 34 | +} |
0 commit comments