Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- two pointers
- Stack
- string
- Binary Search
- Tree
- Number Theory
- dynamic programming
- Counting
- bit manipulation
- Class
- simulation
- 파이썬
- hash table
- geometry
- sorting
- greedy
- 자바
- Math
- Method
- Matrix
- implement
- array
- 구현
- Data Structure
- Binary Tree
- SQL
- java
- database
- 코딩테스트
- 코테
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 2644. Find the Maximum Divisibility Score 본문
1. Input
1) int[] nums
2) int[] divisors
2. Output
1) nums의 요소들 중 divisors[i]로 나누어 떨어지는 요소의 수를 divisibility score라고 할 때, 가장 큰 점수의 divisors[i]를 반환
- 가장 큰 점수가 여러개 나오는 경우, 가장 작은 divisors[i]를 반환
3. Constraint
1) 1 <= nums.length, divisors.length <= 1000
2) 1 <= nums[i], divisors[i] <= 10^9
4. Example
Input: nums = [4,7,9,3,9], divisors = [5,2,3] -> Output: 3
설명:
- 5로 나누어 떨어지는 숫자는 없다.
- 2로 나누어 떨어지는 숫자는 4 1개이다.
- 3으로 나누어 떨어지는 숫자는 9,3,9의 3개이다.
- 따라서 개수가 가장 많은 3을 반환한다.
5. Code
1) 첫 코드(2023/05/08)
class Solution {
public int maxDivScore(int[] nums, int[] divisors) {
Arrays.sort(divisors);
int min = 0, score = 0;
for(int i=divisors.length-1 ; i>=0 ; i--){
int count = 0;
for(int j=0 ; j<nums.length ; j++)
count += nums[j]%divisors[i]==0 ? 1 : 0;
if(count>=score){
score = count;
min = divisors[i];
}
}
return min;
}
}
2) 다시 풀어본 코드(2023/05/08)
class Solution {
public int maxDivScore(int[] nums, int[] divisors) {
HashMap<Integer,Integer> m = new HashMap<>();
int max = 0;
for(int i=divisors.length-1 ; i>=0 ; i--){
int count = 0;
for(int j=0 ; j<nums.length ; j++)
count += nums[j]%divisors[i]==0 ? 1 : 0;
if(count>=max){
max = count;
m.put(divisors[i], max);
}
}
Iterator it = m.entrySet().iterator();
ArrayList<Integer> list = new ArrayList<>();
while(it.hasNext()){
Map.Entry e = (Map.Entry)it.next();
if((int)e.getValue()==max)
list.add((int)e.getKey());
}
list.sort(Comparator.naturalOrder());
return list.get(0);
}
}
- 메모리는 조금 더 먹고, 속도는 조금 더 빨랐다. 하지만 그래도 둘다 영..
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 2652. Sum Multiples (0) | 2023.05.08 |
---|---|
[LeetCode/Easy] 2651. Calculate Delayed Arrival Time (0) | 2023.05.08 |
[LeetCode/Easy] 2643. Row With Maximum Ones (0) | 2023.05.08 |
[LeetCode/Easy] 2639. Find the Width of Columns of a Grid (0) | 2023.05.08 |
[LeetCode/Easy] 2614. Prime In Diagonal (0) | 2023.05.08 |