leecode_kadanesAlgorithm.py
python_old/leecode_kadanesAlgorithm.py
def max_subarray_sum(nums):
max_so_far = float('-inf')
max_ending_here = 0
start = end = temp_start = 0
for i, num in enumerate(nums):
max_ending_here += num
if max_ending_here > max_so_far:
max_so_far = max_ending_here
start = temp_start
end = i
if max_ending_here < 0:
max_ending_here = 0
temp_start = i + 1
return max_so_far, nums[start:end+1]
# Example usage
nums = [-2, 9, -3, 2, -4, -2, 1, 2, 12, -3]
max_sum, subarray = max_subarray_sum(nums)
print("Maximum Subarray Sum:", max_sum)
print("Subarray:", subarray)
Artigos relacionados
01_classical_caesar_cipher.py
01_classical_caesar_cipher.py — python source code from the python old learning materials (python_old/Cryptography/01_classical_caesar_cipher.py).
Ler artigo →02_classical_monoalphabetic.py
02_classical_monoalphabetic.py — python source code from the python old learning materials (python_old/Cryptography/02_classical_monoalphabetic.py).
Ler artigo →03_classical_rail_fence.py
03_classical_rail_fence.py — python source code from the python old learning materials (python_old/Cryptography/03_classical_rail_fence.py).
Ler artigo →04_frequency_analysis.py
04_frequency_analysis.py — python source code from the python old learning materials (python_old/Cryptography/04_frequency_analysis.py).
Ler artigo →05_cryptographic_hashing.py
05_cryptographic_hashing.py — python source code from the python old learning materials (python_old/Cryptography/05_cryptographic_hashing.py).
Ler artigo →06_password_hashing.py
06_password_hashing.py — python source code from the python old learning materials (python_old/Cryptography/06_password_hashing.py).
Ler artigo →