-
[알고리즘] Daily temperatures (O)알고리즘/배열 2021. 4. 20. 11:01
Problem
Key Point
온도가 높은 index가 나올 때까지 stack에 쌓아놓다가 조건을 만족할 때 stack에서 뽑아서 계산한다.
Code
public class DailyTemperature { public void solve(int[] nums) { Stack<Integer> stack = new Stack<>(); int[] result = new int[nums.length]; for (int i = 0; i < nums.length; i++) { while (!stack.isEmpty() && nums[stack.peek()] < nums[i]) { int index = stack.pop(); result[index] = i - index; } stack.push(i); } System.out.println(Arrays.toString(result)); } public static void main(String[] args) { DailyTemperature dailyTemperature = new DailyTemperature(); dailyTemperature.solve(new int[]{73, 74, 75, 71, 69, 72, 76, 73}); } }
'알고리즘 > 배열' 카테고리의 다른 글
[알고리즘] Move Zeroes (0) 2021.04.20 [알고리즘] Two Sum (0) 2021.04.20 [알고리즘] Merge Intervals (O) (0) 2021.04.20 [알고리즘] Meeting Rooms II (0) 2021.04.20 [알고리즘] Jewels And Stones (0) 2021.04.20