알고리즘 #1

less than 1 minute read

알고리즘 #1

프로그래머스 ‘예산’

https://programmers.co.kr/learn/courses/30/lessons/43237

#include <string>  
#include <vector>  
#include <queue>  
#include <iostream>  
#include <algorithm>  
using namespace std;  

priority_queue<int, vector<int>, greater<int>> pq;  
int solution(vector<int> budgets, int M) {  
      int answer = 0;  
      int cnt = 0;  
      long sum = 0;  
      for(int i=0;i<budgets.size();i++){  
            pq.push(budgets[i]);  
            sum+=budgets[i];  
        }  
        if(sum<=M){  
            return *max_element(budgets.begin(), budgets.end());  
        }  
        while(!pq.empty()){  
            answer = M/(budgets.size()-cnt);  
            if(answer>pq.top()){  
                M-=pq.top();  
                pq.pop();  
                cnt++;  
            }  
            else break;  
        }  
        return answer;  
    }  
priority_queue 보다는 sort가 나았을것 같다.
*max_element 잊지말기