ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Algorithm] Two pointers ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•˜์—ฌ ๊ณตํ†ต์›์†Œ ๊ตฌํ•˜๊ธฐ
    ๐Ÿ’ปProgramming/Algorithm 2024. 3. 2. 20:52

    ๋ฌธ์ œ

    A, B ๋‘ ๊ฐœ์˜ ์ง‘ํ•ฉ์ด ์ฃผ์–ด์ง€๋ฉด ๋‘ ์ง‘ํ•ฉ์˜ ๊ณตํ†ต ์›์†Œ๋ฅผ ์ถ”์ถœํ•˜์—ฌ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”. (๊ฐ ์ง‘ํ•ฉ์•ˆ์˜ ์›์†Œ๋Š” ์ค‘๋ณต๋˜์ง€ ์•Š๋Š”๋‹ค.)

     

    ์ž…๋ ฅ ์˜ˆ์‹œ

    5
    1 3 9 5 2
    5
    3 2 5 7 8

     

    ์ถœ๋ ฅ ์˜ˆ์‹œ 

    2 3 5

     

    ์ผ๋‹จ ๋‚ด๊ฐ€ ์ง  ์ฝ”๋“œ๋Š” ์ด์ค‘ for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ n๊ณผ m์ด ๊ฐ ๋ฆฌ์ŠคํŠธ์˜ ์›์†Œ์˜ ์ˆ˜๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(n*m)์ด์—ˆ๋‹ค. ๊ทธ๋žฌ๋”๋‹ˆ ๋ฆฌ์ŠคํŠธ๋ณ„๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ๊ฐ 30000๊ฐœ์ผ ๊ฒฝ์šฐ ํƒ€์ž„์•„์›ƒ ์˜ค๋ฅ˜๊ฐ€ ๋‚ฌ๋‹ค. ์ œํ•œ์ด 1000ms์ธ๋ฐ 1700ms์ธ๊ฐ€.. (๋จธ์“ฑ..)

     

    ํˆฌํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•œ๋‹ค๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(n+m)์ด ๋˜๊ฒŒ ๋œ๋‹ค. ์ด ๋ฐฉ๋ฒ•์œผ๋กœ ํƒ€์ž„์•„์›ƒ ์—๋Ÿฌ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. 

    ์ด์ค‘ for๋ฌธ

    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class Main {
        private String solution(int n, int[] list, int m, int[] list2) {
            ArrayList<Integer> answerList = new ArrayList<>();
            String answer = "";
            
            if(n > m) {
                for(int i = 0; i < m; i++) {
                    for(int j = 0; j < n; j++) {
                        if(list2[i] == list[j]) {
                            answerList.add(list2[i]);
                            break;
                        }
                    }
                }
            } else {
                for(int i = 0; i < n; i++) {
                    for(int j = 0; j < m; j++) {
                        if(list[i] == list2[j]) {
                            answerList.add(list[i]);
                            break;
                        }
                    }
                }
            }
    
            // ๊ณตํ†ต ์›์†Œ๋ฅผ ์ •๋ ฌ
            for(int k = 0; k < answerList.size()-1; k++) {
                int leastIndex = k;
                for(int l = k+1; l < answerList.size(); l++) {
                    if(answerList.get(leastIndex) > answerList.get(l)) {
                        leastIndex = l;
                    }
                }
    
                answer += answerList.get(leastIndex) + " ";
    
                int tmp = answerList.get(k);
                answerList.set(k, answerList.get(leastIndex));
                answerList.set(leastIndex, tmp);
            }
    
            answer += answerList.get(answerList.size()-1);
    
            return answer;
        }
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            Main main = new Main();
    
            int n = scanner.nextInt();
            int[] list = new int[n];
            for(int i = 0; i < n; i++) {
                list[i] = scanner.nextInt();
            }
            int m = scanner.nextInt();
            int[] list2 = new int[m];
            for(int j = 0; j < m; j++) {
                list2[j] = scanner.nextInt();
            }
    
            System.out.println(main.solution(n, list, m, list2));
        }
    }

     

    ๋ถ€๋„๋Ÿฝ์ง€๋งŒ ์ •๋ ฌ์ด ๋˜์–ด์žˆ์ง€ ์•Š์€ ๋‘ ๋ฆฌ์ŠคํŠธ ์ค‘์— ํ•œ ์งง์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ฒซ๋ฒˆ์งธ for๋ฌธ, ๋” ํฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋‘๋ฒˆ ์งธ for๋ฌธ ์•ˆ์— ๋„ฃ์–ด ์ผ์น˜ํ•  ๊ฒฝ์šฐ ๋‹ต์— ์ถ”๊ฐ€ํ•˜๊ณ  breakํ•˜๋„๋ก ๋ฐ˜๋ณตํ•œ ํ›„,

    ์ตœ์ข…์ ์œผ๋กœ ๊ณตํ†ต ์›์†Œ๊ฐ€ ๋“ค์–ด์žˆ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ์„ ํƒ ์ •๋ ฌ์„ ์ด์šฉํ•˜์—ฌ ์ •๋ ฌํ•˜๋„๋ก ํ•œ ์ฝ”๋“œ์ด๋‹ค,,,

    ์ด๊ฒŒ ๊ณ„์† ํƒ€์ž„์•„์›ƒ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋˜ ์ฝ”๋“œ์ด๋‹ค. 

     

    Two Pointers๋ฅผ ์ด์šฉํ•œ ์ฝ”๋“œ

    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Main {
        private String solution(int n, int[] list, int m, int[] list2) {
            Arrays.sort(list);
            Arrays.sort(list2);
            String answer = "";
            int p1 = 0, p2 = 0;
    
            while(p1 < n && p2 < m) {
                if(list[p1] == list2[p2]) {
                    answer += list[p1++] + " ";
                    p2++;
                } else if(list[p1] < list2[p2]) {
                    p1++;
                } else {
                    p2++;
                }
            }
    
            return answer;
        }
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            Main main = new Main();
    
            int n = scanner.nextInt();
            int[] list = new int[n];
            for(int i = 0; i < n; i++) {
                list[i] = scanner.nextInt();
            }
            int m = scanner.nextInt();
            int[] list2 = new int[m];
            for(int j = 0; j < m; j++) {
                list2[j] = scanner.nextInt();
            }
    
            System.out.println(main.solution(n, list, m, list2));
        }
    }
    

     

    ์šฐ์„  ์ด ์ฝ”๋“œ๋Š” Arrays.sort๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ํ›„, ๋‘ ๋ฐฐ์—ด์„ ๊ฐ๊ฐ ๊ฐ€๋ฆฌํ‚ฌ p1, p2 ํฌ์ธํ„ฐ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ๋งŒ์•ฝ ์ผ์น˜ํ•œ๋‹ค๋ฉด ๋‘˜ ๋‹ค +๋ฅผ ํ•ด์ฃผ๊ณ , ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๋ฐ ์ธ๋ฑ์Šค์— ํ•ด๋‹นํ•˜๋Š” ๋ฐฐ์—ด ๊ฐ’์ด ์ž‘์„ ๊ฒฝ์šฐ ์ž‘์€ ์ธ๋ฑ์Šค๋ฅผ +ํ•ด์ฃผ๋Š” ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(n+m)์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

     

     

    ๋Œ€์ถฉ ์ด๋Ÿฐ ํ”Œ๋กœ์šฐ๋กœ ๊ฐ€๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ ์›๋ž˜์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๋‹ฌ๋ฆฌ n+m๋ฒˆ๋งŒ ๋ฐ˜๋ณตํ•˜๋ฉด ์ •๋ ฌ๋œ ๊ณตํ†ต๋œ ์›์†Œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. ์žฌ์ž‘๋…„์— ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ˆ˜์—…๋•Œ ๋ฐฐ์› ๋Š”๋ฐ ์ด์ œ์„œ์•ผ ๊ธฐ์–ต์ด ๋‚˜๋Š” ๋‚˜,,,, ์ •์‹ ์ฐจ๋ฆฌ์Ÿˆ!!! ์—„๊ต์ˆ˜๋‹˜ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค

Designed by Tistory.