2020-12-15 题目类型:简单
1
给你一个数组 candies 和一个整数 extraCandies ,其中 candies[i] 代表第 i 个孩子拥有的糖果数目。
对每一个孩子,检查是否存在一种方案,将额外的 extraCandies 个糖果分配给孩子们之后,此孩子有 最多 的糖果。注意,允许有多个孩子同时拥有 最多 的糖果数目。
class Solution(object):
def kidsWithCandies(self, candies, extraCandies):
"""
:type candies: List[int]
:type extraCandies: int
:rtype: List[bool]
"""
max1 = max(candies)
for i in range(len(candies)):
if candies[i] + extraCandies >= max1:
candies[i] = True
else:
candies[i] = False
return candies
2
给你一个整数数组 nums 。你可以选定任意的 正数 startValue 作为初始值。
你需要从左到右遍历 nums 数组,并将 startValue 依次累加上 nums 数组中的值。
请你在确保累加和始终大于等于 1 的前提下,选出一个最小的 正数 作为 startValue 。
class Solution(object):
def minStartValue(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
min = nums[0]
temp = nums[0]
for i in range(1,len(nums)):
temp += nums[i]
if temp < min:
min = temp
return 1 if min > 0 else (1-min)