ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

[๋ฌธ์ œ]

https://school.programmers.co.kr/learn/courses/30/lessons/64063

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

[ํ’€์ด]

ํ•ด๋‹น ๋ฌธ์ œ๋Š” ๋ฐฉ์„ ๋ฐฐ์ •ํ•˜๋ฉฐ, ํ•ด๋‹น ๋ฐฉ ๋‹ค์Œ์œผ๋กœ ์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋นˆ๋ฐฉ์„ ์—ฐ๊ฒฐํ•ด ๊ณ„์† ๊ฐฑ์‹ ํ•ด์ฃผ๋ฉฐ ํ’€์ดํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

 

Map<Long, Long>์„ ์‚ฌ์šฉํ•˜์˜€๋Š”๋ฐ, ํ•ด๋‹น Map์€ <๋ฐฉ ๋ฒˆํ˜ธ x, x ๋Œ€์‹  ๋ฐฐ์ •๋  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ ๋ฒˆํ˜ธ> ํ˜•์‹์œผ๋กœ ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด๋ฉด, ํ’€์ด ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

room_number : [1,3,4,1,3,1] 

  1. room_number๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉฐ ๋ฐฉ์„ ๋ฐฐ์ •ํ•ด answer[]์„ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.
  2. ๋งŒ์•ฝ, ํ•ด๋‹น ๋ฐฉ ๋ฒˆํ˜ธ๋ฅผ ํ‚ค๋กœํ•˜๋Š” ๊ฐ’ x์ด map์— ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์•„์ง ๋ฐฐ์ •๋˜์ง€ ์•Š์€๋ฐฉ์ด๋ฏ€๋กœ x๋ฅผ ๋ฐฐ์ •ํ•˜๊ณ , map์— <x, x+1> ๊ฐ’์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ( ์ดํ›„ x๊ฐ€ ๋“ค์–ด์™”์„๋•Œ x+1์„ ๋ฐฐ์ •ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ)
  3. ๋งŒ์•ฝ, ํ•ด๋‹น ๋ฐฉ ๋ฒˆํ˜ธ๋ฅผ ํ‚ค๋กœ ํ•˜๋Š” ๊ฐ’ x๊ฐ€ map์— ์กด์žฌํ•œ๋‹ค๋ฉด, ์ด๋ฏธ x๋Š” ๋ฐฐ์ •๋์œผ๋ฏ€๋กœ while๋ฌธ์„ ํ†ตํ•ด map์—์„œ ์•„์ง ๋ฐฐ์ •๋˜์ง€ ์•Š์€ ๋ฐฉ์„ ์ฐพ์•„์„œ ํ•ด๋‹น ๋ฐฉ๋ฒˆํ˜ธ y๋ฅผ ๋ฐฐ์ •ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, map์„ ํ†ตํ•ด ๊ฑฐ์ณ์˜จ ๋ฐฉ๋ฒˆํ˜ธ๋“ค์€ ๋ชจ๋‘ y+1๋กœ ๊ฐฑ์‹ ํ•ด์ค๋‹ˆ๋‹ค.

์œ„์™€ ๊ฐ™์€ ๊ณผ์ •์„ ๊ฑฐ์น˜๊ณ  ๋‚˜๋ฉด map์˜ ๋ณ€ํ™”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

{1=2}
{1=2, 3=4}
{1=2, 3=4, 4=5}
{1=3, 2=3, 3=4, 4=5}
{1=3, 2=3, 3=6, 4=6, 5=6}
{1=7, 2=3, 3=7, 4=6, 5=6, 6=7}
  • 1๋ฒˆ ๋ฐฉ ๋ฐฐ์ • ์š”๊ตฌ -> map.get(1)์€ null์ด๋ฏ€๋กœ 1๋ฒˆ ๋ฐฐ์ •ํ›„ map.get(1) = 2๋กœ ๊ฐฑ์‹ 
  • 3๋ฒˆ ๋ฐฉ ๋ฐฐ์ • ์š”๊ตฌ -> map.get(3)์€ null์ด๋ฏ€๋กœ 3๋ฒˆ ๋ฐฐ์ •ํ›„ map.get(3) = 4๋กœ ๊ฐฑ์‹ 
  • 4๋ฒˆ ๋ฐฉ ๋ฐฐ์ • ์š”๊ตฌ -> map.get(4)์€ null์ด๋ฏ€๋กœ 4๋ฒˆ ๋ฐฐ์ •ํ›„ map.get(4) = 5๋กœ ๊ฐฑ์‹ 
  • 1๋ฒˆ ๋ฐฉ ๋ฐฐ์ • ์š”๊ตฌ -> ์ด๋ฏธ ๋ฐฐ์ •๋œ ๋ฐฉ์ด๋ฏ€๋กœ, map์„ ๋”ฐ๋ผ๊ฐ€๋ฉด 1->2->null ์ด๋ฏ€๋กœ 2๋ฒˆ๋ฐฉ ๋ฐฐ์ •ํ›„
    map.get(1) = 3, map.get(2) = 3์œผ๋กœ ๊ฐ๊ฐ ๊ฐฑ์‹ 
  • 3๋ฒˆ ๋ฐฉ ๋ฐฐ์ • ์š”๊ตฌ -> ์ด๋ฏธ ๋ฐฐ์ •๋œ ๋ฐฉ์ด๋ฏ€๋กœ, map์„ ๋”ฐ๋ผ๊ฐ€๋ฉด 3->4->5-> null ์ด๋ฏ€๋กœ 5๋ฒˆ๋ฐฉ ๋ฐฐ์ •ํ›„
    map.get(3) = 6, map.get(4) = 6, map.get(5) = 6์œผ๋กœ ๊ฐ๊ฐ ๊ฐฑ์‹ 
  • 1๋ฒˆ ๋ฐฉ ๋ฐฐ์ • ์š”๊ตฌ -> ์ด๋ฏธ ๋ฐฐ์ •๋œ ๋ฐฉ์ด๋ฏ€๋กœ, map์„ ๋”ฐ๋ผ๊ฐ€๋ฉด 1->3->6->null ์ด๋ฏ€๋กœ 6๋ฒˆ๋ฐฉ ๋ฐฐ์ •ํ›„
    map.get(1) = 7, map.get(3) = 7, map.get(6) = 7์œผ๋กœ ๊ฐ๊ฐ ๊ฐฑ์‹ 

๋”ฐ๋ผ์„œ answer = [1,3,4,2,5,6]์ด ๋ฉ๋‹ˆ๋‹ค.

[์ฝ”๋“œ]

import java.util.*;
class Solution {
    public static Map<Long, Long> map = new HashMap<Long,Long>();
    public long[] solution(long k, long[] room_number) {
        long[] answer = new long[room_number.length];
        for(int i=0;i<room_number.length;i++) {
            long num = room_number[i];
            if(map.get(num) == null) {
                answer[i] = num;
                map.put(num, num + 1);
            } else {
                ArrayList<Long> list = new ArrayList<Long>();
                while(map.get(num) != null) {
                    list.add(num);
                    num = map.get(num);
                }
                answer[i] = num;
                map.put(num, num + 1);
                for(Long l:list) {
                    map.put(l,num + 1);
                }
            }
            System.out.println(map);
        }
        return answer;
    }
}