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

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

728x90

 

https://hyejin.tistory.com/1221

 

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

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

hyejin.tistory.com

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

 

 

 

3. ์ตœ๋Œ€ ๋งค์ถœ 

์„ค๋ช…

ํ˜„์ˆ˜์˜ ์•„๋น ๋Š” ์ œ๊ณผ์ ์„ ์šด์˜ํ•ฉ๋‹ˆ๋‹ค. ํ˜„์ˆ˜ ์•„๋น ๋Š” ํ˜„์ˆ˜์—๊ฒŒ N์ผ ๋™์•ˆ์˜ ๋งค์ถœ๊ธฐ๋ก์„ ์ฃผ๊ณ  ์—ฐ์†๋œ K์ผ ๋™์•ˆ์˜ ์ตœ๋Œ€ ๋งค์ถœ์•ก์ด ์–ผ๋งˆ์ธ์ง€ ๊ตฌํ•˜๋ผ๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค.

๋งŒ์•ฝ N=10์ด๊ณ  10์ผ ๊ฐ„์˜ ๋งค์ถœ๊ธฐ๋ก์ด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋•Œ K=3์ด๋ฉด

12 1511 20 2510 20 19 13 15

์—ฐ์†๋œ 3์ผ๊ฐ„์˜ ์ตœ๋Œ€ ๋งค์ถœ์•ก์€ 11+20+25=56๋งŒ์›์ž…๋‹ˆ๋‹ค.

์—ฌ๋Ÿฌ๋ถ„์ด ํ˜„์ˆ˜๋ฅผ ๋„์™€์ฃผ์„ธ์š”.

 

์ž…๋ ฅ

์ฒซ ์ค„์— N(5<=N<=100,000)๊ณผ K(2<=K<=N)๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

๋‘ ๋ฒˆ์งธ ์ค„์— N๊ฐœ์˜ ์ˆซ์ž์—ด์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๊ฐ ์ˆซ์ž๋Š” 500์ดํ•˜์˜ ์Œ์ด ์•„๋‹Œ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.

์ถœ๋ ฅ

์ฒซ ์ค„์— ์ตœ๋Œ€ ๋งค์ถœ์•ก์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

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

10 3
12 15 11 20 25 10 20 19 13 15

 

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

56

 

 

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

public int solution2(int n, int k, int[] a) {
    int max = Integer.MIN_VALUE;

    for (int i = 0; i <= n - k; i++) {
        int sum = 0;
        for (int j = 0; j < k; j++) {
            sum += a[i + j];
        }
        max = Math.max(max, sum);
    }

    return max;
}

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป : ๋‚˜์˜ ์ฒซ๋ฒˆ์งธ ์‹œ๋„์˜€๋‹ค... ์ด์ค‘ for๋ฌธ.. ๊ทธ๊ฑฐ ์–ด๋–ป๊ฒŒ ์•ˆ์“ฐ๋Š” ๊ฑด๋ฐ... 

์ด์ค‘ for ๋ฌธ์œผ๋กœ๋งŒ ํ•ด๊ฒฐํ•˜๋ ค๊ณ  ํ•˜๋‹ˆ๊นŒ... ์ž๊พธ ์‹œ๊ฐ„ ์ดˆ๊ณผ ๋œฌ๋‹ค.. (๋‹น์—ฐํ•จ) 

 

 

 

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

public int solution(int n, int k, int[] a) {
    int max = 0;
    int sum = 0;
    for (int i = 0; i <= n - k; i++) {
        if (i == 0) {
            for (int j = 0; j < k; j++) {
                sum += a[i + j];
                max = sum;
            }
        }else {
            sum = sum - a[i - 1] + a[i + k - 1];
            max = Math.max(max, sum);
        }

    }
    return max;
}

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป : ์–ด๋–ป๊ฒŒ ํ’€์–ด์•ผํ• ์ง€.. ๋ง‰๋ง‰ํ•ดํ•˜๋˜ ์™€์ค‘์—... ๊ฐ•์˜ ํžŒํŠธ๋กœ Sliding Window ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•œ๋‹ค ํ•ด์„œ... ๊ทธ๋Ÿผ Sliding Window ๊ฐ€ ๋ญ”๋ฐ! ํ•˜๊ณ  ๊ตฌ๊ธ€์— ์ณ์„œ ํ•ด๋‹ต์„ ์•Œ์•„๋ƒˆ๋‹ค..! ์œ ๋ ˆ์นด ! 

๋ณด๋ฉด ์ฃผ์–ด์ง„ ์ˆ˜ k ๋ผ๋Š” ์ฐฝ๋ฌธ์„ ๋งŒ๋“ค๊ณ ,, ๊ทธ ๋‹ค์Œ ํ•˜๋‚˜์”ฉ ์˜†์œผ๋กœ ์˜ฎ๊ฒจ๊ฐ€๋ฉด์„œ ๋”ํ•˜๊ธฐ๋ฅผ ํ•˜๋‹ˆ๊นŒ ๊ธฐ์กด 2๊ฐœ์˜ ๊ฐ’์€ ๊ทธ๋Œ€๋กœ๋‹ค. ์ด์ „ ๊ฐ’์€ ๋นผ๊ณ , ๊ทธ ๋‹ค์Œ ๊ฐ’์€ ๋”ํ•˜๊ณ ... ํ•˜๋Š” ๊ฒƒ์ผ ๋ฟ์ด๊ธฐ ๋•Œ๋ฌธ์— 

๋จผ์ € ์ดˆ๋ฐ˜ i[0] + i[1] + i[2] ์˜ ํ•ฉ์€ ๊ตฌํ•ด์ค˜์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— i= 0 ์ผ๋•Œ๋Š” k ๋งŒํผ sum์— ๋ˆ„์ ํ•ด์„œ ๋”ํ•˜๊ณ , max ๊ฐ’์— sum ๊ฐ’์„ ๋„ฃ์–ด์คฌ๋‹ค. 

๊ทธ ๋‹ค์Œ ๋ถ€ํ„ฐ๋Š” sum์— ๋งจ ์ฒ˜์Œ ๊ฐ’์€ ๋นผ๊ณ , ์ด์ œ ์˜†์œผ๋กœ ํ•œ์นธ ์˜ฎ๊ธฐ๋‹ˆ ์ƒˆ๋กœ์šด ๊ฐ’์„ ๋”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ๊ทธ ํ›„ max์™€ sum ๊ฐ’์„ ๋น„๊ตํ•ด์„œ max ๊ฐ’์„ ๊ฐฑ์‹ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

 

 

 

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

public int solution3(int n, int k, int[] arr) {
    int answer, sum = 0;
    for (int i = 0; i < k; i++) sum += arr[i];
    answer = sum;

    for (int i = k; i < n; i++) {
        sum += (arr[i] - arr[i - k]);
        answer = Math.max(answer, sum);
    }

    return answer;
}

๐Ÿ‘พ : ๊ฐ•์‚ฌ๋‹˜์€ for๋ฌธ์„ ๋‚˜๋ˆ ์„œ ๋จผ์ € sum์— ๋ฐฐ์—ด์˜ ๋งจ ์ฒ˜์Œ๋ถ€ํ„ฐ k ๋ฒˆ์งธ๊นŒ์ง€์˜ ๊ฐ’์„ ๋”ํ•ด์ฃผ๊ณ , ๊ทธ ๋‹ค์Œ for๋ฌธ์—์„œ๋Š” k ๋ฒˆ์งธ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ arr[i] ๊ฐ’์€ ๋”ํ•˜๊ณ , arr[i-k] ๊ฐ’์€ ๋นผ์ฃผ๋„๋ก ํ–ˆ๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

728x90

'์ธํ”„๋Ÿฐ > ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ์ž…๋ฌธ : ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋Œ€๋น„' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜3. Two Pointers, Sliding Window [ํšจ์œจ์„ฑ : O(n^2) --> O(n)] : ์—ฐ์†๋œ ์ž์—ฐ์ˆ˜์˜ ํ•ฉ  (0) 2023.10.19
์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜3. Two Pointers, Sliding Window [ํšจ์œจ์„ฑ : O(n^2) --> O(n)] : ์—ฐ์† ๋ถ€๋ถ„์ˆ˜์—ด  (1) 2023.10.18
์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜3. Two Pointers, Sliding Window [ํšจ์œจ์„ฑ : O(n^2) --> O(n)] : ๊ณตํ†ต ์›์†Œ ๊ตฌํ•˜๊ธฐ  (0) 2023.10.17
์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜3. Two Pointers, Sliding Window [ํšจ์œจ์„ฑ : O(n^2) --> O(n)]  (0) 2023.10.13
์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜2. Array(1, 2์ฐจ์› ๋ฐฐ์—ด) : ์ž„์‹œ ๋ฐ˜์žฅ ์ •ํ•˜๊ธฐ  (0) 2023.10.13