Skip to content

[1주차] 이예진 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Sep 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
이예진: [PG] 154539 뒤에 있는 큰 수 찾기_240913
- 설명 주석 추가
  • Loading branch information
yeahdy committed Sep 13, 2024
commit 211b3a1fe817bdc8f64cd3ce4bb3b20c109f03ef
13 changes: 9 additions & 4 deletions Programmers/Level2/YJ_뒤에_있는_큰_수_찾기.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import java.util.Stack;

/**
* Stack 문제
* 알고리즘: Stack
* 시간복잡도: O(n)
* 아이디어:
* 전체에서 하나씩 더 큰수를 찾으며 비교하는게 아니라,
* 전체 반복을 돌면서 기존 수과 다음에 오는 수를 계속해서 비교하는데 이때 이전에 누적된 수도 함께 갱신할 수 있어야한다.
* 또한 배열에 담긴 수를 가져오기 위해 index 를 활용해서 Stack에 index를 담고 이전에 누적된 수를 가져오도록 한다
*/
public class YJ_뒤에_있는_큰_수_찾기 {
public static void main(String[] args) {
Expand All @@ -19,14 +24,14 @@ static int[] solution(int[] numbers) {
Stack<Integer> stack = new Stack<>();
int i = 0;
stack.push(i);
for(i=1; i<n; i++){
while(!stack.isEmpty() && numbers[stack.peek()] < numbers[i]){
for(i=1; i<n; i++){ //O(n)
while(!stack.isEmpty() && numbers[stack.peek()] < numbers[i]){ //O(n)
numbers[stack.pop()] = numbers[i];
}
stack.push(i);
}

while(!stack.isEmpty()){
while(!stack.isEmpty()){ //O(n)
numbers[stack.pop()] = -1;
}

Expand Down