LeetCode#643 Maximum Average Subarray I - Python

preview_player
Показать описание
Solving LeetCode problem #643 Maximum Average Subarray I in Python

#leetcode #coding #python #Shorts
Рекомендации по теме
Комментарии
Автор

You can actually speed up this a little by not calculating the avarage every time, since the number of elements in the window stays the same, the average is proportional to the sum, so we can just sum the elements, check if the sum is greater than the max sum, if so update it, and move the window. This won't reduce the time complexity, but division is an expensive operation for cpu, so performing one final division instead of n - 1 divisions will definitely affect the overall speed of your algorithm.

Update: I just noticed that in your program that's exactly what you did, you just didn't explain it.

indyplaygames
welcome to shbcf.ru