๊ด€๋ฆฌ ๋ฉ”๋‰ด

Daehyunii's Dev-blog

์ตœ๋Œ€ ๋งค์ถœ(ํšจ์œจ์„ฑ-์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ) ๋ณธ๋ฌธ

๐Ÿ“š Language & CS knowledge/Algorithm (๊ธฐ์ดˆ๋ฌธ์ œํ’€์ด)

์ตœ๋Œ€ ๋งค์ถœ(ํšจ์œจ์„ฑ-์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ)

Daehyunii 2022. 9. 2. 17:48

๋ฌธ์ œ(์ถœ์ฒ˜ : ์ธํ”„๋Ÿฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ๊ฐ•์˜, ์ •๋ณด์˜ฌ๋ฆผํ”ผ์•„๋“œ)

 

ํ˜„์ˆ˜์˜ ์•„๋น ๋Š” ์ œ๊ณผ์ ์„ ์šด์˜ํ•ฉ๋‹ˆ๋‹ค. ํ˜„์ˆ˜ ์•„๋น ๋Š” ํ˜„์ˆ˜์—๊ฒŒ N์ผ ๋™์•ˆ์˜ ๋งค์ถœ๊ธฐ๋ก์„ ์ฃผ๊ณ  ์—ฐ์† ๋œ K์ผ ๋™์•ˆ์˜ ์ตœ๋Œ€ ๋งค์ถœ์•ก์ด ์–ผ๋งˆ์ธ์ง€ ๊ตฌํ•˜๋ผ๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ N=10์ด๊ณ  10์ผ ๊ฐ„์˜ ๋งค์ถœ๊ธฐ๋ก์ด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ด๋•Œ K=3์ด๋ฉด 12 15 11 20 25 10 20 19 13 15 ์—ฐ์†๋œ 3์ผ๊ฐ„์˜ ์ตœ๋Œ€ ๋งค์ถœ์•ก์€ 11+20+25=56๋งŒ์›์ž…๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ํ˜„์ˆ˜๋ฅผ ๋„์™€์ฃผ์„ธ์š”.

โ–ฃ ์ž…๋ ฅ์„ค๋ช…
์ฒซ ์ค„์— N(5<=N<=100,000)๊ณผ M(2<=K<=N)๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
๋‘ ๋ฒˆ์งธ ์ค„์— N๊ฐœ์˜ ์ˆซ์ž์—ด์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๊ฐ ์ˆซ์ž๋Š” 500์ดํ•˜์˜ ์Œ์ด ์•„๋‹Œ ์ •์ˆ˜์ž…๋‹ˆ๋‹ค.

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

 

โ–ฃ ์ž…๋ ฅ์˜ˆ์ œ 1
10 3
12 15 11 20 25 10 20 19 13 15

 

โ–ฃ ์ถœ๋ ฅ์˜ˆ์ œ 1

56

 

Tip

 

๋ฌธ์ œํ’€์ด

//๊ฐ•์˜๋“ฃ๊ณ  ๋‹ค์‹œ ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ •๋‹ต
function solution(arr, days){
    let max = Number.MIN_SAFE_INTEGER;
    let sum =0

    for(let i = 0 ; i < days ; i++){ 
        sum += arr[i]
        if(sum > max) max = sum;
    }
    for(let i = days ; i < arr.length ; i++){
        sum = sum + (arr[i]-arr[i-days]);
        if(sum > max) max = sum; 
        }
    return max;
}

let arr = [12,15,11,20,25,10,20,19,13,15];
console.log(solution(arr, 3));
console.log('helloo');