Skip to content
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

[Java] BOJ.2493 - 탑 #52

Merged
merged 5 commits into from
Aug 24, 2023
Merged

[Java] BOJ.2493 - 탑 #52

merged 5 commits into from
Aug 24, 2023

Conversation

3o14
Copy link
Member

@3o14 3o14 commented Aug 7, 2023

πŸ“£ Related Issue

μ œκ³΅λ˜λŠ” ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€ + μž„μ˜λ‘œ λ§Œλ“  ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€ λͺ¨λ‘ μ˜¬λ°”λ₯΄κ²Œ λ‚˜μ˜€λŠ”λ°
ν‹€λ ΈμŠ΅λ‹ˆλ‹€ 둜 λœ¨λŠ” μ½”λ“œμž…λ‹ˆλ‹€.
뭐가 λ¬Έμ œμΌκΉŒμš” 😞

🌁 Background

λ°±μ€€ 문제 풀이

πŸ‘©β€πŸ’» Contents

image

μ½”λ“œ

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		int N = Integer.parseInt(br.readLine());
		Integer[] arr = new Integer[N];
		Deque<Integer> stack = new ArrayDeque<>();
		List<Integer> list = new LinkedList<>();
		
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		// 1. 탑 μž…λ ₯λ°›κΈ°
		for(int i=0; i<N; i++) {
			arr[i] = Integer.parseInt(st.nextToken()); 
		}
		
		// 2. λμ—μ„œλΆ€ν„° 탐색
		for(int i=N-1; i>=0; i--) {
			
			// 3. μŠ€νƒμ΄ λΉ„μ–΄μžˆμœΌλ©΄ push(addFirst)
			if(stack.isEmpty()) {
				stack.addFirst(arr[i]);
			} else {
				// 4. ν˜„μž¬ μ›μ†Œκ°€ μŠ€νƒμ˜ top보닀 클 λ•Œ: pop & list에 μˆ˜μ‹ λ°›μ€ 탑 인덱슀 add & ν˜„μž¬ μ›μ†Œ push
				if(stack.peek() < arr[i]) {
					while(!stack.isEmpty() && stack.peek() < arr[i]) { // ν˜„μž¬ μ›μ†Œλ³΄λ‹€ μž‘μ€ 값인 μŠ€νƒ μš”μ†Œλ₯Ό μ „λΆ€ pop&list.add
						stack.poll();
						list.add(i+1);
					}
					stack.addFirst(arr[i]);
					
				// 5. ν˜„μž¬ μ›μ†Œκ°€ μŠ€νƒμ˜ top보닀 μž‘μ„ λ•Œ: μŠ€νƒμ— push
				} else if(stack.peek() >= arr[i]) {
					stack.addFirst(arr[i]);
				}
			}
		}
		
		// 배열을 ν•œλ°”ν€΄ λ‹€ λŒκ³ λ„ μŠ€νƒμ— λ‚¨μ•„μžˆλŠ” 탑듀 (μžκΈ°μžμ‹ λ³΄λ‹€ 높은 탑을 λ°œκ²¬ν•˜μ§€ λͺ»ν•΄ μˆ˜μ‹ λ°›μ§€ λͺ»ν•œ 탑듀)
		while(!stack.isEmpty()) {
			int idx = Arrays.asList(arr).indexOf(stack.pollLast());
			list.add(arr.length-idx-1, 0); // ν•΄λ‹Ή μ›μ†Œ μΈλ±μŠ€μ— 0 μ‚½μž…
		}
		
		
		// λ‹΅ 좜λ ₯
		Collections.reverse(list);
		for(Integer i:list ) {
			sb.append(i).append(" ");
		}
		
		sb.deleteCharAt(sb.length() - 1); // λ§ˆμ§€λ§‰ 곡백 제거
		System.out.println(sb);
	}
}

πŸ“± Screenshot

image

πŸ“ Review Note

λ°±μ€€μ—μ„œ μ΅œμ’… μ œμΆœν•œ μ½”λ“œλŠ” prμž‘μ„±μ„ μœ„ν•΄ μ•„λž˜μ—μ„œ μ°Έκ³ ν•œ μ½”λ“œλ‘œ μ œμΆœν•œ κ²ƒμž…λ‹ˆλ‹€!

πŸ“¬ Reference

μ •λ‹΅ μ½”λ“œ μ°Έκ³  링크

@3o14 3o14 requested a review from a team as a code owner August 7, 2023 07:46
@3o14 3o14 requested review from chjcode and changbill and removed request for a team August 7, 2023 07:46
@minjae9610
Copy link
Member

아직 풀지 λͺ»ν•œ λ¬Έμ œμ— λŒ€ν•œ μ§ˆλ¬Έμ€ 이슈λ₯Ό ν™œμš©ν•˜μ…”λ„ 쒋을 것 κ°™μ•„μš”!

@minjae9610
Copy link
Member

μ‘°λ§Œκ°„ ν’€ 문제 λ¦¬μŠ€νŠΈμ— μžˆλŠ” λ¬Έμ œλ„€μš” :)
저도 ν’€κ³  ν•œλ²ˆ μ˜¬λ €λ“œλ¦΄κ»˜μš”

Copy link
Contributor

@chjcode chjcode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ œκ°€ μžλ°”λ₯Ό 곡뢀 쀑이라 잘 λͺ¨λ₯΄λŠ” 것일 수 도 μžˆμ§€λ§Œ, μ½”λ“œλ₯Ό μ‹€ν–‰μ‹œμΌ°μ„ λ•Œ 5 5 5 5 5 와 같이 μ€‘λ³΅λ˜λŠ” 값을 λ„£μ—ˆμ„ λ•Œ 였λ₯˜κ°€ λ‚¬μŠ΅λ‹ˆλ‹€. λ˜ν•œ λ³€μˆ˜λͺ…을 λ³΄λ‹ˆ stack을 μ‚¬μš©ν•˜λ € κ³„νš ν•˜μ‹  것 같은데 addFirst, poll을 μ‚¬μš©ν•˜μ‹  κ²ƒμœΌλ‘œ 보아 μ½”λ“œκ°€ stack 자료ꡬ쑰λ₯Ό ν™œμš©ν•˜μ§€ λͺ»ν•œ 것 κ°™μŠ΅λ‹ˆλ‹€.
쒋은 문제 μ˜¬λ €μ£Όμ‹  덕뢄에 저도 재밌게 ν’€μ–΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€!

@minjae9610 minjae9610 requested review from a team and CrimsonTheLegoBuilder and removed request for changbill and a team August 14, 2023 01:18
@3o14 3o14 merged commit 33954cb into main Aug 24, 2023
1 check passed
@3o14 3o14 deleted the algo/3o14/boj2493 branch August 24, 2023 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants