-
Notifications
You must be signed in to change notification settings - Fork 0
300. Longest Increasing Subsequence #29
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| ### step1 | ||
|
|
||
| O(N ^ 2)の動的計画法しか思いつかなかった。 | ||
|
|
||
| ### step2 | ||
|
|
||
| ChatGPTに聞いて長さ i+1 の増加部分列の「末尾の最小値」をvectorでもっておけばいいことがわかった。 | ||
| これでO(nlogn)になる。 | ||
|
|
||
| ### step3 | ||
|
|
||
| 3回通すまで書き直し。 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| class Solution { | ||
| public: | ||
| int lengthOfLIS(vector<int>& nums) { | ||
| map<int, int> val_and_length; | ||
|
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. map 型の変数については、 (キー)_to_(値) という命名をよく目にします。
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. そうですね、andよりもtoの方がよさそうです。 |
||
|
|
||
| int max_length = 1; | ||
| for (int i = 0; i < nums.size(); i++) { | ||
| val_and_length[nums[i]] = max(1, val_and_length[nums[i]]); | ||
| for (auto it = val_and_length.begin(); it != val_and_length.end(); it++) { | ||
|
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. ループ内で *it 以外で it を直接使用している箇所がないため、 ranged for 文で書いたほうがシンプルになると思います。 for (auto [val, length] : val_and_length) {
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. そうですね、そちらの方がシンプルそうです。ご指摘ありがとうございます。 |
||
| auto [val, length] = *it; | ||
| if (nums[i] > val) { | ||
| val_and_length[nums[i]] = max(length + 1, val_and_length[nums[i]]); | ||
| max_length = max(max_length, val_and_length[nums[i]]); | ||
| } | ||
| } | ||
| } | ||
| return max_length; | ||
| } | ||
| }; | ||
|
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. map の代わりに nums と同じ要素数の vector でも同じことができると思いました。 |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| class Solution { | ||
| public: | ||
| int lengthOfLIS(vector<int>& nums) { | ||
| vector<int> length_and_val; | ||
|
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. val だけですと、中に含まれている値がどのようなものか、分かりにくいように感じました。自分なら、 length_to_min_tail_val とすると思います。
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. そうですね、そちらの方がわかりやすそうです。 |
||
|
|
||
| for (int x : nums) { | ||
| auto it = lower_bound(length_and_val.begin(), length_and_val.end(), x); | ||
|
|
||
| if (it == length_and_val.end()) { | ||
| length_and_val.push_back(x); | ||
| } else { | ||
| *it = x; | ||
| } | ||
| } | ||
|
|
||
| return length_and_val.size(); | ||
| } | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| class Solution { | ||
| public: | ||
| int lengthOfLIS(vector<int>& nums) { | ||
| vector<int> length_and_val; | ||
|
|
||
| for (int x : nums) { | ||
| auto it = lower_bound(length_and_val.begin(), length_and_val.end(), x); | ||
|
|
||
| if (it == length_and_val.end()) { | ||
| length_and_val.push_back(x); | ||
| } else { | ||
| *it = x; | ||
| } | ||
| } | ||
|
|
||
| return length_and_val.size(); | ||
| } | ||
| }; |
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.
ループの最内周で map にアクセスしているため、 O(N2 log N) のように思いました。
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.
そうですね、O(N2 log N) 掛かっていそうです。