์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜3. Two Pointers, Sliding Window [ํšจ์œจ์„ฑ : O(n^2) --> O(n)] : ๊ณตํ†ต ์›์†Œ ๊ตฌํ•˜๊ธฐ

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

728x90

 

https://hyejin.tistory.com/1220

 

์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜3. Two Pointers, Sliding Window [ํšจ์œจ์„ฑ : O(n^2) --> O(n)]

https://hyejin.tistory.com/1219 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜2. Array(1, 2์ฐจ์› ๋ฐฐ์—ด) : ์ž„์‹œ ๋ฐ˜์žฅ ์ •ํ•˜๊ธฐ https://hyejin.tistory.com/1218 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜2. Array(1, 2์ฐจ์› ๋ฐฐ์—ด) : ๋ด‰์šฐ๋ฆฌ ht

hyejin.tistory.com

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

 

 

 

2. ๊ณตํ†ต ์›์†Œ ๊ตฌํ•˜๊ธฐ 

์„ค๋ช…

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

 

์ž…๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์— ์ง‘ํ•ฉ A์˜ ํฌ๊ธฐ N(1<=N<=30,000)์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

๋‘ ๋ฒˆ์งธ ์ค„์— N๊ฐœ์˜ ์›์†Œ๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์›์†Œ๊ฐ€ ์ค‘๋ณต๋˜์–ด ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์„ธ ๋ฒˆ์งธ ์ค„์— ์ง‘ํ•ฉ B์˜ ํฌ๊ธฐ M(1<=M<=30,000)์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

๋„ค ๋ฒˆ์งธ ์ค„์— M๊ฐœ์˜ ์›์†Œ๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์›์†Œ๊ฐ€ ์ค‘๋ณต๋˜์–ด ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๊ฐ ์ง‘ํ•ฉ์˜ ์›์†Œ๋Š” 1,000,000,000์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.

 

์ถœ๋ ฅ

๋‘ ์ง‘ํ•ฉ์˜ ๊ณตํ†ต์›์†Œ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜์—ฌ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

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

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

 

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

2 3 5

 

 

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

public ArrayList<Integer> solution2(int n, int[] A, int m, int[] B) {
    ArrayList<Integer> result = new ArrayList<>();
    Arrays.sort(A);
    Arrays.sort(B);

    for (int i : A) {
        for (int i1 : B) {
            if (i == i1){
                result.add(i);
                break;
            }
        }
    }

    return result;
}

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป : ๋จผ์ € ๋ฐฐ์—ด 2๊ฐœ๋ฅผ ์˜ค๋ฆ„ ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์ค€ ๋‹ค์Œ, ์ด์ค‘ for๋ฌธ์„ ๋Œ๋ฉด์„œ ๊ฐ™์€ ๊ฐ’์„ result ๋ฆฌ์ŠคํŠธ์— add ํ•˜๋„๋ก ํ–ˆ๋Š”๋ฐ,, ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด 

์‹œ๊ฐ„ ์ œํ•œ์—์„œ ๊ฑธ๋ฆฐ๋‹ค,, ๋ณด๋‹ˆ๊นŒ ์ด์ค‘ for ๋ฌธ์„ ๋Œ๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค,,, 

 

 

 

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

public ArrayList<Integer> solution(int n, int[] A, int m, int[] B) {
    ArrayList<Integer> result = new ArrayList<>();
    Arrays.sort(A);
    Arrays.sort(B);
    int p1 = 0, p2 = 0;

    while (p1 < n && p2 < m) {
        if (A[p1] == B[p2]) {
            result.add(A[p1++]);
            p2++;
        } else if (A[p1] < B[p2]) {
            p1++;
        }else {
            p2++;
        }
    }

    return result;
}

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป : ๊ทธ๋ž˜์„œ ์ €๋ฒˆ ํ’€์ด์—์„œ ๋ดค๋˜ p1, p2 ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ , p1 ๊ณผ p2 ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ผ๋„ n, m ํฌ๊ธฐ๋ฅผ ๋„˜์–ด๊ฐ€๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ์ข…๋ฃŒํ•˜๋„๋ก ํ–ˆ๋‹ค. 

๋จผ์ € A[p1]๊ณผ B[p2] ๊ฐ€ ๊ฐ™์œผ๋ฉด result ๋ฆฌ์ŠคํŠธ์— add ํ•ด์ฃผ๊ณ , ๊ทธ ๋‹ค์Œ p1, p2 ๊ฐ’์„ ๊ฐ๊ฐ +1์”ฉ ์ฆ๊ฐ€ํ•ด์ค€๋‹ค. 

๋งŒ์•ฝ ๊ฐ™์ง€ ์•Š์€๋ฐ A[p1]์ด B[p2]๋ณด๋‹ค ์ž‘์œผ๋ฉด p1์˜ ๊ฐ’์„ 1 ์ฆ๊ฐ€ ํ•ด์ฃผ๊ณ , ๊ทธ ๋ฐ˜๋Œ€์ผ ๊ฒฝ์šฐ์—๋Š” p2์˜ ๊ฐ’์„ 1์ฆ๊ฐ€ํ•ด์ค€๋‹ค. 

 

 

 

 

 

728x90