150強訪談

最大子數組

“最大子数组”问题的详细指南和 Python 实现。

問題陳述

给定一个整数数组 nums,找到总和最大的连续子数组(至少包含一个数字)并返回其总和。

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

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

範例

Example 1
Input
nums = [-2,1,-3,4,-1,2,1,-5,4]
Output
6
Explanation

[4,-1,2,1] has the largest sum = 6.

Example 2
Input
nums = [1]
Output
1
Explanation

[1] has the largest sum = 1.

Need a Hint?
考慮使用特定於貪婪的資料結構,例如集合或堆疊。
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 資源

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