Skip to content

Latest commit

ย 

History

History
44 lines (28 loc) ยท 944 Bytes

LIS (Longest Increasing Sequence).md

File metadata and controls

44 lines (28 loc) ยท 944 Bytes

LIS (Longest Increasing Sequence)

์ตœ์žฅ ์ฆ๊ฐ€ ์ˆ˜์—ด : ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด

[ 7, 2, 3, 8, 4, 5 ] โ†’ ํ•ด๋‹น ๋ฐฐ์—ด์—์„œ๋Š” [2,3,4,5]๊ฐ€ LIS๋กœ ๋‹ต์€ 4


๊ตฌํ˜„ ๋ฐฉ๋ฒ• (์‹œ๊ฐ„๋ณต์žก๋„)
  1. DP : O(N^2)
  2. Lower Bound : O(NlogN)

DP ํ™œ์šฉ ์ฝ”๋“œ
int arr[] = {7, 2, 3, 8, 4, 5};
int dp[] = new int[arr.length]; // LIS ์ €์žฅ ๋ฐฐ์—ด


for(int i = 1; i < dp.length; i++) {
    for(int j = i-1; j>=0; j--) {
        if(arr[i] > arr[j]) {
            dp[i] = (dp[i] < dp[j]+1) ? dp[j]+1 : dp[i];
        }
    }
}

for (int i = 0; i < dp.length; i++) {
	if(max < dp[i]) max = dp[i];
}

// ์ €์žฅ๋œ dp ๋ฐฐ์—ด ๊ฐ’ : [0, 0, 1, 2, 2, 3]
// LIS : dp๋ฐฐ์—ด์— ์ €์žฅ๋œ ๊ฐ’ ์ค‘ ์ตœ๋Œ€ ๊ฐ’ + 1

ํ•˜์ง€๋งŒ, N^2์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์—†๋Š” ๋ฌธ์ œ๋ผ๋ฉด? (ex. ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ์ตœ๋Œ€ 10๋งŒ์ผ ๋•Œ..)

์ด๋•Œ๋Š” Lower Bound๋ฅผ ํ™œ์šฉํ•œ LIS ๊ตฌํ˜„์„ ์ง„ํ–‰ํ•ด์•ผํ•œ๋‹ค.