반응형
- 문제
https://school.programmers.co.kr/learn/courses/30/lessons/12982?language=javascript
- 풀이
- Python
def solution(d, budget):
answer = 0
count = 0
while d:
answer += min(d)
d.remove(min(d))
if answer > budget:
return count
else:
count += 1
return count
- JavaScript
function solution(d, budget) {
d.sort(function(a, b) { return a - b });
let ans = 0;
let result = 0;
for (let i of d) {
ans += i;
if (ans <= budget)
result += 1;
}
return result;
}
- Java
import java.util.*;
class Solution {
public int solution(int[] d, int budget) {
int answer = 0;
int count = 0;
Arrays.sort(d);
for (int i : d) {
if (answer < budget) {
answer += i;
if (answer <= budget) count++;
else return count;
}
}
return count;
}
}
반응형
'코딩 테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스 - Python/JavaScript] Lv.1 숫자 문자열과 영단어 (0) | 2023.02.07 |
---|---|
[프로그래머스 - Python] Lv.1 다트게임 (0) | 2023.02.07 |
[프로그래머스 - Python] Lv.1 신고 결과 받기 (0) | 2023.02.07 |
[프로그래머스 - Python] Lv.1 크레인 인형뽑기 게임 (0) | 2023.02.03 |
[프로그래머스 - Python] Lv.1 과일 장수 (0) | 2023.02.03 |