We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 683470e commit 7f07c71Copy full SHA for 7f07c71
1 file changed
live11/test115/문제2/조진우.js
@@ -0,0 +1,28 @@
1
+const input = require("fs")
2
+ .readFileSync(
3
+ process.platform === "linux"
4
+ ? "/dev/stdin"
5
+ : require("path").join(__dirname, "input.txt"),
6
+ "utf8"
7
+ )
8
+ .trim()
9
+ .split("\n");
10
+
11
+function solution(input) {
12
+ const N = +input[0];
13
+ const A = input[1].split(" ").map(Number);
14
+ const answer = new Array(N).fill(-1);
15
+ const stack = [];
16
17
+ for (let i = N - 1; i >= 0; i--) {
18
+ while (stack.length && stack[stack.length - 1] <= A[i]) stack.pop();
19
20
+ if (stack.length) answer[i] = stack[stack.length - 1];
21
22
+ stack.push(A[i]);
23
+ }
24
25
+ return answer.join(" ");
26
+}
27
28
+console.log(solution(input));
0 commit comments