๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

42584. ์ฃผ์‹ ๊ฐ€๊ฒฉ

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ฃผ์‹๊ฐ€๊ฒฉ


๋ฌธ์ œ ์œ ํ˜•๋‚œ์ด๋„๊ฑธ๋ฆฐ ์‹œ๊ฐ„ํ•ด๊ฒฐ ์œ ๋ฌด(โœ…/โŒ)
์Šคํƒ/ํlv.21์‹œ๊ฐ„โœ…

์„ค๊ณ„ ๋ฐฉ๋ฒ•#

  • pirces ์™€ ๊ฐ™์€ ๊ธธ์ด์˜ ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค.

  • ์ดˆ ๋‹จ์œ„ ์‹œ์ ์„ ๋‹ด์„ ์Šคํƒ์„ ๋งŒ๋“ ๋‹ค.

    • s.top() ์€ ๋ฐ”๋กœ ์ „ ์‹œ์ ์ด๋‹ค.
  • prices ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ํ˜„์žฌ ๊ฐ€๊ฒฉ๋ณด๋‹ค ์ด์ „ ๊ฐ€๊ฒฉ๋ณด๋‹ค ๋–จ์–ด์กŒ๋‹ค๋ฉด,

    • ์ด์ „ ์‹œ์ ์˜ answer ๋Š” ํ˜„์žฌ ์‹œ๊ฐ„ ( i ) - ๋ฐ”๋กœ ์ „ ์‹œ์  ( s.top() )์ด๋‹ค.

    • ์Šคํƒ์„ ๋ฐ˜๋ณตํ•˜๋ฉฐ ๊ทธ ์ „ ์‹œ์ ์— ๋Œ€ํ•ด์„œ๋„ ๋ฐ˜๋ณตํ•œ๋‹ค.

  • ํ˜„์žฌ ์‹œ์  ( i )๋ฅผ ์Šคํƒ์— ๋„ฃ๋Š”๋‹ค.

  • ์Šคํƒ์— ๊ฐ’์ด ๋‚จ์•˜๋‹ค๋ฉด ๊ทธ ์‹œ์ ์˜ ๋‹ต์€ ์ „์ฒด ๊ธธ์ด - ์‹œ์  - 1 ์ด๋‹ค.

์ฝ”๋“œ#

def solution(prices):    answer = [0] * len(prices)    stack = []
    for i in range(len(prices)):        while stack and prices[stack[len(stack) - 1]] > prices[i]:            answer[stack[len(stack) - 1]] = i - stack[len(stack) - 1]            stack.pop()        stack.append(i)
    while stack:        answer[stack[len(stack) - 1]] = len(prices) - stack[len(stack) - 1] - 1        stack.pop()
    return answer
function solution(prices) {    const answer = Array(prices.length);    const s = new Stack();
    prices.forEach((price, i) => {        while (!s.isEmpty() && prices[s.top()] > price) {            answer[s.top()] = i - s.top();            s.pop();        }        s.push(i);    });
    while (!s.isEmpty()) {        answer[s.top()] = prices.length - s.top() - 1;        s.pop();    }
    return answer;}
class Stack {    constructor() {        this._arr = [];    }
    push(value) {        this._arr.push(value);    }
    pop() {        return this._arr.pop();    }
    top() {        return this._arr[this._arr.length - 1];    }
    isEmpty() {        return this._arr.length === 0;    }}
module.exports = solution;

์‹œ๊ฐ„ ๋ณต์žก๋„#

  • prices ์ˆœํšŒ : O(NM)

  • ์Šคํƒ ์ˆœํšŒ : O(M)

  • ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค.

์–ด๋ ค์› ๋˜ ์ #

  • ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ์“ฐ๋Š” ๋ฐฉ๋ฒ•์ด ๋จผ์ € ๋– ์˜ฌ๋ž์ง€๋งŒ ์—ญ์‹œ ์‹œ๊ฐ„ ์ดˆ๊ณผ ์—๋Ÿฌ๊ฐ€ ๋‚˜์„œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ๊ณ ๋ฏผํ•ด์•ผ ํ–ˆ๋‹ค.

  • JS์—์„œ ๋ฐฐ์—ด์„ ์Šคํƒ์œผ๋กœ ์“ธ ๋•Œ top() , isEmpty() ๋ฉ”์†Œ๋“œ๊ฐ€ ์—†์–ด์„œ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋Š”๊ฒŒ ๋ถˆํŽธํ–ˆ๋‹ค.

  • shift ์—ฐ์‚ฐ์ด O(n)์ด๋ผ ์‹ ๊ฒฝ์“ฐ์—ฌ์„œ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ตฌํ˜„ํ•ด ๋ดค๋‹ค.

class Stack {    constructor(...items) {        this._items = [];
        if (items.length > 0) {            items.forEach((item) => this._items.push(item));        }    }
    push(...items) {        items.forEach((item) => this._items.push(item));        return this._items;    }
    pop(count = 0) {        if (count === 0) {            return this._items.pop();        } else {            return this._items.splice(-count, count);        }    }
    peek() {        return this._items[this.end - 1];    }
    size() {        return this._items.length;    }
    isEmpty() {        return this._items.length === 0;    }
    toArray() {        return this._items;    }}
class Queue {    constructor(...items) {        this._items = [];        this.start = 0;        this.end = this._items.length;
        this.enqueue(...items);    }
    enqueue(...items) {        items.forEach((item) => this._items.push(item));        this.end += items.length;    }
    dequeue(count = 1) {        const prev = this.start;        this.start += count;        const items = this._items.slice(prev, this.start);
        if (this.start * 2 >= this._items.length) {            this._items = this._items.slice(this.start);            this.start = 0;            this.end = this._items.length;        }
        return items;    }
    isEmpty() {        return this.end === this.start;    }
    peek() {        return this._items[this.end - 1];    }
    size() {        return this.end - this.start;    }
    toArray() {        return this._items.slice(this.start, this.end);    }}

์ฐธ๊ณ ์ž๋ฃŒ#