๋ฌธ์
ํ์ด
์ฒ์์๋ ๋ฐฐ์ด์ ๋์์๋ถํฐ ํ์์ ์์ํด์ ์์ ๋ณด๋ค ํฐ ๊ฐ์ ๋ง๋ ๋๊น์ง์ ๋ชจ๋ ์์๋ค์ ๊ฐ์ด ์ด๊ธฐํํ๋ ๋ฐฉ์์ ์๊ฐํ๋ค. ํ์ง๋ง ์ด ๊ฒฝ์ฐ์๋ ์๋ ๋ฐฉํฅ์ ํ์ดํ ๋ชจ์(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();
}
}
'๐ Algorithm > ๐ป ๋ฌธ์ ํ์ด' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 11053. ๊ฐ์ฅ ๊ธด ์ฆ๊ฐํ๋ ๋ถ๋ถ ์์ด (0) | 2021.02.10 |
---|---|
[๋ฐฑ์ค] 9663. N-Queens (0) | 2021.02.05 |
[๋ฐฑ์ค] 1436. ์ํ๊ฐ๋ ์ (0) | 2021.02.03 |
[๋ฐฑ์ค] 1018. ์ฒด์คํ ๋ค์ ์น ํ๊ธฐ (0) | 2021.02.02 |
[๋ฐฑ์ค] 2146. ๋ค๋ฆฌ ๋ง๋ค๊ธฐ (1) | 2021.02.02 |
๋๊ธ