Algorithm

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค | ๋‘ ๊ฐœ ๋ฝ‘์•„์„œ ๋”ํ•˜๊ธฐ, ์™„์ „ ํƒ์ƒ‰, HashSet

osean 2020. 12. 11. 02:42

๋“ค์–ด๊ฐ€๊ธฐ ์ „์—

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๊ธฐ์กด์˜ ์•„์ด๋””๋ฅผ ํƒˆํ‡ดํ•˜๊ณ  ๋‹ค์‹œ ์‹œ์ž‘ํ•˜๋Š” ๋งˆ์Œ์œผ๋กœ ์ƒˆ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ ˆ๋ฒจ 1๋ถ€ํ„ฐ ์ฐจ๊ทผํžˆ ํ’€์–ด๋ณด๊ณ  ๊ธฐ๋ก์„ ๋‚จ๊ธฐ๋ ค๊ณ  ํ•œ๋‹ค!

๋ฌธ์ œ

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋‘ ๊ฐœ ๋ฝ‘์•„์„œ ๋”ํ•˜๊ธฐ

์ •์ˆ˜ ๋ฐฐ์—ด numbers๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. numbers์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ์ธ๋ฑ์Šค์— ์žˆ๋Š” ๋‘ ๊ฐœ์˜ ์ˆ˜๋ฅผ ๋ฝ‘์•„ ๋”ํ•ด์„œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์ˆ˜๋ฅผ ๋ฐฐ์—ด์— ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”. ์ œํ•œ

programmers.co.kr

๋ฌธ์ œ ์„ค๋ช…

์ •์ˆ˜ ๋ฐฐ์—ด numbers๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. numbers์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ์ธ๋ฑ์Šค์— ์žˆ๋Š” ๋‘ ๊ฐœ์˜ ์ˆ˜๋ฅผ ๋ฝ‘์•„ ๋”ํ•ด์„œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์ˆ˜๋ฅผ ๋ฐฐ์—ด์— ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.


์ œํ•œ์‚ฌํ•ญ

  • numbers์˜ ๊ธธ์ด๋Š” 2 ์ด์ƒ 100 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
    • numbers์˜ ๋ชจ๋“  ์ˆ˜๋Š” 0 ์ด์ƒ 100 ์ดํ•˜์ž…๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ

numbers return
[2,1,3,4,1] [2,3,4,5,6,7]
[5,0,2,7] [2,5,7,9,12]

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • 2 = 1 + 1 ์ž…๋‹ˆ๋‹ค. (1์ด numbers์— ๋‘ ๊ฐœ ์žˆ์Šต๋‹ˆ๋‹ค.)
  • 3 = 2 + 1 ์ž…๋‹ˆ๋‹ค.
  • 4 = 1 + 3 ์ž…๋‹ˆ๋‹ค.
  • 5 = 1 + 4 = 2 + 3 ์ž…๋‹ˆ๋‹ค.
  • 6 = 2 + 4 ์ž…๋‹ˆ๋‹ค.
  • 7 = 3 + 4 ์ž…๋‹ˆ๋‹ค.
  • ๋”ฐ๋ผ์„œ [2,3,4,5,6,7] ์„ return ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • 2 = 0 + 2 ์ž…๋‹ˆ๋‹ค.
  • 5 = 5 + 0 ์ž…๋‹ˆ๋‹ค.
  • 7 = 0 + 7 = 5 + 2 ์ž…๋‹ˆ๋‹ค.
  • 9 = 2 + 7 ์ž…๋‹ˆ๋‹ค.
  • 12 = 5 + 7 ์ž…๋‹ˆ๋‹ค.
  • ๋”ฐ๋ผ์„œ [2,5,7,9,12] ๋ฅผ return ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

ํ’€์ด

์„ค๋ช…

  1. ํ•ด๋‹น ๋ฌธ์ œ๋Š” ์ž…์ถœ๋ ฅ ์˜ˆ์‹œ์ฒ˜๋Ÿผ ๋ฐฐ์—ด์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ์š”์†Œ๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ๋”ํ•œ ๊ฐ’์„ ์ถœ๋ ฅ ํ•ด์•ผ ํ•œ๋‹ค.
  2. ๋‹จ, ๋”ํ•œ ๊ฐ’์ด ์ค‘๋ณต์œผ๋กœ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ํ•˜๋‚˜๋งŒ ๋ฐฐ์—ด์— ์ €์žฅํ•ด์•ผ ํ•œ๋‹ค.

1๋ฒˆ์˜ ์กฐ๊ฑด์€ ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
ํ•˜์ง€๋งŒ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฐฉ๋ฒ•์—์„œ ๋‚ด๊ฐ€ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ ๋ณด๋‹ค ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ํ›จ์”ฌ ํšจ์œจ์ ์ด๋ผ๊ณ  ํŒ๋‹จํ–ˆ๋‹ค. ๊ทธ๋ž˜์„œ Set ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

Set ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๋Œ€ํ‘œ์ ์ธ ๊ตฌํ˜„ ํด๋ž˜์Šค๋กœ HashSet๊ฐ€ ์กด์žฌํ•œ๋‹ค.
๋จผ์ € HashSet์€ Hash ํ˜•ํƒœ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ •๋ ฌ์— ๋Œ€ํ•ด ์™„์ „ํ•˜์ง€ ๋ชปํ•˜๋‹ค.
์ฆ‰, ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ์˜ ์ˆœ์„œ๊ฐ€ ๋’ค์ฃฝ๋ฐ•์ฃฝ์ด๋ผ๋Š” ๋œป์ด๋‹ค!
๋‹จ, ์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๋Š” ์ €์žฅํ•˜์ง€ ์•Š๋Š” Set ์ž๋ฃŒ๊ตฌ์กฐ์˜ ํŠน์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

Set ์ž๋ฃŒ๊ตฌ์กฐ ํ†บ์•„๋ณด๊ธฐ

 

[Java] ์ž๋ฐ” HashSet ์‚ฌ์šฉ๋ฒ• & ์˜ˆ์ œ ์ด์ •๋ฆฌ

HashSet์ด๋ž€? HashSet์€ Set ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— Set์˜ ์„ฑ์งˆ์„ ๊ทธ๋Œ€๋กœ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค. Set์€ ๊ฐ์ฒด๋ฅผ ์ค‘๋ณตํ•ด์„œ ์ €์žฅํ•  ์ˆ˜ ์—†๊ณ  ํ•˜๋‚˜์˜ null ๊ฐ’๋งŒ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ์ €์žฅ ์ˆœ

coding-factory.tistory.com

์ด๋Ÿฌํ•œ ์ด์œ ๋กœ ๋‚˜๋Š” ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์„ ๊ฐ๊ฐ ๋”ํ•œ ํ›„, ํ•ด๋‹น ๊ฐ’์„ HashSet์— ์ €์žฅํ–ˆ๊ณ , ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ Iterator๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ Arrays.sort() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด ๋”ํ•œ ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ์•„์ด๋””์–ด๋กœ ์ฝ”๋“œ๋ฅผ ๊ตฌ์„ฑํ–ˆ๋‹ค.

์ฝ”๋“œ

package ์‹ฌ์„ฑํ—Œ.์•Œ๊ณ ๋ฆฌ์ฆ˜_6์ฃผ์ฐจ;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class ๋‘๊ฐœ๋ฝ‘์•„์„œ๋”ํ•˜๊ธฐ_ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค {

    public static void main(String[] args) {
        int[] arr = { 5, 0, 2, 7 };
        int[] answer = solution(arr);

        for (int i = 0; i < answer.length; i++) {
            System.out.print(answer[i] + " ");
        }
    }

    public static int[] solution(int[] numbers) {
        Set<Integer> set = new HashSet<Integer>();
        for (int i = 0; i < numbers.length - 1; i++) {
            int x = numbers[i];
            for (int k = i + 1; k < numbers.length; k++) {
                int y = numbers[k];
                set.add(x + y);
            }
        }

        Iterator<Integer> iter = set.iterator();

        int[] answer = new int[set.size()];
        int cnt = 0;
        while (iter.hasNext()) {
            answer[cnt] = iter.next();
            cnt++;
        }

        for (int i = 0; i < answer.length - 1; i++) {
            int tmp = answer[i];
            for(int k = i + 1; k < answer.length; k++) {
                if(tmp > answer[k]) {
                    answer[i] = answer[k];
                    answer[k] = tmp;
                    tmp = answer[i];
                }
            }
        }

        return answer;
    }
}