Notice
Recent Posts
Recent Comments
Link
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Lennon FE

[๋ฐฑ์ค€ 17298๋ฒˆ] ์˜คํฐ์ˆ˜ - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(nodejs) ๋ณธ๋ฌธ

๐Ÿ”ฅ Algorithm/Baekjoon

[๋ฐฑ์ค€ 17298๋ฒˆ] ์˜คํฐ์ˆ˜ - ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ(nodejs)

Lennon 2022. 1. 21. 17:21
728x90
๋ฐ˜์‘ํ˜•

https://www.acmicpc.net/problem/17298

 

17298๋ฒˆ: ์˜คํฐ์ˆ˜

์ฒซ์งธ ์ค„์— ์ˆ˜์—ด A์˜ ํฌ๊ธฐ N (1 ≤ N ≤ 1,000,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์— ์ˆ˜์—ด A์˜ ์›์†Œ A1, A2, ..., AN (1 ≤ Ai ≤ 1,000,000)์ด ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net

const fs = require('fs');
let input = fs.readFileSync('dev/stdin').toString().trim().split('\n');
input.shift();
input = input[0].split(' ').map((v) => +v);

let arr = [];
let answer = new Array(input.length).fill(-1);

for (let i = 0; i < input.length; i++) {
  while (arr.length && input[arr[arr.length - 1]] < input[i]) {
    answer[arr.pop()] = input[i];
  }
  arr.push(i);
}
console.log(answer.join(' '));

์šฐ์„  input ๊ธธ์ด๋งŒํผ -1์„ ์ฑ„์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค.

 

๊ทธ ํ›„ ๋‹จ์ถ•ํ‰๊ฐ€๋ฅผ ํ†ตํ•ด arr๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ 0์ด ์•„๋‹Œ ๊ฐ’์— ๋Œ€ํ•ด input[i]๊ฐ’๊ณผ input[arr[arr.length-1]]์˜ ๊ฐ’์„ ๋น„๊ตํ•œ๋‹ค.

์ฆ‰ ์™ผ์ชฝ ๊ฐ’๊ณผ ๋น„๊ตํ•ด์„œ ์ž์‹ ๋ณด๋‹ค ํฐ ๊ฐ’์„ ์ฐพ๋Š” ๊ฒƒ์ด๋‹ค. 

 

3,4,6,7์„ ์˜ˆ๋กœ ๋“ค์–ด๋ณด์ž.

input = [3,5,2,7]

answer = [-1, -1, -1, -1]

arr = [ ]

 

i = 0       // ์œ„ while๋ฌธ 0๋ฒˆ ์ˆœํšŒ

answer = [-1, -1, -1, -1]

arr = [0]

 

i = 1        // ์œ„ while๋ฌธ 1๋ฒˆ ์ˆœํšŒ

answer = [ 5, -1, -1, -1 ]
arr = [ 1 ]

 

i = 2      // ์œ„ while๋ฌธ 0๋ฒˆ ์ˆœํšŒ

answer = [ 5, -1, -1, -1 ]
arr = [ 1 , 2]

 

i = 3     // ์œ„ while๋ฌธ 2๋ฒˆ ์ˆœํšŒ

answer = [ 5, 7, 7, -1 ]
arr = [3]

 

์ฝ”๋“œ์™€ ํ•จ๊ป˜ ๋ณด๋ฉด ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

728x90
๋ฐ˜์‘ํ˜•
Comments