华为4.26笔试凉经
仅作为自我记录第一题61%第二题超时4%第三题没咋看
第一题
yilaitu存储当前状态
zero_yilai表示当前有多少个是已经完成前置条件
last_zero_yilai表示上轮已经完成前置条件的个数(last_zero_yilai=zero_yilai的话就表明已经进入死循环了,输出-1)
step表示轮次
zero_yilai_dict是字典存储某个是否已经初始化完毕(后续只关注key是否存在即可,不需要判断value值)(也可以用其他数据结构比如set,但是时间复杂度一样?反正考的时候忘记了)
bukeneng表示是否进入循环
代码:
def func():
n = int(input())
yilaitu = []
for i in range(n):
yilaitu.append(list(map(int, input().strip().split())))
zero_yilai = 0
last_zero_yilai = 0
step = 0
zero_yilai_dict = {}
bukeneng = False
for i in range(n):
if yilaitu[i][0] == 0 and i not in zero_yilai_dict:
zero_yilai_dict[i+1] = 0
zero_yilai += 1
if zero_yilai == 0:
print(-1)
while zero_yilai != n:
for i in range(n):
# for j in range(1, len(yilaitu[i])):
j = 1
while j < len(yilaitu[i]):
if yilaitu[i][j] in zero_yilai_dict:
yilaitu[i][0] -= 1
yilaitu[i].pop(j)
j -= 1
j += 1
last_zero_yilai = zero_yilai
step += 1
for i in range(n):
if yilaitu[i][0] == 0 and i+1 not in zero_yilai_dict:
zero_yilai_dict[i+1] = 0
zero_yilai += 1
if last_zero_yilai == zero_yilai:
bukeneng is True
print(-1)
break
if bukeneng is False:
print(step+1)
# please define the python3 input here. For example: a,b = map(int, input().strip().split())
# please finish the function body here.
# please define the python3 output here. For example: print().
if __name__ == "__main__":
func()
第二题
a,b是池子范围
now_stage表示现在有多少空余的空间
caozuo_storage对所有的操作进行存储
storage表示当前池子的情况
storage_dict表示的是某一个空间有没有被占用(0被占用,1空闲)
后面感觉其实不用遍历两次,一边获取数据一边操作就可以了。感觉就是时间复杂度超了,不然其实应该能过(也想到LRU了,但是感觉实现很复杂,想着暴力试试,没想到过的样例这么少)
def func():
a, b = map(int, input().strip().split())
max_storage = b - a + 1
now_storage = max_storage
n = int(input())
caozuo_storage = []
storage = []
storage_dict = {}
# 初始化操作
for i in range(n):
caozuo_storage.append(list(map(int, input().strip().split())))
# 初始化资源池
for i in range(a,b+1):
storage.append(i)
storage_dict[i] = 1
for i in range(n):
# 动态分配
if caozuo_storage[i][0] == 1:
if now_storage < caozuo_storage[i][1]:
continue
else:
now_storage = now_storage - caozuo_storage[i][1]
for tmp in range(caozuo_storage[i][1]):
storage_dict[storage[0]] = 0
storage.pop(0)
# 分配一个
if caozuo_storage[i][0] == 2:
if caozuo_storage[i][1] < a or caozuo_storage[i][1] > b:
continue
elif storage_dict[caozuo_storage[i][1]] == 0:
continue
else:
now_storage -= 1
storage_dict[caozuo_storage[i][1]] = 0
for t in range(len(storage)):
if storage[t] == caozuo_storage[i][1]:
storage.pop(t)
break
# 释放一个
if caozuo_storage[i][0] == 3:
if caozuo_storage[i][1] < a or caozuo_storage[i][1] > b:
continue
elif storage_dict[caozuo_storage[i][1]] == 1:
continue
else:
now_storage += 1
storage_dict[caozuo_storage[i][1]] = 1
print(storage[0])
# please define the python3 input here. For example: a,b = map(int, input().strip().split())
# please finish the function body here.
# please define the python3 output here. For example: print().
if __name__ == "__main__":
func()
#我的实习求职记录##软件开发2023笔面经#