GitHubSeob

C++ / 프로그래머스 / 최고의 집합 본문

Programmers/Level 3

C++ / 프로그래머스 / 최고의 집합

GitHubSeob 2023. 6. 26.

문제

https://school.programmers.co.kr/learn/courses/30/lessons/12938

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제풀이

먼저 n이 2일 때, 3일 때 순으로 숫자들을 살펴봤더니, 숫자들끼리의 차이가 작을수록 좋다 곱이 최대가 됐다.

 

n=2, s=9인 경우 4, 5

n=3, s=14인 경우 4, 5, 5

 

따라서 s를 n으로 나눈 몫이 answer, s는 s - s/n이 된다.

 

코드

#include <string>
#include <vector>
using namespace std;

vector<int> solution(int n, int s) {
    vector<int> answer;
    if (n > s)
        return { -1 };
    n++;
    while (--n) {
        answer.push_back(s / n);
        s -= s / n;
    }

    return answer;
}