백준 1083 nodejs

윤익·2022년 10월 26일
0

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

const fs = require('fs')
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n')

function swap(a, b) {
  ;[A[a], A[b]] = [A[b], A[a]]
  S--
}

function maxIndex(arr) {
  return arr.reduce((i, _, j) => (arr[i] < arr[j] ? j : i), 0)
} 
// 최댓값의 index 찾는 함수

const N = +input[0]
const A = input[1].split(' ').map(e => +e)
let [S, i] = [+input[2], 0]
while (S > 0 && i < N) {
  const maxI = i + maxIndex(A.slice(i, i + 1 + S))
  for (let j = maxI; j > i; j--) swap(j - 1, j)
  i++
}
// 남은 횟수 S를 기준으로 가져올 수 있는 최댓값을 찾아서 i번째 값까지 옮김
console.log(A.join(' '))
profile
https://nickyoon.tistory.com/ 기술 블로그 이전

0개의 댓글