๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm

[Algorithm/Java][ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ธฐ๋Šฅ๊ฐœ๋ฐœ

๋ฐ˜์‘ํ˜•

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ธฐ๋Šฅ๊ฐœ๋ฐœ

https://programmers.co.kr/learn/courses/30/lessons/42586

๋ฌธ์ œ์ ‘๊ทผ

๊ฐ ๊ธฐ๋Šฅ๋งˆ๋‹ค ๋‚จ์€ ์ผ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ด์ฃผ๊ณ , ์•ž ๊ธฐ๋Šฅ์˜ ๋‚จ์€ ์ผ์ˆ˜๋ณด๋‹ค ์ž‘์€ ์ผ์ˆ˜๋ฅผ ๊ฐ€์ง„ ๊ธฐ๋Šฅ๋งˆ๋‹ค count๋ฅผ ์ฆ๊ฐ€์‹œ์ผœ์„œ ํ•œ ๋ฐฐํฌ ๋‹จ๊ณ„์— ํฌํ•จ๋˜๋„๋ก ์ ‘๊ทผํ–ˆ๋‹ค.

Code

import java.util.ArrayList;

class Solution {
    public int[] solution(int[] progresses, int[] speeds) {
        int[] remainDays = new int[progresses.length];
        //๊ฐ ๊ธฐ๋Šฅ์„ ์™„๋ฃŒํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์ผ ์ €์žฅ
        for(int i = 0; i<progresses.length; i++){
            int remain = 100 - progresses[i];
            remainDays[i] = (remain % speeds[i] == 0) ? remain/speeds[i] : remain/speeds[i] + 1;
        }

        // ๊ฐ๊ฐ ๋ฐฐํฌ ๋‹จ๊ณ„์—์„œ ๋ช‡๊ฐœ์˜ ๊ธฐ๋Šฅ์ด ํฌํ•จ๋  ์ง€ ๊ณ„์‚ฐ
        ArrayList<Integer> deploy = new ArrayList<>();
        for(int i = 0 ; i< remainDays.length; i++){
            if(remainDays[i]<0) continue;
            int count = 1;
            for(int j = i+1; j<remainDays.length; j++){
                if(remainDays[j] >remainDays[i]) break;
                remainDays[j] = -1;
                count++;
            }
            deploy.add(count);
        }
        int[] answer = new int[deploy.size()];
        for(int i = 0; i<answer.length; i++){
            answer[i] = deploy.get(i);
        }
        return answer;
    }
}

์–ด๋ ค์› ๋˜ ์  / ๋ฐฐ์šด ์  / ๋Š๋‚€ ์ 

๋ฌธ์ œ ๋ถ„๋ฅ˜๊ฐ€ ์Šคํƒ/ํ๋กœ ๋ถ„๋ฅ˜๋˜์–ด ์žˆ์–ด์„œ ํ๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์–ด๋ณด๋ ค๊ณ  ํ–ˆ๋‹ค๊ฐ€ ์‹คํŒจํ•ด์„œ ๊ทธ๋ƒฅ ์ƒ๊ฐ๋‚˜๋Š”๋Œ€๋กœ ํ’€์—ˆ๋‹ค.

๋ฐ˜์‘ํ˜•