-
Notifications
You must be signed in to change notification settings - Fork 0
add: Longest Increasing Subsequence #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rossy0213
wants to merge
1
commit into
main
Choose a base branch
from
arai60-31
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| ```java | ||
| // Time spend: 04:20 | ||
| class Solution { | ||
| public int lengthOfLIS(int[] nums) { | ||
| List<Integer> 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<Integer> 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<Integer> 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]); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -index-1 は、~index でしたっけ?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. はい。bit演算普段あまりみないので、このままにしました。 |
||
| } | ||
| } | ||
|
|
||
| return increasingNums.size(); | ||
| } | ||
| } | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
長らく触ってないのでJavaの常識があまりわからないのですが、varを使った方がわかりやすい気がしました。しかしArrayListまで絞らずListにしておいた方が良いという考え方もありそうですね。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
実際に業務で使ってる人に質問になるのですが、業務で型推論は推奨になっていますか?
(個人的には型推論利用したい派はあるが、諸事情で使っていません)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://chromium.googlesource.com/chromium/src/+/main/styleguide/java/java.md#var
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
なるほど、右側だけで型が明確である場合、
varの方が好ましいのよくわかります。今回の場合、Listインタフェースを利用したいというのがあって、明示的に書くしかなさそうです。