Skip to content

300. Longest Increasing Subsequence#34

Merged
Ryotaro25 merged 3 commits intomainfrom
problem31
Jul 1, 2025
Merged

300. Longest Increasing Subsequence#34
Ryotaro25 merged 3 commits intomainfrom
problem31

Conversation

@Ryotaro25
Copy link
Copy Markdown
Owner

問題へのリンク
https://leetcode.com/problems/longest-increasing-subsequence/description/

問題文(プレミアムの場合)

備考

次に解く問題の予告
Maximum Subarray

フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cpp、binary_search.cppとmemo.mdとなります。

memo.md内に各ステップで感じたことを追記します。

public:
int lengthOfLIS(vector<int>& nums) {
// least increasing order
vector<int> lis;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

https://github.com/Yoshiki-Iwasa/Arai60/pull/46/files/56e8cf4d4efc42c5784108191d1e5fc615de9206#r1716128766
他のPRでたしかに、と思う議論があったので貼っておきます。厳密にはこれはLISではないという話です

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.

@fhiyo
レビューありがとうございます。

たとえばnums = [10, 11, 12, 1, 2]を与えると、lisは[1, 2, 12]になると思います。実際には[1, 2, 12]という部分列を取ることはできません。
日本語でいうと、「長さ index の IS の末尾の最小値」なので、end_minimums_of_is とかですかね。難しいですね。

この辺りですね。確かに中身を追っていくと一致しないので命名難しいです。

int max_length = 0;
for (int i = 0; i < nums.size(); i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

iとjの位置関係が分かりにくく感じました。 nums[j] < nums[i] の書き方の方が j < i であることが分かりやすい気がします。

思いついたのは愚直にループを回しながら頭からみていく方法と
頭から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.

この解法はDPのテーブルを埋めているだけでメモ化ではないような気がします。

https://en.wikipedia.org/wiki/Memoization

In computing, memoization or memoisation is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls to pure functions and returning the cached result when the same inputs occur again.
(強調引用者)

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.

@fhiyo

longest_lengths[j] + 1の部分が、過去に入力されたデータを使っているイメージだったのですが再帰呼び出しの関数の結果を保存していることなのですね。誤って認識しておりました🙇


こちらも二分探索
他にもBitとセグメント木というものがある(名前を聞いたことがあるようなないような。。。)
ぱっと見「アルゴリズムイントロダクション」に載っていない?
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ソフトウェアエンジニアの常識から微妙に外れていると思います。知っている人は多いけれども、知らなくても別に動揺されないものです。
つまり、これを使ってできる高速化はできなくても構わないのですが、しかし、考える時にこれも知っていると、セグメントツリーを使わない方法を思いついたりするので、知っていてもいいかもしれません。

@Ryotaro25 Ryotaro25 merged commit f106dbc into main Jul 1, 2025
1 check passed
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