1. 解题思路
这一题的话我们倒序看一下以各个位置作为终点时前面各个位置作为起点时的energy各是多少,取出其中的最大值即可。
2. 代码实现
给出python代码实现如下:
class Solution:
def maximumEnergy(self, energy: List[int], k: int) -> int:
n = len(energy)
ans = -math.inf
for i in range(k):
s = 0
for j in range(n-1-i, -1, -k):
s += energy[j]
ans = max(ans, s)
return ans
提交代码评测得到:耗时1139ms,占用内存29.7MB。