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