From d343f45f9a652b2351a01ca765280326904f0a46 Mon Sep 17 00:00:00 2001 From: rossy0213 Date: Tue, 21 May 2024 01:12:49 +0900 Subject: [PATCH] add: Longest Increasing Subsequence --- LongestIncreasingSubsequence/step1.md | 27 +++++++++++ LongestIncreasingSubsequence/step2.md | 30 ++++++++++++ LongestIncreasingSubsequence/step3.md | 70 +++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 LongestIncreasingSubsequence/step1.md create mode 100644 LongestIncreasingSubsequence/step2.md create mode 100644 LongestIncreasingSubsequence/step3.md diff --git a/LongestIncreasingSubsequence/step1.md b/LongestIncreasingSubsequence/step1.md new file mode 100644 index 0000000..c515ac6 --- /dev/null +++ b/LongestIncreasingSubsequence/step1.md @@ -0,0 +1,27 @@ +```java +// Create a memory to store the maxLength for each element +// In each step, check if there is a smaller number in the past +// If yes, then count up, then compare with the max number we already have +// Time complexity: O(N**2) +// Space complexity: O(N) memory of the dp. +// Time spend: 08:30 +class Solution { + public int lengthOfLIS(int[] nums) { + int[] dp = new int[nums.length]; + dp[0] = 1; + int maxLength = 1; + + for (int i = 1; i < nums.length; i++) { + dp[i] = 1; + for (int j = 0; j < i; j++) { + if (nums[j] < nums[i]) { + dp[i] = Math.max(dp[i], dp[j] + 1); + } + } + maxLength = Math.max(maxLength, dp[i]); + } + + return maxLength; + } +} +``` \ No newline at end of file diff --git a/LongestIncreasingSubsequence/step2.md b/LongestIncreasingSubsequence/step2.md new file mode 100644 index 0000000..339736f --- /dev/null +++ b/LongestIncreasingSubsequence/step2.md @@ -0,0 +1,30 @@ +```java +// After checking the solutions tab...What's an elegant solution +// Time complexity: O(NlogN) +// Time spend: 07:30 +class Solution { + public int lengthOfLIS(int[] nums) { + int[] tails = new int[nums.length]; + int maxLength = 0; + + for (int num : nums) { + int l = 0; + int r = maxLength; + while (l != r) { + int mid = l + (r - l) / 2; + if (tails[mid] < num) { + l = mid + 1; + continue; + } + r = mid; + } + tails[l] = num; + if (l == maxLength) { + maxLength++; + } + } + + return maxLength; + } +} +``` \ No newline at end of file diff --git a/LongestIncreasingSubsequence/step3.md b/LongestIncreasingSubsequence/step3.md new file mode 100644 index 0000000..984ca57 --- /dev/null +++ b/LongestIncreasingSubsequence/step3.md @@ -0,0 +1,70 @@ +```java +// Time spend: 04:20 +class Solution { + public int lengthOfLIS(int[] nums) { + List increasingNums = new ArrayList<>(); + increasingNums.add(nums[0]); + + for (int i = 1; i < nums.length; i++) { + if (nums[i] > increasingNums.get(increasingNums.size() - 1)) { + increasingNums.add(nums[i]); + continue; + } + int low = Collections.binarySearch(increasingNums, nums[i]); + if (low < 0) { + // bit否定? + low = -(low + 1); + } + increasingNums.set(low, nums[i]); + } + + return increasingNums.size(); + } +} +``` + +```java +// Time spend: 03:30 +class Solution { + public int lengthOfLIS(int[] nums) { + List increasingNums = new ArrayList<>(); + increasingNums.add(nums[0]); + + for (int i = 1; i < nums.length; i++) { + if (nums[i] > increasingNums.get(increasingNums.size() - 1)) { + increasingNums.add(nums[i]); + continue; + } + int index = Collections.binarySearch(increasingNums, nums[i]); + if (index < 0) { + increasingNums.set(-(index + 1), nums[i]); + } + } + + return increasingNums.size(); + } +} +``` + +```java +// Time spend: 02:20 +class Solution { + public int lengthOfLIS(int[] nums) { + List increasingNums = new ArrayList<>(); + increasingNums.add(nums[0]); + + for (int i = 0; i < nums.length; i++) { + if (nums[i] > increasingNums.get(increasingNums.size() - 1)) { + increasingNums.add(nums[i]); + continue; + } + int index = Collections.binarySearch(increasingNums, nums[i]); + if (index < 0) { + increasingNums.set(-index-1, nums[i]); + } + } + + return increasingNums.size(); + } +} +``` \ No newline at end of file