Daily Temperature
Given a list of daily temperatures T
, return a list such that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0
instead.
For example, given the list of temperatures T = [73, 74, 75, 71, 69, 72, 76, 73]
, your output should be [1, 1, 4, 2, 1, 1, 0, 0]
.
Note: The length of temperatures
will be in the range [1, 30000]
. Each temperature will be an integer in the range [30, 100]
.
์๋ฃ๊ตฌ์กฐ : Stack์ด์ฉ
์๊ณ ๋ฆฌ์ฆ ์คํ ํ์ฉํ๊ธฐ ํ ํฌ๋๐ 2-1. ์ผ๋จ ์ธ๋ฑ์ค๊ฐ์ ์คํ์ ๋ค ๋ฃ๋๋ค. ์ฒ์์๋ ์คํ์ด ๋น์ด์๊ณ , result[i] = 0. - stack.push(i), (i๋ T.length-1๋ถํฐ ๊ฐ์ํ๋ ๋ฐ๋ณต๋ฌธ) 2-2. ์์๊ฐ์ ๋น๊ตํ๋ค. 2-3. ํ๋จ๊ธฐ์ค์ ๋ถํฉํ๋ค๋ฉด stack.peek()-i๋ฅผ ๊ฒฐ๊ณผ๊ฐ์ ๋ฃ๋๋ค. 2-4. ๋ถํฉํ์ง ์๋๋ค๋ฉด ๊ธฐ์ค์ ๋ถํฉํ๋ ๊ฒ์ ์ฐพ์ ๋๊น์ง stack.pop()ํ๋ค. stack์ด ๋น ๋๊น์ง ๋ชป์ฐพ์ผ๋ฉด ๊ฒฐ๊ณผ๊ฐ์ 0. => stack.isEmpty() ? 0 : stack.peek()-i =>2-1๊ณผ 2-4์์ ๊ณตํต๋ ๋ถ๋ถ : ์คํ์ด ๋น์ด์์ผ๋ฉด result[i] = 0
์๊ณ ๋ฆฌ์ฆ์ Java๋ก ๊ตฌํ
Last updated