牛客编程巅峰赛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
全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 11:00
点赞 评论 收藏
分享
白火同学:能。我当初应届沟通了1200,收简历50,面试10左右吧,加油投吧
投了多少份简历才上岸
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-08 10:39
一个证都没&nbsp;我能填什么
程序员小白条:别人有,你为什么没有,还是这个道理,社会就是比较,竞争,淘汰,你要安逸,那么就要做好淘汰的准备
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务