diff --git a/Algorithm/LIS (Longest Increasing Sequence).md b/Algorithm/LIS (Longest Increasing Sequence).md index 4c19862b..9e62d84d 100644 --- a/Algorithm/LIS (Longest Increasing Sequence).md +++ b/Algorithm/LIS (Longest Increasing Sequence).md @@ -21,15 +21,19 @@ int dp[] = new int[arr.length]; // LIS 저장 배열 for(int i = 1; i < dp.length; i++) { - if(dp[i] == 0) dp[i] = 1; for(int j = i-1; j>=0; j--) { - if(arr[i] > arr[i-1]) { + if(arr[i] > arr[j]) { dp[i] = (dp[i] < dp[j]+1) ? dp[j]+1 : dp[i]; } } } -// 저장된 dp 배열 값 : [0, 1, 2, 3, 1, 4] -// LIS : dp배열에 저장된 값 중 최대 값 + +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 ```
diff --git a/README.md b/README.md index 98c23ace..683c3d6a 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ - [머지소트](https://github.com/kim6394/Dev_BasicKnowledge/blob/master/Algorithm/MergeSort.md) - [힙소트](https://github.com/kim6394/Dev_BasicKnowledge/blob/master/Algorithm/HeapSort.md) - [해시 테이블 구현]() +- [최장 증가 수열(LIS)]([https://github.com/kim6394/Dev_BasicKnowledge/blob/master/Algorithm/LIS%20(Longest%20Increasing%20Sequence).md](https://github.com/kim6394/Dev_BasicKnowledge/blob/master/Algorithm/LIS (Longest Increasing Sequence).md)
diff --git a/TIL/19.08.07.md b/TIL/19.08.07.md index 0f0fbf5e..3b0edc57 100644 --- a/TIL/19.08.07.md +++ b/TIL/19.08.07.md @@ -23,15 +23,19 @@ int dp[] = new int[arr.length]; // LIS 저장 배열 for(int i = 1; i < dp.length; i++) { - if(dp[i] == 0) dp[i] = 1; for(int j = i-1; j>=0; j--) { - if(arr[i] > arr[i-1]) { + if(arr[i] > arr[j]) { dp[i] = (dp[i] < dp[j]+1) ? dp[j]+1 : dp[i]; } } } -// 저장된 dp 배열 값 : [0, 1, 2, 3, 1, 4] -// LIS : dp배열에 저장된 값 중 최대 값 + +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 ```