Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- axios
- react-router
- 이코테
- react
- 항해플러스
- JavaScript
- sw expert academy
- Algorithm
- redux-toolkit
- C++
- 리액트
- 프로그래머스
- redux
- json-server
- redux-saga
- 매일메일
- 자바
- createSlice
- java
- maeil-mail
- programmers
- react-redux
- 코딩테스트합격자되기
- 알고리즘
- 테코테코
- Get
- 항해99
- useDispatch
- Python
- SW
Archives
- Today
- Total
Binary Journey
[프로그래머스] 예산 본문
반응형
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
오름차순 정렬 후 누적합계가 budget 값 이하인 것만 추출
** Javascript
function solution(d, budget) {
d.sort((a, b) => a - b);
while(d.reduce((a, b) => a + b, 0) > budget) d.pop();
return d.length;
}
효율성이 더 나은 건 아래
function solution(d, budget) {
d.sort((a, b) => a - b);
let total = 0;
for (let i = 0; i < d.length; i++) {
total += d[i];
if (total > budget) return i;
}
return d.length;
}
** Python
def solution(d, budget):
answer = 0
for i, x in enumerate(sorted(d)):
answer += x
if answer > budget:
return i
return len(d)
** Java
import java.util.*;
class Solution {
public int solution(int[] d, int budget) {
Arrays.sort(d);
int sum = 0;
for (int i = 0; i < d.length; i++) {
sum += d[i];
if (sum > budget) return i;
}
return d.length;
}
}
반응형
'프로그래머스 > level 1' 카테고리의 다른 글
[프로그래머스] 폰켓몬 (0) | 2021.10.18 |
---|---|
[프로그래머스] 체육복 (0) | 2021.10.18 |
[프로그래머스] 약수의 개수와 덧셈 (0) | 2021.10.18 |
[프로그래머스] 3진법 뒤집기 (0) | 2021.10.18 |
[프로그래머스][위클리챌린지] 8주차 최소직사각형 (0) | 2021.09.30 |