-
Notifications
You must be signed in to change notification settings - Fork 0
300. Longest Increasing Subsequence #31
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
potrue
wants to merge
2
commits into
main
Choose a base branch
from
300
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,104 @@ | ||
| ## 何も見ずに解いてみる | ||
|
|
||
| 後半30問はC++で解いてみようと思います。 | ||
| まだC++はあまりわかっていないのでいろいろとLLMに聞きながら書きました。 | ||
|
|
||
| ```cpp | ||
| #include <unordered_map> | ||
| #include <limits> | ||
|
|
||
| class Solution { | ||
| public: | ||
| int lengthOfLIS(vector<int>& nums) { | ||
| std::unordered_map<int, int> last_num_to_max_length; | ||
| int min_value = std::numeric_limits<int>::min(); | ||
| last_num_to_max_length[min_value] = 0; | ||
| for (auto num : nums) { | ||
| int max_num = 0; | ||
| for (auto [last_num, max_length] : last_num_to_max_length) { | ||
| if (last_num < num) { | ||
| max_num = std::max(max_num, max_length + 1); | ||
| } | ||
| } | ||
| last_num_to_max_length[num] = max_num; | ||
| } | ||
| auto max_it = std::max_element(last_num_to_max_length.begin(), last_num_to_max_length.end(), | ||
| [](const auto& a, const auto& b) { | ||
| return a.second < b.second; | ||
| } | ||
| ); | ||
| return max_it->second; | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| ### Follow up: Can you come up with an algorithm that runs in O(n log(n)) time complexity? | ||
| (末尾の数字, それで実現できる最長の長さ) のtupleみたいなものをlistとしてどちらの要素に関してもincreasingになるように保存して、二分探索でindexを見つけて更新していくことでできるのではないかと思ったが、 | ||
| 更新の際に (今見ている数字, それより小さい数字の最長の長さ + 1) をinsertして、次に大きい数字と同じ文字列長だったらそっちを消す、 みたいなことをやる必要があり、それに一回あたりO(n)かかって結局O(n^2)かかってしまいそう | ||
|
|
||
| ## 色々調べてみる | ||
|
|
||
| https://github.com/irohafternoon/LeetCode/pull/34/files | ||
| https://github.com/Miyamoto-tryk/leetcode-arai60/pull/4/files | ||
| https://github.com/Ryotaro25/leetcode_first60/pull/34/files | ||
| https://github.com/tokuhirat/LeetCode/pull/31/files | ||
| https://github.com/Satorien/LeetCode/pull/31/files | ||
|
|
||
| https://cpprefjp.github.io/reference/vector/vector.html | ||
| https://cpprefjp.github.io/reference/utility/pair.html | ||
| https://cpprefjp.github.io/reference/algorithm/lower_bound.html | ||
|
|
||
| O(n^2)のDPの解法ではhashmapではなくvectorを使っている人が多かった。個人的には辞書の方が直感的な気もする。 | ||
|
|
||
| 実現しうる文字列長以下のすべての数字について末尾にできる最小の数字が存在するので、文字列長をindexとする配列で二分探索を行って、随時indexを更新していけばよい。 | ||
| あるいは自分が考えていたようにtupleのlistを作っても、次に大きい数字のtupleを直接更新すればできそう。 | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| int lengthOfLIS(vector<int>& nums) { | ||
| vector<std::pair<int, int>> tail_len_pairs; | ||
| for (const auto& num : nums) { | ||
| auto it = std::lower_bound(tail_len_pairs.begin(), tail_len_pairs.end(), num, | ||
| [](const std::pair<int, int>& p, const int val) { | ||
| return p.first < val; | ||
| }); | ||
| if (it == tail_len_pairs.end()) { | ||
| tail_len_pairs.push_back({num, tail_len_pairs.size() + 1}); | ||
| } else { | ||
| it->first = num; | ||
| } | ||
| } | ||
| return tail_len_pairs.back().second; | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| 普通にやったほうがわかりやすそうか。 | ||
|
|
||
| ## 最終コード | ||
|
|
||
| 変数名の決め方が難しい。。 | ||
|
|
||
| ```cpp | ||
| #include <vector> | ||
| #include <algorithm> | ||
| using namespace std; | ||
|
|
||
| class Solution { | ||
| public: | ||
| int lengthOfLIS(vector<int>& nums) { | ||
| vector<int> minimum_tails; | ||
| for (const auto& num : nums) { | ||
| auto it = lower_bound(minimum_tails.begin(), minimum_tails.end(), num); | ||
| if (it == minimum_tails.end()) { | ||
| minimum_tails.push_back(num); | ||
| } else { | ||
| *it = num; | ||
| } | ||
| } | ||
| return minimum_tails.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.
length_to_min_last_value という変数名をご提案いただいたことがありました。あとは変数の意味をコメントで補足するのもありかなと思いました。
tokuhirat/LeetCode#31 (comment)