Post

[2025 프로그래머스 코드챌린지 2차 예선] 서버 증설 횟수

[2025 프로그래머스 코드챌린지 2차 예선] 서버 증설 횟수

🙋‍♂️ 들어가며

이번 문제에서는 현재 가동중인 서버 현황을 잘 확인하고, 최소한의 전력을 사용하는게 핵심이다.

test case 1번 기준으로 작성했다

1
2
m = 3;
k = 5;

Desktop View

이런식으로 풀면 되겠다.


준비한 것은 2개의 풀이다.

2중 반복문

그리고

1중 반복문인 DP


코드는 다음과 같다



✅ 2중 반복문

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
32
33
34
35
class Solution {
    public int solution(int[] players, int m, int k) {
        int cnt = 0;
        
        int time = 24;
        // 현재 가동중인 서버 현황
        int[] DP = new int[time];
        
        for (int t = 0; t < time; t++) {
            // 현재 필요한 서버수
            int req_server = players[t] / m;
            int diff = 0;
            
            // 현재 가동중인 서버 < 현재 필요한 서버수라면??
            if (DP[t] < req_server) {
                diff = req_server - DP[t];
                cnt += diff;
            }
            
            // 현재 가동중인 서버에 k 만큼 전력 추가
            for (int i = t; i < t + k; i++) {
                // 범위 초과시 -> skip
                if (i >= time) continue;
                
                // 그게 아니라면
                DP[i] += diff;
            }
            
        }
        
        
        return cnt;
    }
}




메모리, 수행시간을 절약하기 위해 누적합 DP를 사용해야겠다

✅ 1중 반복문 DP

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
32
33
34
35
36
class Solution {
    public int solution(int[] players, int m, int k) {
        int time = 24;
        
        // 누적합용 DP
        int[] DP = new int[time];
        
        for (int t = 0; t < time; t++) {
            // 1. 누적합은 항상 갱신
            if (t > 0) {
                DP[t] = DP[t-1];
            }
            
            // 2. 마지막으로 서버를 켰을 수도 있고, 켜지 않았을 수도 있을때, 누적 몇개인지?
            int last_lighted_server_cum_cnt = 0;
            if (t >= k) {
                last_lighted_server_cum_cnt = DP[t-k];
            }
            
            // 3. 현재 구동중인 서버수
            int cur_running_server = DP[t] - last_lighted_server_cum_cnt;
            
            // 4. 필요한 서버수
            int req_server = players[t] / m;
            
            // 5. 만약에 서버수가 모자라다면?
            if (cur_running_server < req_server) {
                int diff = req_server - cur_running_server;
                DP[t] += diff;
            }
            
        }
        
        return DP[time-1];
    }
}
This post is licensed under CC BY 4.0 by the author.
3D GIF

Trending Tags

도움이 필요하면
나를 눌러 구리🐻
챗봇
너굴
bot
어서와 구리 ʕ ·ᴥ·ʔ
무엇이 필요하셔 구리?

Trending Tags

도움이 필요하면
나를 눌러 구리🐻
챗봇
너굴
bot
어서와 구리 ʕ ·ᴥ·ʔ
무엇이 필요하셔 구리?