Conversation
| public: | ||
| int lengthOfLIS(vector<int>& nums) { | ||
| // least increasing order | ||
| vector<int> lis; |
There was a problem hiding this comment.
https://github.com/Yoshiki-Iwasa/Arai60/pull/46/files/56e8cf4d4efc42c5784108191d1e5fc615de9206#r1716128766
他のPRでたしかに、と思う議論があったので貼っておきます。厳密にはこれはLISではないという話です
There was a problem hiding this comment.
@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]) { |
There was a problem hiding this comment.
iとjの位置関係が分かりにくく感じました。 nums[j] < nums[i] の書き方の方が j < i であることが分かりやすい気がします。
| 思いついたのは愚直にループを回しながら頭からみていく方法と | ||
| 頭から1歩ずつ進んでいき、地点ごとの最大距離をメモ化する | ||
|
|
||
| メモ化のロジックを、形で覚えてしまっている感がある |
There was a problem hiding this comment.
この解法は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.
(強調引用者)
There was a problem hiding this comment.
longest_lengths[j] + 1の部分が、過去に入力されたデータを使っているイメージだったのですが再帰呼び出しの関数の結果を保存していることなのですね。誤って認識しておりました🙇
|
|
||
| こちらも二分探索 | ||
| 他にもBitとセグメント木というものがある(名前を聞いたことがあるようなないような。。。) | ||
| ぱっと見「アルゴリズムイントロダクション」に載っていない? |
There was a problem hiding this comment.
ソフトウェアエンジニアの常識から微妙に外れていると思います。知っている人は多いけれども、知らなくても別に動揺されないものです。
つまり、これを使ってできる高速化はできなくても構わないのですが、しかし、考える時にこれも知っていると、セグメントツリーを使わない方法を思いついたりするので、知っていてもいいかもしれません。
問題へのリンク
https://leetcode.com/problems/longest-increasing-subsequence/description/
問題文(プレミアムの場合)
備考
次に解く問題の予告
Maximum Subarray
フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cpp、binary_search.cppとmemo.mdとなります。
memo.md内に各ステップで感じたことを追記します。