๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ”Ž Algorithm/๐Ÿ’ป ๋ฌธ์ œ ํ’€์ด

[๋ฐฑ์ค€] 2493. ํƒ‘

by Seongpyo Hong 2021. 2. 4.

๋ฌธ์ œ

 

ํ’€์ด

์ฒ˜์Œ์—๋Š” ๋ฐฐ์—ด์˜ ๋์—์„œ๋ถ€ํ„ฐ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•ด์„œ ์ž์‹ ๋ณด๋‹ค ํฐ ๊ฐ’์„ ๋งŒ๋‚  ๋•Œ๊นŒ์ง€์˜ ๋ชจ๋“  ์š”์†Œ๋“ค์„ ๊ฐ™์ด ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋ฐฉ์‹์„ ์ƒ๊ฐํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ๊ฒฝ์šฐ์—๋Š” ์•„๋ž˜ ๋ฐฉํ–ฅ์˜ ํ™”์‚ดํ‘œ ๋ชจ์–‘(ex. 5 2 1 3 4) ๊ฒฝ์šฐ๋ฅผ ์ปค๋ฒ„ํ•˜์ง€ ๋ชปํ•ด์„œ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด๋ดค๋‹ค.

์ตœ์ข…์ ์œผ๋กœ๋Š” ์Šคํƒ ๋‘๊ฐœ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•˜๋‚˜์˜ ์Šคํƒ(A)์€ ์ž…๋ ฅ ์ˆœ์„œ๋Œ€๋กœ ๋“ค์–ด๊ฐ€ ์žˆ๊ณ  ๋‚˜๋จธ์ง€(B)๋Š” ๊บผ๋‚ธ ๊ฐ’์„ ์ €์žฅํ–ˆ๋‹ค. ๊บผ๋‚ธ ๊ฐ’์„ B ์Šคํƒ์— ๋„ฃ๊ธฐ ์ „์— B์Šคํƒ์˜ top์ด ๋” ์ž‘์€ ๊ฒฝ์šฐ์—๋Š” ๊บผ๋‚ธ ๊ฐ’์˜ ์ธ๋ฑ์Šค๊ฐ€ top์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋œ๋‹ค. ์ธ๋ฑ์Šค๋ฅผ ์ฐพ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ๋Š” Map์„ ํ†ตํ•ด์„œ ์ดˆ๊ธฐ ์ž…๋ ฅ์‹œ ํ•ด๋‹น ๊ฐ’์„ key๋กœ ์ธ๋ฑ์Šค๋ฅผ value๋กœ ์ €์žฅํ•ด์„œ O(1)๋กœ ์ฐพ์„ ์ˆ˜ ์žˆ๋„๋ก ํ–ˆ๋‹ค.

Map์„ ํ†ตํ•œ ๊ตฌํ˜„

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Stack;

public class Main {
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	public static void main(String[] args) throws NumberFormatException, IOException {
		int N = Integer.parseInt(br.readLine());
		HashMap<Integer, Integer> idxMap = new HashMap<>();

		int[] result = new int[N];
		Stack<Integer> src = new Stack<>();
		Stack<Integer> ready = new Stack<>();
		
		String[] line = br.readLine().split(" ");
		for (int i = 0; i < line.length; i++) {
			int input = Integer.parseInt(line[i]);
			idxMap.put(input, i + 1);
			src.push(input);
		}
		
		while(!src.isEmpty()) {
			int top = src.pop();
			int topIdx = idxMap.get(top);
			while(!ready.isEmpty()) {
				if (top < ready.peek()) break;
				int idx = idxMap.get(ready.pop());
				result[idx-1] =  topIdx;
			}
			ready.push(top);
		}

		for (int i = 0; i < result.length; i++) {
			System.out.print(result[i] + " ");
		}
	}
}

Map์„ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋„ˆ๋ฌด ๋งŽ์ด ์‚ฌ์šฉํ•ด์„œ ์ค‘๋ณต์ €์žฅ์„ ์—†์• ๊ธฐ ์œ„ํ•ด ๊ฐ’๊ณผ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ™์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” Tower ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•ด์„œ ์‚ฌ์šฉํ–ˆ๊ณ , ๊ฒฐ๊ณผ๋กœ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด 1/2 ์ •๋„๋กœ ๊ฐ์†Œํ–ˆ๋‹ค.

package hw;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class SolutionHW {
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	
	static class Tower {
		int value;
		int idx;
		public Tower(int value, int idx) {
			this.value = value;
			this.idx = idx;
		}
		
		int getIdx() {
			return this.idx;
		}
		
		int getValue() {
			return this.value;
		}
	}
	public static void main(String[] args) throws NumberFormatException, IOException {
		int N = Integer.parseInt(br.readLine());

		int[] result = new int[N];
		Stack<Tower> src = new Stack<>();
		Stack<Tower> ready = new Stack<>();
		
		String[] line = br.readLine().split(" ");
		for (int i = 0; i < line.length; i++) {
			int input = Integer.parseInt(line[i]);
			src.push(new Tower(input, i+1));
		}
		
		while(!src.isEmpty()) {
			Tower top = src.pop();
			int topIdx = top.getIdx();
			while(!ready.isEmpty()) {
				if (top.getValue() < ready.peek().getValue()) break;
				int idx = ready.pop().getIdx();
				result[idx-1] =  topIdx;
			}
			ready.push(top);
		}

		for (int i = 0; i < result.length; i++) {
			System.out.print(result[i] + " ");
		}
	}
}

 

์œ„์˜ ํ’€์ด๋“ค์€ ์†Œ์š” ์‹œ๊ฐ„์ด ๋Œ€๋žต 3000ms์ •๋„ ์†Œ์š”๋˜๋Š”๋ฐ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ๊ฒฐ๊ณผ๋Š” 800ms ์ •๋„์—ฌ์„œ ๋” ๋‚˜์€ ๋กœ์ง์ด ์žˆ๋‚˜ ๊ณ ๋ฏผํ•ด๋ดค๋Š”๋ฐ, ์ถœ๋ ฅ์˜ ์ฐจ์ด์˜€๋‹ค... ์ถœ๋ ฅ์€ StringBuilder๋ฅผ ํ†ตํ•ด ํ•œ ๋ฒˆ์— ์ถœ๋ ฅํ•˜๋˜๊ฐ€ ์•„๋‹ˆ๋ฉด BufferedWrtier๋ฅผ ํ†ตํ•ด ์ถœ๋ ฅํ•ด์•ผ 2~3๋ฐฐ ์ •๋„ ๋น ๋ฅธ ์ˆ˜ํ–‰์‹œ๊ฐ„์„ ๊ฐ–๋Š”๋‹ค.

BufferedWriter ์‚ฌ์šฉ ๋ฒ„์ „

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Stack;

public class SolutionHW {
	static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	
	static class Tower {
		int value;
		int idx;
		public Tower(int value, int idx) {
			this.value = value;
			this.idx = idx;
		}
		
		int getIdx() {
			return this.idx;
		}
		
		int getValue() {
			return this.value;
		}
	}
	public static void main(String[] args) throws NumberFormatException, IOException {
		int N = Integer.parseInt(br.readLine());

		int[] result = new int[N];
		Stack<Tower> src = new Stack<>();
		Stack<Tower> ready = new Stack<>();
		
		String[] line = br.readLine().split(" ");
		for (int i = 0; i < line.length; i++) {
			int input = Integer.parseInt(line[i]);
			src.push(new Tower(input, i+1));
		}
		
		while(!src.isEmpty()) {
			Tower top = src.pop();
			int topIdx = top.getIdx();
			while(!ready.isEmpty()) {
				if (top.getValue() < ready.peek().getValue()) break;
				int idx = ready.pop().getIdx();
				result[idx-1] =  topIdx;
			}
			ready.push(top);
		}

		for (int i = 0; i < result.length; i++) {
			bw.write(result[i] + " ");
		}
		bw.flush();
		bw.close();
	}
}

๋Œ“๊ธ€