๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/์ž๋ฃŒ๊ตฌ์กฐ & ์•Œ๊ณ ๋ฆฌ์ฆ˜

์ฒด์œก๋ณต (Java)

by 1mj 2022. 2. 8.

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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ฒด์œก๋ณต

์ ์‹ฌ์‹œ๊ฐ„์— ๋„๋‘‘์ด ๋“ค์–ด, ์ผ๋ถ€ ํ•™์ƒ์ด ์ฒด์œก๋ณต์„ ๋„๋‚œ๋‹นํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹คํ–‰ํžˆ ์—ฌ๋ฒŒ ์ฒด์œก๋ณต์ด ์žˆ๋Š” ํ•™์ƒ์ด ์ด๋“ค์—๊ฒŒ ์ฒด์œก๋ณต์„ ๋นŒ๋ ค์ฃผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ํ•™์ƒ๋“ค์˜ ๋ฒˆํ˜ธ๋Š” ์ฒด๊ฒฉ ์ˆœ์œผ๋กœ ๋งค๊ฒจ์ ธ ์žˆ์–ด, ๋ฐ”๋กœ ์•ž๋ฒˆ

programmers.co.kr

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๊ณ ๋“์  Kit ์ฒด์œก๋ณต ๋ฌธ์ œ ํ’€์ด์ด๋‹ค.

 

reserve ๋ชฉ๋ก ๋‚ด lost ๋ชฉ๋ก์„ ๋„ฃ์–ด ์ค‘์ฒฉ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋น„๊ตํ•ด์„œ ํ’€์—ˆ๋‹ค.

์ดˆ๋ฐ˜์—” ๋น„๊ต์  ์‰ฝ๊ฒŒ ํ’€๋ฆฌ๋Š” ๋“ฏ ํ–ˆ๋Š”๋ฐ ์ผ๋ถ€ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ์‹คํŒจํ–ˆ๊ณ  ์‹คํŒจ ์›์ธ์€ ์•„๋ž˜์™€ ๊ฐ™์•˜๋‹ค.

 

- 12, 13, 18 ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์‹คํŒจ

reserve ๋ชฉ๋ก๊ณผ lost ๋ชฉ๋ก์€ ์ •๋ ฌ๋˜์–ด์žˆ์ง€ ์•Š์„ ์ˆ˜ ์žˆ์–ด ๋„๋‚œ ๋‹นํ•œ ์‚ฌ๋žŒ์˜ ์•ž, ๋’ค ๋ชจ๋‘ ์—ฌ๋ฒŒ์˜ท์ด ์žˆ์„ ๋•Œ ๋’ท ์‚ฌ๋žŒ์—๊ฒŒ ๋จผ์ € ๋นŒ๋ ค์ฃผ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ฒจ ์ตœ์ ์˜ ๋ฐฉ๋ฒ•์„ ์ฐพ์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธด๋‹ค. ๊ฐ ๋ชฉ๋ก์€ ์ •๋ ฌํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

- 5, 12 ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์‹คํŒจ

๋„๋‚œ ๋‹นํ•œ ์‚ฌ๋žŒ์ด ์—ฌ๋ฒŒ ์˜ท์ด ์žˆ์„ ๋•Œ๋Š” ๋‚จ์€ ์ฒด์œก๋ณต์ด ํ•˜๋‚˜๋กœ ๋ณธ์ธ์ด ์ž…์–ด์•ผ ํ•ด์„œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์—๊ฒŒ ๋นŒ๋ ค์ฃผ์ง€ ๋ชปํ•œ๋‹ค. ๋‹จ์ˆœํžˆ ์ž…์„ ์ˆ˜ ์žˆ๋Š” ์‚ฌ๋žŒ์˜ ์ˆ˜๋งŒ ๋Š˜๋ฆฌ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ๋ณธ์ธ์ด ์ž…์–ด์•ผ ํ•˜๋Š”๋ฐ ๋‚จ์—๊ฒŒ ๋จผ์ € ๋นŒ๋ ค์ฃผ๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธฐ์ง€ ์•Š๋„๋ก ์ฒ˜๋ฆฌํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

n: 3 / lost: [1, 2] / reserve: [2, 3] ์˜ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด๋ณด๋ฉด ์ •๋‹ต์€ 2๊ณ , 1์—๊ฒŒ 2๊ฐ€ ๋นŒ๋ ค์ฃผ๊ฒŒ ๋˜๋ฉด ์•ˆ ๋˜๊ธฐ ๋•Œ๋ฌธ์— removeAll()๋กœ ๋‘ ๋ชฉ๋ก์„ ๋น„๊ตํ•ด ์ด๋Ÿฐ ๊ฒฝ์šฐ๋Š” ๋ฆฌ์ŠคํŠธ์—์„œ ๋ฏธ๋ฆฌ ์ง€์›Œ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

 

import java.util.stream.Collectors;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

class Solution {
    public int solution(int n, int[] lost, int[] reserve) {
        // int[] to List<Integer>
        List<Integer> lostList = Arrays.stream(lost).boxed().collect(Collectors.toList());
        List<Integer> reserveList = Arrays.stream(reserve).boxed().collect(Collectors.toList());
        
        // ๋„๋‚œ ๋‹นํ•œ ์‚ฌ๋žŒ์ด ์—ฌ๋ฒŒ ์˜ท์ด ์žˆ์„ ๋•Œ๋Š” ๋ฌด์กฐ๊ฑด ๋ณธ์ธ์ด ๊ฐ€์ ธ์•ผ ํ•จ(๋‚จ์—๊ฒŒ ๋นŒ๋ ค์ค„ ์ˆ˜ ์—†์Œ)
        // lost๊ฐ€ [1, 2]์ผ ๋•Œ reserve๊ฐ€ [2, 3]์ด๋ฉด 2๋ฒˆ์ด 1๋ฒˆ์—๊ฒŒ ๋จผ์ € ์˜ท์„ ๋นŒ๋ ค์ฃผ๊ฒŒ ๋˜๋ฏ€๋กœ ๋‘ ๋ฆฌ์ŠคํŠธ ๊ฐ„ ์ค‘๋ณต ์ œ๊ฑฐ
        List<Integer> targetLostList = new ArrayList<>(lostList);
        List<Integer> targetReserveList = new ArrayList<>(reserveList);
        targetLostList.removeAll(reserveList);
        targetReserveList.removeAll(lostList);
        
        // ์ „์ฒด ์ธ์› ์ˆ˜์—์„œ ๋„๋‚œ ๋‹นํ•œ ์ˆ˜ ์ œ์™ธ, ๋„๋‚œ ๋‹นํ•œ ์‚ฌ๋žŒ์ด ์—ฌ๋ฒŒ ์˜ท์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ œ์™ธ
        int answer = n - lost.length + (lostList.size() - targetLostList.size());

        // ๋ชฉ๋ก์ด ์ •๋ ฌ๋˜์–ด ์žˆ์–ด์•ผ ์ˆœ์ฐจ์ ์œผ๋กœ ๋น„๊ต ๊ฐ€๋Šฅ
        Collections.sort(targetLostList);
        Collections.sort(targetReserveList);
        
        // ์—ฌ๋ฒŒ ์˜ท ์žˆ๋Š” ์‚ฌ๋žŒ ์ค‘ ๋นŒ๋ ค์ค„ ์ˆ˜ ์žˆ๋Š” ์‚ฌ๋žŒ ์ฐพ๊ธฐ
        for (int i = 0; i < targetReserveList.size(); i++) {
            int prev = targetReserveList.get(i) - 1;    // ์ด์ „ ๋ฒˆํ˜ธ
            int next = targetReserveList.get(i) + 1;    // ์ดํ›„ ๋ฒˆํ˜ธ
            for (int j = 0; j < targetLostList.size(); j++) {
                int temp = targetLostList.get(j);
                if (temp == prev || temp == next) {
                    targetLostList.remove(j);   // ์ด๋ฏธ ๋นŒ๋ ค์ค€ ๊ฒฝ์šฐ ๋ชฉ๋ก์—์„œ ์ œ์™ธ
                    answer++;
                    break;
                } 
            }
        }
        return answer;
    }
}

๋Œ“๊ธ€