알고리즘 : LIS (이분탐색)
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const n = Number(input[0]);
const stones = input[1].split(' ').map(Number);
function bisect_left(arr, target) {
let left = 0;
let right = arr.length;
while (left < right){
const mid = Math.floor((left+ right)/ 2)
if (arr[mid] < target){
left = mid + 1
}else {
right = mid
}
}
return left
}
const dp = [0];
for (let i = 0; i < n; i++) {
const idx = bisect_left(dp, stones[i]);
if (idx === dp.length) {
dp.push(stones[i]);
} else {
dp[idx] = stones[i];
}
}
console.log(dp.length - 1);
LIS 알고리즘을 알고 있다면 간단한 문제
'Coding test' 카테고리의 다른 글
[백준/1422/파이썬] 숫자의 신 (1) | 2024.12.27 |
---|---|
[백준/ 파이썬/ 21608] 상어 초등학교 (0) | 2024.12.27 |
99클럽 코테 스터디 42일차 TIL, 프로그래머스 / 코딩테스트공부 (4) | 2024.09.01 |
99클럽 코테 스터디 41일차 TIL, 프로그래머스 / 도둑질 (0) | 2024.08.31 |
99클럽 코테 스터디 40일차 TIL, 프로그래머스 / 등굣길 (0) | 2024.08.30 |