rueki

프로그래머스(Lv2). 구명보트(Greedy) 본문

프로그래머스 연습

프로그래머스(Lv2). 구명보트(Greedy)

륵기 2022. 10. 5. 02:39
728x90
반응형

 

 

- 파이썬

def solution(people, limit):
    answer = 0
    
    people.sort()
    s, e = 0, len(people) - 1
    while s<=e:
        answer += 1
        if people[s] + people[e] <= limit:
            s += 1
        e -=1
    
    return answer

- C++

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int solution(vector<int> people, int limit) {
    int answer = 0;
    int s = 0;
    int e = people.size()-1;
    
    sort(people.begin(), people.end());
    
    int sum = 0;
    while (s<=e)
    {
        if((people[s] + people[e]) <=limit)
        {
            s++;
        }
        
        e--;
        
        
        answer ++;
    }
         
    
    return answer;
}
728x90
반응형
Comments