일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 서이추 매크로
- 셀레니움
- Selenium 네이버 블로그
- 스크래퍼
- 실시간
- Node
- Java
- 크롤러
- 웹소켓
- 서로이웃추가 자동
- 국세청
- node.js
- 크롤링
- Selenium
- 실시간 웹소켓 서버
- 네이버 블로그 이웃추가 자동
- nodejs
- kwoss2341
- 웹소켓 서버
- rabbitmq
- 네이버 블로그
- amqplib
- 서로이웃추가 매크로
- 서이추 자동
- socket.io
Archives
- Today
- Total
defaultK
[프로그래머스] (최소히프) 더 맵게 c++ 본문
https://programmers.co.kr/learn/courses/30/lessons/42626
코딩테스트 연습 - 더 맵게
매운 것을 좋아하는 Leo는 모든 음식의 스코빌 지수를 K 이상으로 만들고 싶습니다. 모든 음식의 스코빌 지수를 K 이상으로 만들기 위해 Leo는 스코빌 지수가 가장 낮은 두 개의 음식을 아래와 같
programmers.co.kr
기본적인 아이디어.
1. 스코빌 지수를 최소힙으로 구현.
2. 섞은 음식의 소코빌 지수를 구한 후 섞인 두 음식은 delete 연산 ,섞어서 만든 음식은 insert 연산
3. 최솟값이 K이상일때 까지 반복 후 출력
4. 안되면 -1
#include <string>
#include <vector>
#include <iostream>
using namespace std;
void swap(int heap[],int x,int y)
{
int temp=heap[x];
heap[x]=heap[y];
heap[y]=temp;
return;
}
void insertheap(int heap[],int x,int i)
{
i++;
heap[i]=x;
while(1)
{
if(i==1) break;
if(x<heap[i/2])
{
swap(heap,i,i/2);
i=i/2;
}
else
{
break;
}
}
}
void deleteheap(int heap[],int n)
{
if(n==0) return;
int item=heap[1];
int temp=heap[n];
n--;
int i=1;
int j=2;
while(j<=n)
{
if(j<n && heap[j]>heap[j+1]) j=j+1;
if(temp<=heap[j]) break;
heap[i]=heap[j];
i=j;
j=j*2;
}
heap[i]=temp;
}
//https://programmers.co.kr/learn/courses/30/lessons/42626#
int solution(vector<int> scoville, int K) {
int answer = 0;
int n=scoville.size();
int heap[n+2];
for(int i=0; i<n; i++)
{
insertheap(heap,scoville[i],i);
}
int heapsize=n;
int mixsc;
int cnt=0;
while(n>1)
{
mixsc=heap[1]+min(heap[2],heap[3])*2;
deleteheap(heap,n);
deleteheap(heap,n-1);
insertheap(heap,mixsc,n-2);
n--;
cnt++;
if(heap[1]>=K)
{
return cnt;
}
}
return -1;
}
'알고리즘 ( C++ ) > 프로그래머스' 카테고리의 다른 글
[프로그래머스] (조합) 카카오 메뉴 리뉴얼 c++ (0) | 2021.02.05 |
---|---|
[프로그래머스] (그리디) 큰 수 만들기 c++ (0) | 2021.01.31 |
[프로그래머스] 카카오 캠핑 c++ (0) | 2021.01.27 |
[프로그래머스] (DFS/BFS) 카카오 경주로 건설 c++ (0) | 2021.01.25 |
[프로그래머스] (문자열, 정렬) 카카오 파일명 정렬 c++ (0) | 2021.01.24 |
Comments