BoxesDiv2——SRM 622 DIV2
import java.util.*;
public class BoxesDiv2{
public BoxesDiv2(){}
public int findSize(int[] candyCounts)
{
Arrays.sort(candyCounts);
for(int i=0;i<candyCounts.length;i++){
int v = candyCounts[i];
int j=1;
while(j<v){
j<<=1;
}
candyCounts[i]=j;
}
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(candyCounts.length);
for(int i=0;i<candyCounts.length;i++){
pq.add(candyCounts[i]);
}
while(!pq.isEmpty()){
Integer v1 = pq.peek();
pq.poll();
if(pq.isEmpty()){
return v1;
}
Integer v2= pq.peek();
pq.poll();
int v3 = v1+v2;
int j=1;
while(j<v3){
j<<=1;
}
pq.add(j);
}
return 1;
}
}
아이디어:
상향 2의 지수 멱을 정돈하여
즉 1-> 1;2--》2;3,4--》4;5,6,7,8--》8
(1) 모든 원소를 2의 지수 멱에 따라 정돈한다.
(2) 더미를 사용하여 모든 원소를 더미에 넣는다
(3) 더미에 원소가 하나만 있으면 퇴적하고 더미 꼭대기 원소를 되돌려줍니다
(4) 그렇지 않으면 두 개의 원소를 취하고 덧붙인 후 위로 2의 지수 멱을 취하여 정돈하고 입적한다.
이 단계는 실제로 이 두 수의 큰 원소에 2를 곱하기만 하면 된다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
BoxesDiv2——SRM 622 DIV2아이디어: 상향 2의 지수 멱을 정돈하여 즉 1-> 1;2--》2;3,4--》4;5,6,7,8--》8 (1) 모든 원소를 2의 지수 멱에 따라 정돈한다. (2) 더미를 사용하여 모든 원소를 더미에 넣는다 (3) 더미...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.