관리 메뉴

fatalite

C++ / 구명보트 본문

코딩 인터뷰/프로그래머스

C++ / 구명보트

fataliteforu 2023. 5. 8. 20:34

Problem

난이도 : 프로그래머스 2단계 68%

분류 : Greedy

 

Solution

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

int solution(vector<int> people, int limit) {
    int answer = 0;
    sort(people.begin(),people.end(),greater<int>());
    int left = 0;
    int right = people.size() - 1;
    while(left <= right){
        while(people[left] + people[right] > limit && left <= right){
            left++;
            answer++;
        }
        if(left <= right){
            right--;
            left++;
            answer++;
        }
    }
    return answer;
}

'코딩 인터뷰 > 프로그래머스' 카테고리의 다른 글

C++ / N개의 최소 공배수 구하기  (0) 2023.05.10
C++ / 예상대진표  (0) 2023.05.09
C++ / 카펫  (0) 2023.05.07
C++ / 영어 끝말잇기  (0) 2023.05.07
C++ / 숫자의 표현  (0) 2023.04.24