スポンサーリンク

【LeetCode】300. Longest Increasing Subsequence 解答・解説【Python】

スポンサーリンク
スポンサーリンク
この記事は約2分で読めます。

 

問題

原文

Given an integer array nums, return the length of the longest strictly increasing 

.

 

Example 1:

Example 2:

Example 3:

 

Constraints:

  • 1 <= nums.length <= 2500
  • -104 <= nums[i] <= 104

 

Follow up: Can you come up with an algorithm that runs in O(n log(n)) time complexity?

 

内容

整数配列numsが与えられるので、厳密に増加する最長の部分配列を返してください。

※部分配列は各要素が連続していなくても良い

 

※正しくない可能性があります。

 

解答

解答1:Python, DP

 

time:O(n^2)

space:O(n)

 

解答2:Python, DP + Binary Search

 

time:O(nlog(n))

space:O(n)

bisectを使うと通るが、自分で二分探索を書いて実行するとエラーになるケースがあり未解決。

dp配列内でどこに要素を入れるかを探索するときに二分探索を使うことで、時間計算量がO(n^2)からO(log(n))に改善

問題文よりdp配列は昇順となるように要素が追加される実装になるので、もともとソートされている。

 

 

 

 

メモ・参考・感想

 

 

 

前:413. Arithmetic Slices

次:2351. First Letter to Appear Twice

LeetCode 解答・解説記事一覧

コメント