์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜4. HashMap, TreeSet (ํ•ด์‰ฌ, ์ •๋ ฌ์ง€์› Set) : K๋ฒˆ์งธ ํฐ ์ˆ˜ (TreeSet)

2023. 10. 25. 15:23ใ†์ธํ”„๋Ÿฐ/์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ์ž…๋ฌธ : ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋Œ€๋น„

728x90

 

https://hyejin.tistory.com/1230

 

์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜4. HashMap, TreeSet (ํ•ด์‰ฌ, ์ •๋ ฌ์ง€์› Set) : ๋ชจ๋“  ์•„๋‚˜๊ทธ๋žจ ์ฐพ๊ธฐ

https://hyejin.tistory.com/1229 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜4. HashMap, TreeSet (ํ•ด์‰ฌ, ์ •๋ ฌ์ง€์› Set) : ๋งค์ถœ์•ก์˜ ์ข…๋ฅ˜ https://hyejin.tistory.com/1228 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜4. HashMap, TreeSet (ํ•ด์‰ฌ

hyejin.tistory.com

-> ์ด์ „ ๋ฌธ์ œ ํ’€์ด 

 

 

 

5. K๋ฒˆ์งธ ํฐ ์ˆ˜ 

์„ค๋ช…

ํ˜„์ˆ˜๋Š” 1๋ถ€ํ„ฐ 100์‚ฌ์ด์˜ ์ž์—ฐ์ˆ˜๊ฐ€ ์ ํžŒ N์žฅ์˜ ์นด๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ™์€ ์ˆซ์ž์˜ ์นด๋“œ๊ฐ€ ์—ฌ๋Ÿฌ์žฅ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ˜„์ˆ˜๋Š” ์ด ์ค‘ 3์žฅ์„ ๋ฝ‘์•„ ๊ฐ ์นด๋“œ์— ์ ํžŒ ์ˆ˜๋ฅผ ํ•ฉํ•œ ๊ฐ’์„ ๊ธฐ๋กํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. 3์žฅ์„ ๋ฝ‘์„ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.

๊ธฐ๋กํ•œ ๊ฐ’ ์ค‘ K๋ฒˆ์งธ๋กœ ํฐ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๋งŒ์•ฝ ํฐ ์ˆ˜๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด์ง„ ์ˆ˜๊ฐ€ 25 25 23 23 22 20 19......์ด๊ณ  K๊ฐ’์ด 3์ด๋ผ๋ฉด K๋ฒˆ์งธ ํฐ ๊ฐ’์€ 22์ž…๋‹ˆ๋‹ค.

 

์ž…๋ ฅ

์ฒซ ์ค„์— ์ž์—ฐ์ˆ˜ N(3<=N<=100)๊ณผ K(1<=K<=50) ์ž…๋ ฅ๋˜๊ณ , ๊ทธ ๋‹ค์Œ ์ค„์— N๊ฐœ์˜ ์นด๋“œ๊ฐ’์ด ์ž…๋ ฅ๋œ๋‹ค.

 

์ถœ๋ ฅ

์ฒซ ์ค„์— K๋ฒˆ์งธ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. K๋ฒˆ์งธ ์ˆ˜๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด -1๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ ์ž…๋ ฅ 1 

10 3
13 15 34 23 45 65 33 11 26 42

 

์˜ˆ์‹œ ์ถœ๋ ฅ 1

143

 

๋ฌธ์ œ ํ’€์ด  1

public int solution(int n, int k, int[] arr) {
    int answer;
    Set<Integer> set = new TreeSet<>();

    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            for (int x = j + 1; x < n; x++) {
                set.add(arr[i] + arr[j] + arr[x]);
            }
        }
    }
    if (set.size() < k) {
        return -1;
    }

    answer =  set.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList()).get(k - 1);

    return answer;
}

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป : ์ด ๋ฌธ์ œ๋Š” n ๊ฐœ์˜ ์นด๋“œ๊ฐ€ ์ฃผ์–ด์ง€๋Š”๋ฐ ์ด ์นด๋“œ 3๊ฐœ๋ฅผ ๋ฝ‘์•„์„œ ๋”ํ•˜๋Š”๋ฐ ์ด๋•Œ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฐ’๋“ค์„ ๋ชจ๋‘ ๊ตฌํ•œ ๋‹ค์Œ 

K ๋ฒˆ์งธ ํฐ ์ˆ˜์˜ ๊ฐ’์„ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค. (์ด๋•Œ ์ค‘๋ณต๋œ ๊ฐ’์€ ์ œ์™ธํ•˜๊ณ  K๋ฒˆ์งธ ์ˆ˜์ด๋ฏ€๋กœ ์ค‘๋ณต ์ œ๊ฑฐ๋ฅผ ์ด์šฉํ•ด์•ผ ํ•œ๋‹ค.)

 

์ค‘๋ณต ์ œ๊ฑฐ๋ฅผ ํ•  ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ Set ์ด๋‹ค. Set์€ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๊ณ , ์ˆœ์„œ ์œ ์ง€๋ฅผ ํ•˜์ง€ ์•Š๋Š”๋ฐ 

์—ฌ๊ธฐ์„œ ํฐ ์ˆ˜๋กœ ์ •๋ ฌํ•ด์„œ ๋”ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด TreeSet ์„ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค. 

 

์‚ผ์ค‘ for ๋ฌธ์„ ์ด์šฉํ•ด์„œ arr[i] + arr[j] + arr[x] ์˜ ํ•ฉ์„ set์— add ํ•ด์ฃผ๊ณ , 

k ๊ฐ€ set์˜ ํฌ๊ธฐ๋ณด๋‹ค ํฌ๋ฉด ์—†๋‹ค๋Š” ๊ฒƒ์ด๋ฏ€๋กœ -1 ๋ฅผ ๋ฆฌํ„ดํ•ด์ค€๋‹ค. 

 

TreeSet ์— add ํ•œ ๊ฐ’์ด ์˜ค๋ฆ„์ฐจ์ˆœ์ด๋‹ˆ๊นŒ ๋‚ด๋ฆผ์ฐจ์ˆœ์„ ๋งŒ๋“ค์–ด์ค˜์•ผํ•˜๋Š”๋ฐ... ๋‚˜๋Š” new TreeSet<> (Collections.reverseOrder()); ์„ ๋ชฐ๋ผ์„œ.. ^^ ์ƒ๊ฐ๋‚ฌ๋˜ stream.sorted ์‚ฌ์šฉ ํ›„, Comparator.reverseOrder()๋ฅผ ์‚ฌ์šฉํ•ด์คฌ๋‹ค.. 

๊ทธ ๋‹ค์Œ Stream์ด๋ฏ€๋กœ List ํ˜•ํƒœ๋กœ ๋ฐ”๊พผ๋‹ค์Œ get(k-1) ์„ ํ•ด์„œ answer ๊ฐ’์„ ๊ตฌํ–ˆ๋‹ค. (k-1) ๋ฒˆ์งธ๋ถ€ํ„ฐ ํ•œ ์ด์œ ๋Š” 0 ๋ถ€ํ„ฐ get ํ•˜๊ธฐ ๋•Œ๋ฌธ !~ 

-> ์ด ๋ถ€๋ถ„์€ ์•ฝ๊ฐ„์˜ ๋ป˜์ง“์ด ๋“ค์–ด๊ฐ„ ๋ถ€๋ถ„์ด๋‹ค... 

 

 

๋ฌธ์ œ ํ’€์ด  2

public int solution2(int[] arr, int n, int k) {
    int answer = -1;
    TreeSet<Integer> treeSet = new TreeSet<>(Collections.reverseOrder());

    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            for (int l = j + 1; l < n; l++) {
                treeSet.add(arr[i] + arr[j] + arr[l]);
            }
        }
    }

    int cnt = 0;
    for (Integer integer : treeSet) {
        cnt++;
        if (cnt == k) return integer;
    }

    return answer;
}

๐Ÿ‘พ : ์‚ผ์ค‘ for๋ฌธ์œผ๋กœ treeSet์— ์„ธ๊ฐœ์˜ ํ•ฉ์„ ๊ตฌํ•ด์„œ addํ•˜๋Š” ๊ฑด ๋˜‘๊ฐ™์€๋ฐ TreeSet์„ ์ƒ์„ฑํ•  ๋•Œ Collections.reverseOrder()๋ฅผ ์ด์šฉํ•˜๋ฉด ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์„œ ์ค‘๋ณต ์ œ๊ฑฐํ•ด์„œ ์ €์žฅํ•ด์ค€๋‹ค..! 

 

๊ทธ ๋‹ค์Œ cnt ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ์„ ์–ธํ•ด์„œ cnt +1  ํ•˜๋ฉด์„œ treeSet์„ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๊ฐ’์„ ๊ตฌํ•˜๋Š”๋ฐ cnt ๊ฐ’์ด k ๊ฐ’์ด ๋˜๋ฉด ๊ทธ ๊ฐ’์„ return ํ•ด์ฃผ๋„๋ก ํ–ˆ๋‹ค. 

๋งŒ์•ฝ ์ด ๋ฐ˜๋ณต๋ฌธ์„ ๋„˜์–ด์„œ๋„ k ๊ฐ’์„ ๊ตฌํ•˜์ง€ ๋ชปํ•œ๋‹ค๋ฉด ๊ธฐ๋ณธ answer ๋ฅผ -1๋กœ ์ดˆ๊ธฐํ™”ํ•ด์คฌ๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋Œ€๋กœ ๋ฆฌํ„ดํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

 

 

 

 

<TreeSet์˜ ๋ฉ”์„œ๋“œ>

public void solution3(int[] arr, int n, int k) {
    TreeSet<Integer> treeSet = new TreeSet<>(Collections.reverseOrder());

    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            for (int l = j + 1; l < n; l++) {
                treeSet.add(arr[i] + arr[j] + arr[l]);
            }
        }
    }

    System.out.println("treeSet.size() = " + treeSet.size());
    treeSet.remove(143);
    System.out.println("treeSet.size() = " + treeSet.size());
    System.out.println("treeSet = " + treeSet);
    System.out.println("treeSet.first() = " + treeSet.first()); // ์ œ์ผ ์•ž์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅ
    System.out.println("treeSet.last() = " + treeSet.last()); // ์ œ์ผ ๋งˆ์ง€๋ง‰์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅ
}

treeSet.size() : TreeSet์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฉ”์„œ๋“œ 

treeSet.remove() : ์ฃผ์–ด์ง„ ๊ฐ’์„ TreeSet์—์„œ ์ œ๊ฑฐํ•˜๋Š” ๋ฉ”์„œ๋“œ 

treeSet.first() : treeSet์˜ ์ œ์ผ ์•ž์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅ 

treeSet.last() : treeSet์˜ ์ œ์ผ ๋งˆ์ง€๋ง‰ ์ˆ˜๋ฅผ ์ถœ๋ ฅ 

 

๊ฒฐ๊ณผ 

treeSet.size() = 72
treeSet.size() = 71
treeSet = [152, 144, 141, 140, 136, 133, 132, 130, 125, 124, 123, 122, 121, 120, 118, 114, 113, 112, 111, 110, 109, 106, 105, 104, 103, 102, 101, 100, 99, 98, 94, 93, 92, 91, 90, 89, 88, 87, 86, 84, 83, 82, 81, 80, 79, 78, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 64, 62, 61, 60, 59, 58, 57, 54, 52, 51, 50, 49, 47, 39]
treeSet.first() = 152
treeSet.last() = 39

 

 

 

 

 

728x90