// Process
// 1. 총 카드묶음 개수, 각 카드묶음의 크기들 입력 받는다.
// 2. 각 카드묶음의 크기들을 입력 받을 때, 추후에 최소값을 찾기 쉽도록 우선순위 큐를 이용한다.
// 3. 남은 카드묶음이 1개가 될 때까지 반복한다.
// 3.1. 최소크기 카드묶음을 빼둔다. (first)
// 3.2. 최소크기 카드묶음을 빼둔다. (second)
// 3.3. 비교한 횟수(first + second)를 전체 비교횟수에 더한다.
// 3.4. 합쳐진 카드묶음크기를 다시 남은 카드묶음에 추가해둔다.
// 4. 전체 비교횟수를 반환한다.
import java.util.*;
import java.io.*;
class CardSorting {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int cards = Integer.parseInt(br.readLine());
PriorityQueue<Integer> cardPacks = new PriorityQueue<>();
for (int i = 0; i < cards; ++i)
cardPacks.add(Integer.parseInt(br.readLine()));
int comparisonSum = 0;
int first;
int second;
while (cardPacks.size() > 1) {
first = cardPacks.poll();
second = cardPacks.poll();
comparisonSum += (first + second);
cardPacks.add(first+second);
}
System.out.println(comparisonSum);
}
}
백준 1080 - 행렬, 문제풀이, 프로그래머스, 알고리즘, Programmers, Stack, Queue, Hash, 코딩테스트, Algorith
// Process // 1. Input n, m, matrixA,B // 2. Check are matrixA,B same // 3. Check n >= 3, m >= 3 // 4. Iterate 0 to n-2 // 4.1. Iterate 0 to m-2 // 4.1.1. Flip..
itdar.tistory.com
LeetCode #175 CombineTwoTables. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그
LeetCode #175 CombineTwoTables. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터..
itdar.tistory.com
LeetCode #896 MonotonicArray. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문
LeetCode #896 MonotonicArray. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,문제해결능력,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,..
itdar.tistory.com