From 7eb4e85f7992c00235de3b245b4f268e14483dd0 Mon Sep 17 00:00:00 2001 From: rl544 <111494264+rl544@users.noreply.github.com> Date: Wed, 21 Dec 2022 01:00:28 +0900 Subject: [PATCH] =?UTF-8?q?uploaded=20at=202022.=2012.=2021.=20=EC=98=A4?= =?UTF-8?q?=EC=A0=84=201:00:29?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- programmers/구명보트/solution.java | 35 ++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 programmers/구명보트/solution.java diff --git a/programmers/구명보트/solution.java b/programmers/구명보트/solution.java new file mode 100644 index 0000000..6784a11 --- /dev/null +++ b/programmers/구명보트/solution.java @@ -0,0 +1,35 @@ +// [문제 링크]: https://school.programmers.co.kr/learn/courses/30/lessons/42885# + +import java.util.Arrays; +class Solution { + public int solution(int[] people, int limit) { + int answer = 0, ll = 0; + Arrays.sort(people); + for(int p = 0; p < people.length; p++){ + if(people[p] > limit - people[0]) { + ll = p; + break; + } + } + for(int p = ll; p < people.length; p++){ + if(people[p] > limit - people[0]) answer ++; + } + System.out.println(ll); + if(ll!=0){ + int x = 0; + for(int p = ll-1; p >= 0; p --){ + if(x == p) { + answer++; + x++; + break; + } + else if(p < x) break; + if(people[x]+people[p] <= limit) { + answer ++; + x++; + } else answer++; + } + } + return answer; + } +} \ No newline at end of file