Publish:

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

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

img_3.png

๋ฌธ์ œ

๋ฌธ์ œ ๋งํฌ

img_3.png

์„ค๋ช…

์—˜๋ฆฌ๋ฒ ์ดํ„ฐ๋ฅผ ํƒ€๊ณ  0์ธต ์œผ๋กœ ๋‚ด๋ ค๊ฐ€๋Š”๋ฐ ๋ˆ„๋ฅผ ์ˆ˜ ์žˆ๋Š” ๋ฒ„ํŠผ์€ 10^c ๋กœ๋งŒ ์ฃผ์–ด์ง„๋‹ค. 0์ธต์„ ๊ฐ€๋Š”๋ฐ ํ•„์š”ํ•œ ์ตœ์†Œ ๋ฒ„ํŠผ ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ. ์˜ˆ๋ฅผ ๋“ค์–ด ํ˜„์žฌ ์ธต์ด 16 ์ธต์ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ๋“ค์ด ์žˆ๋‹ค.

1
2
3
4
5
6
// 16์ธต์—์„œ 0์ธต์„ ๊ฐ€๋Š” ๋ฐฉ๋ฒ•
// 1. 
-10, -10, +1, +1, +1, +1    ==> ๋ฒ„ํŠผ ์—ฌ์„ฏ๋ฒˆ ๋ˆ„๋ฅด๋Š” ๊ฒฝ์šฐ

// 2. 
-10, -1, -1, -1, -1, -1, -1    ==> ๋ฒ„ํŠผ ์ผ๊ณฑ๋ฒˆ ๋ˆ„๋ฅด๋Š” ๊ฒฝ์šฐ

๋ฒ„ํŠผ์„ ์ตœ์†Œ๋กœ ๋ˆ„๋ฅด๋Š” ํšŸ์ˆ˜๋Š” ์—ฌ์„ฏ๋ฒˆ ์ด๋‹ค.

์‹œ๋„ํ•œ ๋ฐฉ๋ฒ•

์ผ๋‹จ +1 ์ด๋‚˜ -1 ์„ ๋ˆ„๋ฅด๋Š” ํšŸ์ˆ˜๋ฅผ ์ตœ์†Œํ™” ํ•ด์•ผ ํ•œ๋‹ค. ์œ„ ์˜ˆ์‹œ์˜ 16 ๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ์—” -1 ์„ ์—ฌ์„ฏ๋ฒˆ ๋ˆ„๋ฃจ๋Š” ๊ฒƒ๋ณด๋‹ค -10 ์„ ํ•œ๋ฒˆ ๋ˆ„๋ฅธ ํ›„ +1 ์„ ๋ˆ„๋ฅด๋Š” ํšŸ์ˆ˜๊ฐ€ ๋” ์ ๋‹ค๋Š” ๊ฒƒ์— ์ฐฉ์•ˆํ•˜๋ฉด,

  • 1์˜ ์ž๋ฆฌ ์ˆซ์ž๊ฐ€ 5๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ ์˜ฌ๋ฆผ์„ ํ•ด์„œ 20์œผ๋กœ ๋งŒ๋“ค๊ณ  ์ถœ๋ฐœํ•˜๋Š” ๊ฒƒ์ด ๋” ์ด๋“์ด๋‹ค.
  • 1์˜ ์ž๋ฆฌ ์ˆซ์ž๊ฐ€ 5๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ ๊ทธ ์ˆซ์ž๋งŒํผ -1 ํ˜น์€ +1 ์„ ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

์˜ฌ๋ฆผ์— ํ•„์š”ํ•œ ์ˆ˜๋ฅผ ์นด์šดํŠธํ•ด answer ๋ณ€์ˆ˜์— ๋‹ด๊ณ , storey ๋„ ๊ทธ๋งŒํผ ๋Š˜๋ ค์ค€๋‹ค. ๊ทธ ํ›„ 1์˜ ์ž๋ฆฌ๋ฅผ ์ œ์™ธํ•œ ์ˆซ์ž๋ถ€ํ„ฐ ๋‹ค์‹œ ๋ฐ˜๋ณตํ•œ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
    public int solution(int storey) {
        int answer = 0;
        while (storey > 0) {
          int lastNum = storey % 10;
          if (lastNum > 5) {
            answer += (10 - lastNum);
            storey += (10 - lastNum);
          } else {
            answer += lastNum;
          }
          storey /= 10;
        }
        return answer;
    }
}

img_3.png

์ด๋Œ€๋กœ ํ’€๋ฆด ์ค„ ์•Œ์•˜๋Š”๋ฐ, ํ†ต๊ณผ๋ฅผ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์–ด์„œ ๋‹ค์‹œ ์ƒ๊ฐํ•ด๋ดค๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด 54 ์ธ ๊ฒฝ์šฐ ๋จผ์ € 60์„ ๋งŒ๋“ค๊ณ  ์ถœ๋ฐœํ•  ์ง€, 50์„ ๋งŒ๋“ค๊ณ  ์ถœ๋ฐœํ•  ์ง€ ์ƒ๊ฐํ•ด๋ณด๋ฉด -1 ๋˜๋Š” +1 ์„ ์ตœ์†Œ๋กœ ๋ˆ„๋ฅด๋ ค๋ฉด 54์—์„œ ๊ฐ€๊นŒ์šด 50 ์œผ๋กœ ๊ฐ€๋Š”๊ฒƒ์ด ๋” ์œ ๋ฆฌํ•˜๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ 65 ์˜ ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ๋‹ค. img_3.png 65๋Š” ์˜ฌ๋ฆผํ•˜๋Š”๊ฒŒ ์œ ๋ฆฌํ•˜๋‹ค

์ฆ‰, ๋งˆ์ง€๋ง‰ ์ˆซ์ž๊ฐ€ 5์ธ ๊ฒฝ์šฐ ์˜ฌ๋ฆผ์ด ์œ ๋ฆฌํ•œ์ง€ ๋‚ด๋ฆผ์ด ์œ ๋ฆฌํ•œ์ง€๋Š” ๊ทธ ๋‹ค์Œ ์ˆซ์ž๊นŒ์ง€ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค.

ํ’€์ด (๊ทธ๋ฆฌ๋””)

๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ๊ฐ ์ž๋ฆฌ์ˆ˜๋งˆ๋‹ค 0์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์ตœ์ ์˜ ์„ ํƒ์„ ํ•ด์•ผ ํ•œ๋‹ค.

1
2
[ 0, 1, 2, 3, 4, 5 ]   --> ๊ทธ๋ƒฅ -1 ์”ฉ ๋นผ๋Š” ๊ฒŒ ์œ ๋ฆฌ
[ 6, 7, 8, 9 ]         --> +1 ์„ ๋ˆŒ๋Ÿฌ 10์„ ๋งŒ๋“ค๊ณ  -10 ์„ ํ•˜๋Š”๊ฒŒ ์œ ๋ฆฌ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
    public int solution(int storey) {
        int answer = 0;
        while (storey > 0) {
          int lastNum = storey % 10;
          if (lastNum > 5 || (lastNum == 5 && (storey / 10) % 10 >= 5)) {
            answer += (10 - lastNum);
            storey += (10 - lastNum);
          } else {
            answer += lastNum;
          }
          storey /= 10;
        }
        return answer;
    }
}

๋งˆ์ง€๋ง‰ ์ˆซ์ž๊ฐ€ 5์ธ ๊ฒฝ์šฐ์—” ๋‹ค์Œ ์ˆซ์ž๋„ ํ™•์ธํ•œ๋‹ค. ๋‹ค์Œ ์ˆซ์ž๋„ 5 ์ด์ƒ์ธ ๊ฒฝ์šฐ์—” ์˜ฌ๋ฆผ์ด ์œ ๋ฆฌํ•˜๋‹ค.

ํ’€์ด (๊ทธ๋ฆฌ๋”” + DP)

์˜ฌ๋ฆผ, ๋‚ด๋ฆผ์˜ ๊ฒฝ์šฐ๋ฅผ ์ ๋‹ค๋ณด๋‹ˆ ์ ํ™”์‹์ด ๋ฐœ๊ฒฌ๋œ๋‹ค. ์˜ฌ๋ฆผ๊ณผ ๋‚ด๋ฆผ ๋‘˜ ์ค‘ ์ตœ์†Œ๊ฐ’์„ ์„ ํƒํ•˜๋ฉด ๋˜๋Š”๋ฐ, ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ํŒ๋‹จํ•˜๋Š” ๋ฐฉ์‹์ด ์žฌ๊ท€์ ์œผ๋กœ ์ˆ˜ํ–‰๋œ๋‹ค. dp ๋‚˜ dfs ๋กœ๋„ ํ’€์ด๊ฐ€ ๊ฐ€๋Šฅํ•  ๊ฒƒ ๊ฐ™๋‹ค.

1
f(65) = Math.min(5 + f(7), 5 + f(6)) 

์ ํ™”์‹์„ ๊ทธ๋Œ€๋กœ ์ฝ”๋“œ๋กœ ์˜ฎ๊ธฐ๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค. 10 ์ดํ•˜์˜ ์ˆ˜์— ๋Œ€ํ•ด์„  ์žฌ๊ท€ํ•จ์ˆ˜๋กœ ์ฒ˜๋ฆฌํ•  ํ•„์š” ์—†์ด ๋ฐ”๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

top-down

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
  private final Map<Integer, Integer> memo = new HashMap<>();
  
  public int solution(int storey) {
    int answer;
    if (storey <= 5) {
      return storey;
    } else if (storey < 10) {
      return 11 - storey;
    }

    if (memo.containsKey(storey)) {
      return memo.get(storey);
    }
    
    int lastNum = storey % 10;
    int down = solution(storey / 10) + lastNum;
    int up = solution(storey / 10 + 1) + (10 - lastNum);
    
    answer = Math.min(down, up);
    memo.put(storey, answer);
    return answer;
  }
}

bottom-up

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
  public int solution(int storey) {

    int[] dp = new int[storey + 1];

    dp[1] = 1;
    dp[2] = 2;
    dp[3] = 3;
    dp[4] = 4;
    dp[5] = 5;
    dp[6] = 5;
    dp[7] = 4;
    dp[8] = 3;
    dp[9] = 2;
    for (int i = 10; i <= storey; i++) {
      int lastNum = i % 10;
      dp[i] = Math.min(dp[i / 10 + 1] + (10 - lastNum), dp[i / 10] + lastNum);
    }
    return dp[storey];
  }
}

ํšŒ๊ณ 

๊ทธ๋ฆฌ๋””๋Š” ๊ทธ ์ž์ฒด๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ผ๊ณ  ํ•˜๊ธฐ ๋ณด๋‹ค๋Š” ๋ฌธ์ œ ํ’€์ด์˜ ํ•ต์‹ฌ ์•„์ด๋””์–ด์ด๊ณ , ๊ทธ๊ฑธ ์ด์šฉํ•ด ํ’€์ดํ•˜๋Š” ๋ฐฉ์‹์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€๋กœ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์„ ๋Š๊ผˆ๋‹ค.

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

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