일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- java
- 코틀린
- 갑상선암
- MYSQL
- kotlin
- 카페
- 수술
- 프로그래머스
- 자바
- 맛집
- kotlin In Action
- 동위원소치료
- 알고리즘
- 임파선전이
- 개발책
- android auto
- Compose
- 저요오드식
- Android
- firebase
- Android Compose
- 방사성동위원소치료
- 방사성 동위원소 치료
- 코틀린 인 액션
- 입원
- 정렬 알고리즘
- 개발독서
- 백준알고리즘
- 림프절전이
- leetcode
Archives
- Today
- Total
새우버거의 개발 블로그
[Java] LeetCode 914 : X of a Kind in a Deck of Cards 본문
LeetCode # 914
Example 1:
Input: deck = [1,2,3,4,4,3,2,1]
Output: true
Explanation: Possible partition [1,1],[2,2],[3,3],[4,4].
Note:
- 1 <= deck.length <= 10⁴
- 0 <= deck[i] < 10⁴
Solution #1 (10ms)
class Solution {
public boolean hasGroupsSizeX(int[] deck) {
Map<Integer, Integer> map = new HashMap<>();
for (int i : deck) {
if (map.containsKey(i)) {
int value = map.get(i);
map.put(i, value + 1);
} else {
map.put(i, 1);
}
}
List<Integer> list = new ArrayList<Integer>();
for (Integer value : map.values()) {
list.add(value);
}
int g = list.get(0);
for (int i = 1; i < list.size(); i++) {
g = gcd(g, list.get(i));
}
return g != 1;
}
private int gcd(int x, int y) {
return x == 0 ? y : gcd(y % x, x);
}
}
Result #1
Solution #2 (3ms)
class Solution {
public boolean hasGroupsSizeX(int[] deck) {
if (deck.length >= 2) {
int[] array = new int[10000];
for (int d : deck) {
array[d] = array[d] + 1;
}
int result = 0;
for (int i = 0; i < 10000; i++) {
if (array[i] > 0) result = gcd(result, array[i]);
}
return result != 1;
}
return false;
}
private int gcd(int x, int y) {
return x == 0 ? y : gcd(y % x, x);
}
}
Result #2
More Algorithm!
👇👇
github.com/ggujangi/ggu.leet-code
ggujangi/ggu.leet-code
LeetCode, Java. Contribute to ggujangi/ggu.leet-code development by creating an account on GitHub.
github.com
출처 : LeetCode
'알고리즘 > LeetCode' 카테고리의 다른 글
[Java] LeetCode 1518 : Water Bottles (0) | 2021.04.17 |
---|---|
[Java] LeetCode 290 : Word Pattern (0) | 2021.04.17 |
[Java] LeetCode 1486 : XOR Operation in an Array (0) | 2021.04.17 |
[Java] LeetCode 977 : Squares of a Sorted Array (0) | 2021.03.30 |
[Java] LeetCode 1295 : Find Numbers with Even Number of Digits (0) | 2021.03.30 |