Open
Conversation
nodchip
reviewed
Feb 21, 2026
| if (nums.empty()) { | ||
| return 0; | ||
| } | ||
| vector<int> increasing_subsequence(1, nums.front()); |
There was a problem hiding this comment.
vector<int> increasing_subsequence = { 1 };のほうがシンプルに感じました。
Owner
Author
There was a problem hiding this comment.
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]) { |
There was a problem hiding this comment.
自分なら std::lower_bound() が nums.end() を返した場合は push_back() する、というコードを書くと思うのですが、趣味の範囲だと思います。
Owner
Author
There was a problem hiding this comment.
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で置き換える |
There was a problem hiding this comment.
nums の要素で new_num 以上の数のうち,最小のものをnew_numで置き換える
ではないでしょうか。
https://cpprefjp.github.io/reference/algorithm/lower_bound.html
イテレータ範囲 [first, last) のうち、指定された要素以上の値が現れる最初の位置のイテレータを取得する。
dxxsxsxkx
reviewed
Feb 21, 2026
| # 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]`が出力になるように書いてしまった. |
| return 0; | ||
| } | ||
| vector<int> increasing_subsequence(1, nums.front()); | ||
| int max_len = 1; |
| return 0; | ||
| } | ||
| vector<int> increasing_subsequence(1, nums.front()); | ||
| int max_len = 1; |
Owner
Author
There was a problem hiding this comment.
ご指摘ありがとうございます.
(最後にコードを通しで見直す癖をつけたいです...)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
問題: 300. Longest Increasing Subsequence