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

[๋ฌธ์ œ]

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ณด์„ ์‡ผํ•‘

["DIA", "RUBY", "RUBY", "DIA", "DIA", "EMERALD", "SAPPHIRE", "DIA"] [3, 7]

programmers.co.kr

[ํ’€์ด]

ํˆฌํฌ์ธํ„ฐ ๋ฐฉ์‹์œผ๋กœ ๋ฐฐ์—ด์„ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ํ•ต์‹ฌ์ธ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

 

์šฐ์„ , ์ดˆ๊ธฐ์— ํ•„์š”ํ•œ ์ž๋ฃŒ๋ฅผ ์„ธํŒ…ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

  • Set<String> set์œผ๋กœ ๋ณด์„์˜ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค.
  • gems๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ Map<String, Integer> pick์— ๊ฐ ๋ณด์„์˜ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋ฉฐ ๋งจ์•ž์—์„œ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ณด์„์„ธํŠธ๋ฅผ ์ €์žฅํ•ด์„œ ์ดˆ๊ธฐ start์™€ end๋ฅผ ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๊ทธ๋‹ค์Œ, ํˆฌํฌ์ธํ„ฐ ๋ฐฉ์‹์œผ๋กœ left๋ฅผ right๋ฅผ ๊ฐฑ์‹ ํ•˜๋ฉฐ  gems๋ฐฐ์—ด์˜ ๋๊นŒ์ง€ ์ƒˆ๋กœ์šด ๋ณด์„์„ธํŠธ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

  • left = start, right = end๋ฅผ ์ €์žฅํ•˜๊ณ , right < gems.length ๊นŒ์ง€ ํƒ์ƒ‰
  • right++ํ†ตํ•ด ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ์นธ ํ™•์žฅํ•˜๊ณ , left๋ถ€ํ„ฐ pick์—์„œ ๋ณด์„๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•ด์™€, ๊ฒน์น˜๋Š” ๋ณด์„์ด ์žˆ๋‹ค๋ฉด ( ๋ณด์„์ด 2๊ฐœ ์ด์ƒ์ด๋ผ๋ฉด ) ๋ณด์„์„ ์ œ๊ฑฐํ•˜๋ฉฐ left++๋ฅผ ์ง„ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
    ์—ฌ๊ธฐ์„œ ํ•ต์‹ฌ์€, ๊ฐ ๋ณด์„์ด ์ตœ์†Œ 1๊ฐœ๋Š” ์žˆ์–ด์•ผ์ง€ ๋ณด์„์„ธํŠธ๊ฐ€ ์™„์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๋งŒ์•ฝ ํ˜„์žฌ left๊ฐ€ ๊ฐ€๋ฅดํ‚ค๋Š” ๋ณด์„์ด pick์— 1๊ฐœ๋งŒ ์กด์žฌํ•œ๋‹ค๋ฉด, left ๊ฐฑ์‹ ์€ ์ค‘๋‹จ๋ฉ๋‹ˆ๋‹ค.

[์ฝ”๋“œ]

import java.util.*;
class Solution {
    public static int N = 0; // ์ค‘๋ณต ์ œ๊ฑฐ๋œ ๋ณด์„์˜ ์ด ๊ฐœ์ˆ˜
    public static int start = 0;
    public static int end = 0;
    public int[] solution(String[] gems) {
        int[] answer = new int[2];
        
        // ๋ณด์„์˜ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
        Set<String> set = new HashSet<String>();
        for(int i=0;i<gems.length;i++) {
            set.add(gems[i]);
        }
        N = set.size();
    
        Map<String, Integer> pick = new HashMap<String,Integer>();
        for(int i=start;i<gems.length;i++) {
            if(pick.get(gems[i]) == null) {
                pick.put(gems[i], 1);
            } else {
                int cnt = pick.get(gems[i]) + 1;
                pick.put(gems[i], pick.get(gems[i]) + 1);
            }
            if(pick.size() == N) {
                end = i;
                break;
            }
        }
        for(int i=start;i<end;i++) {
            if(pick.get(gems[i]) == 1) {
                start = i;
                break;
            } else {
                int cnt = pick.get(gems[i]) - 1;
                pick.put(gems[i], cnt);
            }
        }
        
        int left = start;
        int right = end + 1;
        
        while(right < gems.length) {
            int cnt = pick.get(gems[right]) + 1;
            pick.put(gems[right], cnt);
            
            // ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ™•์žฅ
            for(int i=left;i<=right;i++) {
                cnt = pick.get(gems[i]);
                if(cnt == 1) {
                    left = i;
                    break;
                } else {
                    pick.put(gems[i], cnt - 1);
                }
            }
            if((right - left) < (end - start)) {
                start = left;
                end = right;
            }
            right++;
        }
        answer[0] = start + 1;
        answer[1] = end + 1;
        return answer;
    }
}