拼多多笔试
第一道:100%
s = input()
count = 0
stack = []
for c in s:
if not stack:
stack.append(c)
continue
if c == stack[-1]:
del stack[-1]
count += 1
else:
stack.append(c)
print(count)
第二道:40%,直接print(0)
的话,70%
n, k = list(map(int, input().split()))
x = list(map(int, input().split()))
ans = x[0]
for i in x[1:]:
if ans + i < n:
print(0)
break
ans = ans + i - n
print(ans)
第三道:没时间做了
第四道:100%
def main():
m, n = list(map(int, input().split()))
h = list(map(int, input().split()))
hash = {}
for i in h:
if i not in hash:
hash[i] = 1
else:
hash[i] += 1
tmp = list(hash.keys())
tmp.sort(reverse=True)
ans = 4000 * 4000
cur_m = m
for i in range(len(tmp)):
if hash[tmp[i]] >= n:
print(0)
return
cur_m -= hash[tmp[i]]
if n - hash[tmp[i]] > cur_m:
break
count = n - hash[tmp[i]]
temp = 0
for j in range(i + 1, len(tmp)):
if count > hash[tmp[j]]:
count -= hash[tmp[j]]
temp += (tmp[i] - tmp[j]) * hash[tmp[j]]
else:
temp += (tmp[i] - tmp[j]) * count
break
ans = min(ans, temp)
print(ans)
main()
#拼多多##笔试题目#