반응형
Recent Posts
Notice
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 우선순위 큐
- dfs
- 알고리즘
- 깊이우선탐색
- string
- 리트코드
- two pointers
- programmers
- recursion
- HashMap
- PCCP
- DP
- Algorithm
- 브루트포스
- coding
- 재귀함수
- greedy
- priority queue
- Array
- binary tree
- 부분배열
- Java
- hashset
- leetcode
- ArrayList
Archives
- Today
- Total
지식창고
[Java]LeetCode 2244. Minimum Rounds to Complete All Tasks 본문
728x90
반응형
LeetCode 2244. Minimum Rounds to Complete All Tasks - Java
문 제 :
task의 difficulty를 값으로 가지는 정수배열 tasks가 주어진다.
작업자는 같은 difficulty를 가지는 작업을 2~3개를 한 번에 처리할 수 있다.
가장 최소로 작업을 처리하기 위한 횟수를 구해라.
(처리할 작업이 1개라면 -1을 리턴)
Input: tasks = [2,2,3,3,2,4,4,4,4,4]
Output: 4
Explanation: To complete all the tasks, a possible plan is:
- In the first round, you complete 3 tasks of difficulty level 2.
- In the second round, you complete 2 tasks of difficulty level 3.
- In the third round, you complete 3 tasks of difficulty level 4.
- In the fourth round, you complete 2 tasks of difficulty level 4.
It can be shown that all the tasks cannot be completed in fewer than 4 rounds, so the answer is 4.
접 근 :
tasks 배열에서 같은 값을 가지는 값을 다 카운팅 한 다음, 최소 작업 처리 횟수를 구한다.
해 결 :
1. 같은 값들을 모일 수 있게 하기 위해 tasks 배열을 정렬한다.
2. 같은 값들을 센다.
3. 일단 3으로 나누어 몫을 구한다.
4. 나머지가 있으면 (1 or 2) 작업 처리횟수를 +1 해준다.
Because) 나머지가 2일 경우 2로 묶어서 처리하면된다.
하지만 나머지가 1일 경우는 문제에 제시한 작업 그룹핑과 맞지 않는다. 그렇다면 잘못 계산한 것이 아닌가?
아니다. 3으로 카운팅한 작업 횟수에서 작업 하나를 빌려온다고 생각하면 해결이 가능하다.
작업이 (3, 1)로 나누어져 있는 것을 (2, 2)로 생각하는 것이다.
tasks 배열 정렬
Arrays.sort(tasks);
전체 작업을 돌면서 작업의 개수 카운팅
카운팅의 마지막이면 작업 처리 횟수를 더해준다.
for(int i=0; i<tasks.length; i++){
count++;
if(i == tasks.length-1 || tasks[i] != tasks[i+1]){ // 연속된 숫자의 마지막인지 검사
if(count == 1) return -1;
result += count/3; // count를 3으로 나눴을 때 몫
if(count % 3 != 0) result++; // 나머지가 0이 아니면
count=0;
}
}
결 과 :
class Solution {
public int minimumRounds(int[] tasks) {
int result = 0;
int count = 0; // 몇 개 인지
Arrays.sort(tasks);
for(int i=0; i<tasks.length; i++){
count++;
if(i == tasks.length-1 || tasks[i] != tasks[i+1]){ // 연속된 숫자의 마지막인지 검사
if(count == 1) return -1;
result += count/3; // count를 3으로 나눴을 때 몫
if(count % 3 != 0) result++; // 나머지가 0이 아니면
count=0;
}
}
return result;
}
}
728x90
반응형
'Algorithm > Leetcode' 카테고리의 다른 글
[Java] LeetCode 149. Max Points on a Line (0) | 2023.01.09 |
---|---|
[Java] LeetCode 452. Minimum Number of Arrows to Burst Balloons (0) | 2023.01.06 |
[Java]LeetCode 1834. Single-Threaded CPU (0) | 2022.12.29 |
[Java]LeetCode 1962. Remove Stones to Minimize the Total (0) | 2022.12.28 |
[Java]LeetCode 817. Linked List Components (0) | 2022.12.24 |
Comments