Publish:

ํƒœ๊ทธ: , , , , ,

์นดํ…Œ๊ณ ๋ฆฌ:

img_3.png

๋ฌธ์ œ

๋ฌธ์ œ ๋งํฌ

img_3.png

ํ’€์ด

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
class Solution {
  public int[] solution(int[] progresses, int[] speeds) {

    List<Integer> days = new ArrayList<>();
    for (int i = 0; i < progresses.length; i++) {
      int v = (int) Math.ceil((100 - (double) progresses[i]) / (double) speeds[i]);
      days.add(v);
    }
    
    ArrayList<Integer> result = new ArrayList<>();
    int count = 1;
    int prev = days.get(0);

    for (int i = 1; i < days.size(); i++) {
      if (prev >= days.get(i)) {
        count++;
      } else {
        result.add(count);
        count = 1;
        prev = days.get(i);
      }
    }
    result.add(count);


    int[] answer = new int[result.size()];
    for (int i = 0; i < answer.length; i++) {
      answer[i] = result.get(i);
    }

    return answer;

  }
}

days ์•ˆ์—๋Š” ๊ฐ ์ž‘์—…์ด ์™„๋ฃŒ๋˜๊ธฐ ๊นŒ์ง€ ๋‚จ์€ ๊ธฐ๊ฐ„์ด ๋“ค์–ด์žˆ๋‹ค. ๋งจ ์•ž ์ž‘์—…๋ถ€ํ„ฐ ์ˆœ์ฐจ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์šฐ์„  prev ๋ฅผ days ์˜ ๋งจ ์•ž ์š”์†Œ๋กœ ์ดˆ๊ธฐํ™” ์‹œํ‚จ๋‹ค. ๊ทธ ํ›„ days ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ์ด prev ๊ฐ€ ๊ฐฑ์‹ ๋˜์–ด์•ผ ํ•œ๋‹ค.

days ๋ฐฐ์—ด์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋“ค์–ด ์žˆ์„๋•Œ

1
2
// days
[ 5, 10, 1, 1, 20, 1]

prev ๋ฅผ 5๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด ๊ทธ ๋‹ค์Œ ์ˆซ์ž๊ฐ€ ๋” ํฐ์ง€ ์ž‘์€์ง€ ๋น„๊ตํ•œ๋‹ค.

  • if (prev >= days.get(i)) : ์ด ๊ฒฝ์šฐ์—” ๋’ค์ชฝ ์ž‘์—…๋„ ๋ฐฐํฌ์— ํฌํ•จ์‹œ์ผœ์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— count + 1 ์„ ํ•ด์ค€๋‹ค. prev ์ž‘์—…๋„ count ๊ฐฏ์ˆ˜์— ๊ฐ™์ด ํฌํ•จ์‹œ์ผœ์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์ดˆ count ๋ณ€์ˆ˜๋Š” 1๋กœ ์ดˆ๊ธฐํ™”ํ–ˆ๋‹ค.

  • if (prev < days.get(i)) : prev ๋ณด๋‹ค ๊ธฐ๊ฐ„์ด ๋” ๊ธด ์ž‘์—…์„ ๋งŒ๋‚˜๋ฉด ํ˜„์žฌ๊นŒ์ง€ ๋ˆ„์ ๋œ count ๊ฐ’์„ ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค. ๊ทธ ํ›„ count ๊ฐ’์„ ๋‹ค์‹œ 1๋กœ ์ดˆ๊ธฐํ™” ํ•˜๊ณ , prev ๋ฅผ ํ˜„์žฌ ์ž‘์—…์œผ๋กœ ๊ฐฑ์‹ ํ•œ๋‹ค.

  • result.add(count) : ๋งจ ๋งˆ์ง€๋ง‰ ํ•ญ๋ชฉ์— ๋Œ€ํ•œ count ๊ฐ’๋„ result ์— ์ €์žฅํ•ด์•ผ ํ•œ๋‹ค.

๋ฐฉ๋ฌธํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋Œ“๊ธ€,์ง€์ ,ํ”ผ๋“œ๋ฐฑ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜Š

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ