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

 

1. ๋ฌธ์ œ : Link

์ž‘์—… ์™„๋ฃŒ๋„[90,40,20]์™€ ํ•˜๋ฃจ๋‹น ์ž‘์—…์ฒ˜๋ฆฌ ์†๋„ [10,1,5]๊ฐ€ ์ฃผ์–ด์กŒ์„๋•Œ ์„ ์ˆ˜ ์ž‘์—…์ด ์™„๋ฃŒ๋˜๋Š”๋Œ€๋กœ ์ž‘์—…์ด ์™„๋ฃŒ๋  ๋•Œ ํ•œ ๋ฒˆ์— ์™„๋ฃŒ๋˜๋Š” ์ž‘์—…์˜ ์ˆ˜

 

2. ํ’€์ด

๋จผ์ € ์„ ์ˆ˜ ์ž‘์—…์˜ day๋งŒํผ ์ง€๋‚˜๊ฐ”์„ ๋•Œ ํ•ด๋‹น ์ž‘์—…์ด ์™„๋ฃŒ๊ฐ€ ๋œ์ƒํƒœ์ธ์ง€ ํ™•์ธํ•˜๊ณ  ๋งŒ์•ฝ ์™„๋ฃŒ๊ฐ€ ๋˜์—ˆ๋‹ค๋ฉด fin ๊ฐ’์„ 1์˜ฌ๋ ค์ค€๋‹ค. ๋งŒ์•ฝ ์ฒ˜์Œ์ด๋‚˜ ์™„๋ฃŒ๋˜์ง€ ์•Š์„ ๊ฒฝ์šฐ๋ผ๋ฉด cnt๊ฐ’์„ ์—…๋ฐ์ดํŠธ ํ•ด์ฃผ๊ณ  fin๊ฐ’์„ ์˜ฌ๋ ค์ค€๋‹ค. ์ด ๋•Œ ์™„๋ฃŒ๋˜์ง€ ์•Š์€ ๊ฐ’์ด ๋‚˜์™”๋‹ค๋Š”๊ฑด ์ด์ „๊นŒ์ง€์˜ ์ž‘์—…์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ค˜์•ผํ•จ์„ ์˜๋ฏธํ•˜๋ฏ€๋กœ answer์— appendํ•ด์ค€๋‹ค.

 

๋งŒ์•ฝ ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰์ธ ๊ฒฝ์šฐ answer์— appendํ•ด์ค€๋‹ค.

 

3. ์ฝ”๋“œ

import math

def solution(progresses, speeds):
    answer = [] 
    fin = 0
    cnt = 0
    
    for i in range(len(progresses)):
        pro = progresses[i]
        speed = speeds[i]
        left = 100 - pro
        
        if left - speed * cnt <= 0:
            fin += 1
        else:   
            cnt += math.ceil(left / speed) - cnt
            if i == 0:
                fin += 1
            else:
                answer.append(fin)
                fin = 1
        
        if i == len(progresses) - 1:
            answer.append(fin)
            
    return answer

ceil : ์˜ฌ๋ฆผ

floor : ๋‚ด๋ฆผ

class Solution {
    public int[] solution(int[] progresses, int[] speeds) {
        int days = 0;
        int temp = 0;
        ArrayList<Integer> arr = new ArrayList<Integer>();
        
        if(progresses.length == 1) {
            int[] answer = new int[1];
            answer[0] = 1;
            return answer;
        }
        
        for(int i=0; i<progresses.length; i++){
            int leftpro = 100-progresses[i];
            int leftdays = (int)Math.ceil(leftpro/speeds[i]);
            if(leftpro%speeds[i] != 0) leftdays++;
            if(leftdays > days){
                days = leftdays;
                if(i != 0) {
                    arr.add(temp);
                }
                temp = 1;
                if(i == progresses.length-1){
                    arr.add(temp);
                }
            }
            else{
                temp++;
                if(i == progresses.length-1){
                    arr.add(temp);
                }
            }
        }
        int[] answer = new int[arr.size()];
        for(int i=0; i<arr.size(); i++){
            answer[i] = arr.get(i);
        }

        return answer;
    }
}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ