HOIT_B

[ 프로그래머스 C++ ] 구명보트 본문

작고소중한 알고리즘 풀기

[ 프로그래머스 C++ ] 구명보트

HOIT_77 2023. 11. 15. 23:03
728x90

문제 

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

몸무게 적은 순으로 정렬 

1번 하고 n번 더해서 limit 보다 작으면 answer 1회 추가 
시작 인덱스++, 시작이랑 끝이랑 같으니까 end--
더 크면 최소랑 탔는데 초과라 그냥 anwer++ , end-- 

 

코드 

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

using namespace std;

int solution(vector<int> people, int limit) {
    int answer = 0;
    int start = 0; // 시작 인덱스
    int end = people.size() - 1; //끝 인덱스
    
    sort(people.begin(), people.end());
    
    while (start <= end) {
        if (people[start] + people[end] <= limit) 
        {
            start++;   
        }
     
        end--;
        answer++;
    }
    
    return answer;
}

 

728x90
Comments