반응형
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
- 그래프
- Nestjs
- 크롤링
- socket
- 수학
- 가천대
- 문자열
- TCP
- Node.js
- 타입스크립트
- BFS
- Crawling
- 프로그래머스
- dfs
- 레벨 1
- 알고리즘
- 타입 챌린지
- 자바스크립트
- Algorithm
- type challenge
- javascript
- 프로그래머스 레벨 2
- dp
- 소켓
- HTTP
- ip
- typescript
- HTTP 완벽 가이드
- 쉬운 문제
- 백준
Archives
- Today
- Total
kakasoo
[node.js] 예산 ( 프로그래머스 레벨1 ) 본문
반응형
//프로그래머스 레벨1 예산을 풀었습니다.
function solution(d, budget) {
let sum = 0;
let count = 0;
d.sort((o1, o2) => o1 - o2).some((el, i) => {
if (sum + el <= budget) {
sum += el;
count++;
return false;
} else {
return true;
}
});
return count;
}
function solution(d, budget) {
d = d.sort((a,b) => a - b);
const DP = new Array(d.length).fill(0);
if (budget - d[0] >= 0) {
DP[0] = 1;
budget -= d[0];
}
for (let i = 1; i < DP.length; i++) {
if (budget - d[i] >= 0) {
DP[i] = Math.max(DP[i -1] + 1, DP[i]);
budget -= d[i];
}
}
return Math.max(...DP);
}
정렬해서 작은 것부터 더하는 그리디한 방식이 더 쉽지만, DP로 풀 수도 있다.
반응형
'프로그래밍 > 알고리즘 풀이' 카테고리의 다른 글
[node.js] [1차]비밀지도 ( 프로그래머스 레벨1 ) (0) | 2021.06.28 |
---|---|
[node.js] 실패율 ( 프로그래머스 레벨 1 ) (0) | 2021.06.28 |
[node.js] 직사각형 별찍기 ( 프로그래머스 레벨1 ) (0) | 2021.06.28 |
[node.js] x만큼 간격이 있는 n개의 숫자 ( 프로그래머스 레벨1 ) (0) | 2021.06.28 |
[node.js] 행렬의 덧셈 ( 프로그래머스 레벨1 ) (0) | 2021.06.28 |