牛客编程巅峰赛S2第1场 - 青铜&白银&黄金题解

最小差值

https://ac.nowcoder.com/acm/contest/9004/A

链接: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 minDif

Tree 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
全部评论

相关推荐

废铁汽车人:秋招真是牛鬼蛇神齐聚一堂
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务