Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions 0300-longest-increasing-subsequence/memo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
### step1

O(N ^ 2)の動的計画法しか思いつかなかった。
Copy link
Copy Markdown

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) のように思いました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そうですね、O(N2 log N) 掛かっていそうです。


### step2

ChatGPTに聞いて長さ i+1 の増加部分列の「末尾の最小値」をvectorでもっておけばいいことがわかった。
これでO(nlogn)になる。

### step3

3回通すまで書き直し。
19 changes: 19 additions & 0 deletions 0300-longest-increasing-subsequence/step1.cpp
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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map 型の変数については、 (キー)_to_(値) という命名をよく目にします。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The 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++) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ループ内で *it 以外で it を直接使用している箇所がないため、 ranged for 文で書いたほうがシンプルになると思います。

for (auto [val, length] : val_and_length) {

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The 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;
}
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map の代わりに nums と同じ要素数の vector でも同じことができると思いました。
vector を使うと時間計算量が O(N^2) になります。

18 changes: 18 additions & 0 deletions 0300-longest-increasing-subsequence/step2.cpp
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;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

val だけですと、中に含まれている値がどのようなものか、分かりにくいように感じました。自分なら、 length_to_min_tail_val とすると思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The 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();
}
};
18 changes: 18 additions & 0 deletions 0300-longest-increasing-subsequence/step3.cpp
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();
}
};