[프로그래머스 lv2] 구명보트(그리디/투포인터/python c++)

밀루·2023년 4월 9일
0

백준 문제풀이

목록 보기
32/51

https://school.programmers.co.kr/learn/courses/30/lessons/42885

def solution(people, limit):
    people.sort()
    answer = 0
    s, e = 0, len(people)-1
    while s <= e:
        if people[s] + people[e] <= limit: s += 1
        e -= 1
        answer += 1
    return answer
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int solution(vector<int> people, int limit) {
    int answer = 0;
    int start = 0, end = people.size()-1;
    sort(people.begin(), people.end());
    
    while (start <= end) {
        if (people[start] + people[end] <= limit) {start++;}
        end--;
        answer++;
    }
    return answer;
}
profile
벨로그에 틀린 코드나 개선할 내용이 있을 수 있습니다. 지적은 언제나 환영합니다.

0개의 댓글