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 | 29 |
30 | 31 |
Tags
- Git
- Google Refund
- critical rendering path
- java
- draganddrop
- docker
- css framework
- screencapture
- spread 연산자
- nodejs
- server
- unity
- Unity Editor
- Spring Boot
- Camera Movement
- Digital Ocean
- rpg server
- springboot
- MySQL
- SDK upgrade
- Unity IAP
- express
- react
- Google Developer API
- --watch
- linux
- mongoDB
- Packet Network
- OverTheWire
- Camera Zoom
Archives
- Today
- Total
우당탕탕 개발일지
[프로그래머스] Level 3. 최고의 집합 본문
https://school.programmers.co.kr/learn/courses/30/lessons/12938
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
해결 방법
처음엔 [1,1,1,1...,1] 부터시작하여, 하나씩 더하는 방식으로 모든 경우의 수를 구해서 풀었다. 그러나 시간초과가 발생했다. 탐색은 아니고, 균등하게 분배할 수록 곱이 커진다는 것을 이용하여 푸는 문제였다. N개짜리 배열을 만들고 거기에 최대한 균등하게 배분한 뒤, 남은 수는 각 원소에 1씩 더하는 방법으로 간단하게 해결할 수 있는 문제였다.
#include <string>
#include <vector>
using namespace std;
vector<int> solution(int n, int s) {
if(n>s) return vector<int>(1,-1);
int val = s/n;
vector<int> answer (n, val);
int remain = s - val*n;
int idx =answer.size()-1;
for(int i=0; i<remain; i++){
answer[idx]+=1;
idx--;
}
return answer;
}
'Algorithm(c++) > Level 3' 카테고리의 다른 글
[프로그래머스] Level 3. 단속 카메라 (0) | 2025.02.26 |
---|---|
[프로그래머스] Level 3 . 숫자 게임 (1) | 2025.02.25 |
[프로그래머스] Level 3. 이중 우선순위 큐 (1) | 2025.02.23 |
[프로그래머스] Level 3. 정수 삼각형 (0) | 2025.02.22 |
[프로그래머스] Level 3. 외벽 점검 (1) | 2025.02.09 |