150強訪談中等

最長遞增子序列

“最長遞增子序列”問題的詳細指南和 Python 實作。

問題陳述

中等

給定一個整數數組 nums,傳回最長嚴格遞增子序列的長度。

寫一個函數 lengthOfLIS(nums: List[int]) -> int

約束條件
  • 1 <= len(nums) <= 2500
  • -10^4 <= nums[i] <= 10^4

範例

Example 1
Input
nums = [10,9,2,5,3,7,101,18]
Output
4
Explanation

The longest increasing subsequence is [2,3,7,101], therefore the length is 4.

Example 2
Input
nums = [0,1,0,3,2,3]
Output
4
Explanation

The longest increasing subsequence is [0,1,2,3], length is 4.

Example 3
Input
nums = [7,7,7,7,7,7,7]
Output
1
Explanation

The longest increasing subsequence is [7], length is 1.

Need a Hint?
考慮使用一維 DP 特定的資料結構,例如集合或堆。
Edge Cases to Watch
  • 空輸入結構
  • 單元素輸入
  • 大數值範圍

準備好解決了嗎?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在編輯器中開啟
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。