MicroSoft interview 2022 test1
作者:互联网
MicroSoft interview 2022 test1
Programing
one
点击查看代码
def solution(S, B):
# write your code in Python (Python 3.6)
fixed = 0
holes = []
len_hole = 0
for idx in range(len(S)):
# print(S[idx])
if S[idx] == 'x':
len_hole += 1
else:
if len_hole > 0:
holes.append(len_hole)
len_hole = 0
if len_hole > 0:
holes.append(len_hole)
# print("1:", holes)
holes = sorted(holes)
# print("2:", holes)
for item in holes[::-1]:
cost = item + 1
if B >= cost:
B -= cost
fixed += item
elif 1 < B < cost:
fixed += B - 1
B = 0
else:
break
return fixed
if __name__ == '__main__':
# A = 24
S = '...xxx..x....xxx.'
B = 7
lea_count = solution(S, B)
print(lea_count)
点击查看代码
def solution(S):
# write your code in Python (Python 3.6)
min_swap = 0
r_swap, l_swap = [], []
sub_s = []
start, end = 0, 0
for idx, item in enumerate(S):
if item == 'R':
start = idx
break
for idx, item in enumerate(S[::-1]):
if item == 'R':
end = len(S) - idx
break
# print(start, end)
S = S[start:end]
# print(S)
r_count = 0
w_count = 0
for item in S:
if item == 'R':
r_count += 1
else:
w_count += 1
l_swap.append(r_count)
r_count = 0
for item in S[::-1]:
if item == 'R':
r_count += 1
else:
r_swap.append(r_count)
r_swap = r_swap[::-1]
for l, r in zip(l_swap, r_swap):
min_swap += min(l, r)
if min_swap > 1e9:
min_swap = -1
break
return min_swap
if __name__ == '__main__':
# A = 24
S = 'WRW' * 10000
B = 7
lea_count = solution(S)
print(lea_count)
点击查看代码
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
# def solution(A, B, S):
# # write your code in Python (Python 3.6)
# if len(set(A + B)) < len(A):
# return False
# fixed = [0] * S
# flag = True
# while flag:
# for i in range(len(A) - 1):
# for j in range(i + 1, len(A)):
# if A[i] == A[j]:
# if fixed[i] == 0:
# fixed[i] = 1
# A[i] = B[i]
# else:
# return False
# flag = False
# # print(A)
# return True
def solution(A, B, S):
# write your code in Python (Python 3.6)
slices = set(A + B)
if len(slices) >= len(A):
assigned = True
else:
assigned = False
# print(A)
# a = ["a", "b", "a", "c", "a", "c", "b", "d", "e", "c", "a", "c"]
# # set集合去重
# duixiang = set(a) # 先去重,取出计数对象
# # 保存为dict,一一对应
# d = {}
# for i in duixiang:
# d[i] = a.count(i)
# # 对字典按value排序
# a = sorted(d.items(), key=lambda x: x[1], reverse=True)
return assigned
if __name__ == '__main__':
# False
A = [1, 2, 4, 2, 4, 6, 7, 5]
B = [2, 1, 3, 7, 5, 5, 6, 4]
S = 8
# True
# A = [1, 2, 2]
# B = [3, 1, 3]
# S = 3
# True
# A = [1, 2, 2, 8, 4, 6, 7, 6]
# B = [2, 1, 3, 7, 5, 5, 6, 4]
# S = 8
# True
# A = [1, 1, 4, 8, 4, 6, 7, 6]
# B = [2, 2, 3, 7, 5, 5, 6, 4]
# S = 8
# True
# A = [1] * 100000
# B = [(i + 1) % 100001 for i in range(1, 100000)] + [1000]
# S = 100000
lea_count = solution(A, B, S)
print(lea_count)
标签:test1,count,item,len,print,swap,__,interview,MicroSoft 来源: https://www.cnblogs.com/Coulson/p/16589019.html