늘
탐욕법(Greedy)_구명보트 본문
728x90
백준 저울문제 www.acmicpc.net/problem/2437를 못풀어서 한이 맺혀서 프로그래머스 그리디 문제를 풀었다..!!
level2 치고는 쉬운 그리디 문제였다. 덕분에 그리디 자신감도 다시 찾았다!!
*그리디 여러 문제 풀어보면서 그리디는 은근 아이디어 싸움(?) 인것 같다. 난이도가 높아질수록..
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int solution(vector<int> people, int limit) {
int answer = 0;
sort(people.begin(), people.end());
int start=0;
int sum=people[0];
for(int i=people.size()-1; i>=start; --i){
if(sum + people[i] <=limit){
answer++;
start++;
sum=people[start];
}
else{
answer++;
continue;
}
}
return answer;
}
최대 보트가 2명만 탈수 있어서 인지 배열을 소팅을 해주고
맨 작은 수와 맨큰 수를 비교해서 최대한 많이 태우도록 생각했다..!
728x90
'알고리즘_프로그래머스 > 기타 문제' 카테고리의 다른 글
타겟 넘버 (0) | 2021.05.18 |
---|---|
괄호 회전하기(월간 코드 챌린지 시즌2) Lv.2 (0) | 2021.04.20 |
해시_전화번호 목록 (0) | 2021.03.28 |
완전탐색_카펫 (0) | 2021.03.23 |
탐욕법(Greedy)_조이스틱 (0) | 2021.03.18 |
Comments