3.29微软笔试

3.29微软笔试

朋友的笔试
Codility
能用遍历O(n)就不要瞎想其他算法
遍历yyds!
3题120分钟:

1、
maximum non-negative slice pair (P, Q) that A[P:Q] are all non-negative.
Return maximum sum of any non-negative slice in the array.
e.g.
[1, 2, -3, 4, 5, -6]
return 9: 4+5 = 9

还是遍历就好

2、
把一根有N个数字的list的链子切两次 P,Q 成三段 (0 < P < Q < N-1)
list[i]对应值为在此处cut的cost,求最小cost

实际为找到A[1,N-1]的两个最小值,两个不重复的min就好

3、
an array A of N integers and an int K
def solution(A,K)
return the minimal amplitude after removing K consecutive elements from A
Amplitude: max-min
e.g. A = [5,3,6,1,3], K=2
return 2: remove 6,1, amplitude of [5,3,3]=2

def solution(A,K):
    if len(A) == 0:
        return 0
    mindiff = 1000000000
    for i in range(len(A)):
        sublist = A[:i] + A[i+K:]
        submax = max(sublist)
        submin = min(sublist)
        temp = submax - submin
        if temp < mindiff:
            mindiff = temp
    return mindiff
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值