Day45力扣打卡

打卡记录

Day45力扣打卡_第1张图片


无矛盾的最佳球队(线性DP)

class Solution:
    def bestTeamScore(self, scores: List[int], ages: List[int]) -> int:
        n = len(scores)        
        nums = sorted(zip(scores, ages))
        f = sorted(scores)
        for i in range(n):
            for j in range(0, i):
                if nums[j][1] <= nums[i][1]:
                    f[i] = max(f[i], f[j] + nums[i][0])
        return max(f)

背单词的小智(二分 + 贪心)

n, k = map(int, input().split())
a = list(map(int, input().split()))

s = [0] * (n + 1)
for i in range(n):
    s[i + 1] = s[i] + a[i] ** 2


def check(x):
    res = 0
    i = 0
    while i < n:
        start = i
        if s[start + 1] - s[start] > x:
            return False
        i += 1
        while i < n and s[i + 1] - s[start] <= x:
            i += 1
        res += 1
    return res <= k


l, r = 0, s[n] - s[0]
while l < r:
    mid = (l + r) >> 1
    if check(mid):
        r = mid
    else:
        l = mid + 1
print(l)

你可能感兴趣的:(leetcode刷题打卡,leetcode,算法,python)