Skip to content

300. Longest Increasing Subsequence#29

Open
hemispherium wants to merge 1 commit into
mainfrom
0300-longest-increasing-subsequence
Open

300. Longest Increasing Subsequence#29
hemispherium wants to merge 1 commit into
mainfrom
0300-longest-increasing-subsequence

Conversation

@hemispherium
Copy link
Copy Markdown
Owner

@hemispherium hemispherium self-assigned this Apr 21, 2026
@hemispherium hemispherium changed the title https://leetcode.com/problems/longest-increasing-subsequence/ 300. Longest Increasing Subsequence Apr 21, 2026
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.

そうですね、そちらの方がシンプルそうです。ご指摘ありがとうございます。

@@ -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) 掛かっていそうです。

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.

そうですね、そちらの方がわかりやすそうです。

}
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) になります。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants