链接:https://ac.nowcoder.com/acm/contest/9004/A
最小差值:
排序+贪心
class Solution:
def minDifference(self , a ):
# write code here
a = sorted(a)
minDif = float("inf")
for i in range(len(a) - 1):
minDif = min(a[i+1] - a[i], minDif)
return minDifTree IV:
分层求和,python对小数取模好像有坑,具体在哪不太清楚。然后要先除以2在取模要不然会丢失精度。
链接:https://ac.nowcoder.com/acm/contest/9004/B
来源:牛客网
mod = 998244353
total = 0
left = 1
dep = 1
while left <= n:
right = min(2*left - 1, n)
total = (total + (dep * ((left + right) * (right - left + 1) // 2) % mod) % mod) % mod
left = right + 1
dep += 1
return total % mod链接:https://ac.nowcoder.com/acm/contest/9004/C
来源:牛客网
牛牛组数:
排序+贪心
class Solution:
def Maxsumforknumers(self , x , k ):
# write code here
x = sorted(x, reverse=True)
total = 0
maxlen = len(x) - k + 1
total += int("".join(x[:maxlen]))
for i in range(maxlen, len(x)):
total += int(x[i])
return total
京公网安备 11010502036488号