|
| 1 | +- 문제: https://leetcode.com/problems/longest-increasing-subsequence/ |
| 2 | +- 풀이: https://algorithm.jonghoonpark.com/2024/02/27/leetcode-300 |
| 3 | + |
| 4 | +Beats 62.23% |
| 5 | + |
| 6 | +```java |
| 7 | +class Solution { |
| 8 | + public int lengthOfLIS(int[] nums) { |
| 9 | + int[] dp = new int[nums.length]; |
| 10 | + Arrays.fill(dp, 1); |
| 11 | + |
| 12 | + int max = 1; |
| 13 | + for (int i = 1; i < nums.length; i++) { |
| 14 | + for (int j = 0; j < i; j++) { |
| 15 | + if (nums[j] < nums[i]) { |
| 16 | + dp[i] = Math.max(dp[j] + 1, dp[i]); |
| 17 | + } |
| 18 | + } |
| 19 | + max = Math.max(max, dp[i]); |
| 20 | + } |
| 21 | + return max; |
| 22 | + } |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +### TC, SC |
| 27 | + |
| 28 | +시간복잡도는 O(n^2), 공간복잡도는 O(n)이다. |
| 29 | + |
| 30 | +## Follow up 문제 |
| 31 | + |
| 32 | +> Follow up: Can you come up with an algorithm that runs in O(n log(n)) time complexity? |
| 33 | +
|
| 34 | +파이썬 에서는 bisect_left 를 쓰면 된다고 하지만 자바에는 존재하지 않는다. 하지만 방법을 찾아보자. |
| 35 | + |
| 36 | +### dp 를 사용하지 않은 풀이 (Beats 82.20%) |
| 37 | + |
| 38 | +우선은 ArrayList를 이용하여 비슷하게 모방해보았다. |
| 39 | + |
| 40 | +```java |
| 41 | +public int lengthOfLIS(int[] nums) { |
| 42 | + ArrayList<Integer> subsequence = new ArrayList<>(); |
| 43 | + subsequence.add(nums[0]); |
| 44 | + |
| 45 | + for (int i = 1; i < nums.length; i++) { |
| 46 | + int current = nums[i]; |
| 47 | + |
| 48 | + if(current > subsequence.get(subsequence.size() - 1)) { |
| 49 | + subsequence.addLast(current); |
| 50 | + } else if (current < subsequence.get(0)) { |
| 51 | + subsequence.set(0, current); |
| 52 | + } else { |
| 53 | + for (int j = 1; j < subsequence.size(); j++) { |
| 54 | + if(current > subsequence.get(j - 1) && current < subsequence.get(j)) { |
| 55 | + subsequence.set(j, current); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return subsequence.size(); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +#### TC, SC |
| 66 | + |
| 67 | +아직은 여전히 시간복잡도는 O(n^2), 공간복잡도는 O(n)이다. |
| 68 | +빅오 표기법 상으로는 동일하나 실제 동작 시간은 감소하였다. |
| 69 | + |
| 70 | +### 진짜 binary search를 도입해보기 (Beats 92.57%) |
| 71 | + |
| 72 | +자바 Collections 에서는 binarySearch 메소드를 제공해준다. |
| 73 | +적용해보면 다음과 같다. |
| 74 | + |
| 75 | +```java |
| 76 | +public int lengthOfLIS(int[] nums) { |
| 77 | + ArrayList<Integer> subsequence = new ArrayList<>(); |
| 78 | + |
| 79 | + for (int current : nums) { |
| 80 | + // Collections.binarySearch : 목록에 포함된 경우 검색 키의 인덱스, 그렇지 않으면 (-(삽입점) - 1) 을 반환함. |
| 81 | + int pos = Collections.binarySearch(subsequence, current); |
| 82 | + if (pos < 0) pos = -(pos + 1); |
| 83 | + if (pos >= subsequence.size()) { |
| 84 | + subsequence.add(current); |
| 85 | + } else { |
| 86 | + subsequence.set(pos, current); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return subsequence.size(); |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +#### Collections.binarySearch |
| 95 | + |
| 96 | +해당 메소드의 리턴값은 다음과 같다. |
| 97 | + |
| 98 | +> 목록에 포함된 경우 검색 키의 인덱스, 그렇지 않으면 (-(삽입점) - 1). |
| 99 | +> 삽입 지점은 키가 목록에 삽입되는 지점, 즉 키보다 큰 첫 번째 요소의 인덱스 또는 목록의 모든 요소가 지정된 키보다 작은 경우 list.size()로 정의됩니다. |
| 100 | +> 키가 발견되는 경우에만 반환값이 >= 0이 되도록 보장합니다. |
| 101 | +
|
| 102 | +#### TC, SC |
| 103 | + |
| 104 | +시간복잡도는 `O(n * logn)`, 공간복잡도는 `O(n)`이다. |
0 commit comments