[프로그래머스] 뒤에 있는 큰 수 찾기 - JAVA [자바]

doxxx·2023년 3월 10일
1

프로그래머스

목록 보기
13/17
post-thumbnail
post-custom-banner
import java.util.*;  
  
class Solution {  
  
    public int[] solution(int[] numbers) {  
        int[] answer = new int[numbers.length];  
        Stack<int[]> stack = new Stack<>();  
        for (int i = 0; i < numbers.length; i++) {  
            if (stack.isEmpty()) {  
                stack.push(new int[]{numbers[i], i});  
            } else {  
                while (!stack.isEmpty() && stack.peek()[0] < numbers[i]) {  
                    answer[stack.pop()[1]] = numbers[i];  
                }  
                stack.push(new int[]{numbers[i], i});  
            }  
        }  
        while (!stack.isEmpty()) {  
            answer[stack.pop()[1]] = -1;  
        }  
        return answer;  
    }  
}
import java.util.*;  
  
class Solution {  
  
    public int[] solution(int[] numbers) {  
        int[] answer = new int[numbers.length];  
        Stack<Integer> stack = new Stack<>();  
        for (int i = 0; i < numbers.length; i++) {  
            while (!stack.isEmpty() && numbers[stack.peek()] < numbers[i]) {  
                answer[stack.pop()] = numbers[i];  
            }  
            stack.push(i);  
        }  
        while (!stack.isEmpty()) {  
            answer[stack.pop()] = -1;  
        }  
        return answer;  
    }  
}
import java.util.*;  
  
class Solution {  
  
    public int[] solution(int[] numbers) {  
        int[] answer = new int[numbers.length];  
        Arrays.fill(answer, -1);  
        Stack<Integer> stack = new Stack<>();  
        for (int i = 0; i < numbers.length; i++) {  
            while (!stack.isEmpty() && numbers[stack.peek()] < numbers[i]) {  
                answer[stack.pop()] = numbers[i];  
            }  
            stack.push(i);  
        }  
        return answer;  
    }  
}
post-custom-banner

0개의 댓글