Skip to content

300. longest increasing subsequence#32

Open
5ky7 wants to merge 4 commits intomainfrom
300.-Longest-Increasing-Subsequence
Open

300. longest increasing subsequence#32
5ky7 wants to merge 4 commits intomainfrom
300.-Longest-Increasing-Subsequence

Conversation

@5ky7
Copy link
Copy Markdown
Owner

@5ky7 5ky7 commented Feb 21, 2026

if (nums.empty()) {
return 0;
}
vector<int> increasing_subsequence(1, nums.front());
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

vector<int> increasing_subsequence = { 1 };

のほうがシンプルに感じました。

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.

vector<int> increasing_subsequence = {nums[0]};

でしょうか.
確かに1要素ならこちらのがシンプルですね.

vector<int> increasing_subsequence(1, nums.front());
int max_len = 1;
for (int i = 1; i < nums.size(); ++i) {
if (increasing_subsequence.back() < nums[i]) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分なら std::lower_bound() が nums.end() を返した場合は push_back() する、というコードを書くと思うのですが、趣味の範囲だと思います。

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.

std::lower_bound() が nums.end() を返した場合は push_back() する

nums.end() -> increasing_subsequence.end()でしょうか.
確かに次のようにUpdateSubsequence()を分離せず,かつ効率的なコードが書けますね.

class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        if (nums.empty()) {
            return 0;
        }
        vector<int> increasing_subsequence(1, nums.front());
        for (int i = 1; i < nums.size(); ++i) {
            auto it = std::lower_bound(increasing_subsequence.begin(), increasing_subsequence.end(), nums[i]);
            if (it == increasing_subsequence.end()) {
                increasing_subsequence.push_back(nums[i]);
                continue;
            }
            *it = nums[i];
        }
        return increasing_subsequence.size();
    }
};


private:
void UpdateSubsequence(int new_num, vector<int>& nums) {
// numsの要素でnew_numより小さい数のうち,最大のものをnew_numで置き換える
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nums の要素で new_num 以上の数のうち,最小のものをnew_numで置き換える
ではないでしょうか。

https://cpprefjp.github.io/reference/algorithm/lower_bound.html

イテレータ範囲 [first, last) のうち、指定された要素以上の値が現れる最初の位置のイテレータを取得する。

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.

勘違いしていました.ありがとうございます!

Copy link
Copy Markdown

@dxxsxsxkx dxxsxsxkx left a comment

Choose a reason for hiding this comment

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

概ね良いと思いました。

# Step 1
* 過去に解いたものが[Code1](#Code1).
* 時間が経ったので解き直してみたのが[Code2](#Code2).所要時間28分.時間計算量O(n^2), 空間計算量O(1).
* 一旦「連続な部分列」だと勘違いして解いた.つまり入力`[10,9,2,5,3,7,101,18]`に対して`[2, 3, 7, 101]`でなくて`[3, 7, 101]`が出力になるように書いてしまった.
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

初見の時に同じ勘違いをしました...

return 0;
}
vector<int> increasing_subsequence(1, nums.front());
int max_len = 1;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

max_len は他の箇所で使われていないように思います。

return 0;
}
vector<int> increasing_subsequence(1, nums.front());
int max_len = 1;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

max_len は他の箇所で使われていないように思います。

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.

ご指摘ありがとうございます.
(最後にコードを通しで見直す癖をつけたいです...)

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