鱼C论坛

 找回密码
 立即注册
查看: 2275|回复: 7

【一道难题】

[复制链接]
发表于 2020-6-19 13:30:14 | 显示全部楼层
确实,网上检索“LeetCode-Python-1172”
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-19 19:32:56 | 显示全部楼层
你可能用的是C,怎么语句后面都是分号:
用python写了个,没有用python的heapq
  1. #DinnerPlates(int capacity) - 给出栈的最大容量 capacity。
  2. #void push(int val) - 将给出的正整数 val 推入 从左往右第一个 没有满的栈。
  3. #int pop() - 返回 从右往左第一个 非空栈顶部的值,并将其从栈中删除;如果所有的栈都是空的,请返回 -1。
  4. #int popAtStack(int index) - 返回编号 index 的栈顶部的值,并将其从栈中删除;如果编号 index 的栈是空的,请返回 -1。

  5. class DP:
  6.     stacks = []
  7.     stacks_index = []
  8.     capacity = 5

  9.     @staticmethod
  10.     def DinnerPlates(capacity):
  11.         DP.capacity = capacity

  12.     @staticmethod
  13.     def free_pos(start=None, end =None):
  14.         if start == None:
  15.             start,end = 0,len(DP.stacks)
  16.         selected = DP.stacks_index[start:end]
  17.         return (end-start)* DP.capacity - sum(DP.stacks_index)
  18.         
  19.     @staticmethod
  20.     def first_free(start,end):
  21.         if start == end-1:
  22.             if DP.stacks_index[start] < DP.capacity:
  23.                 return start
  24.             else:
  25.                 return -1

  26.         middle = (start + end)//2
  27.         part1 = DP.first_free(start,middle)
  28.         return part1 if part1 !=-1 else DP.first_free(middle,end)

  29.     @staticmethod
  30.     def first_filled(start,end):
  31.         if start == end-1:
  32.             if DP.stacks_index[start] > 0:
  33.                 return start
  34.             else:
  35.                 return -1

  36.         middle = (start + end)//2
  37.         part1 = DP.first_filled(middle,end)
  38.         return part1 if part1 !=-1 else DP.first_filled(start,middle)

  39.     @staticmethod
  40.     def push(val):
  41.         if DP.free_pos()>0:
  42.             index = DP.first_free(0,len(DP.stacks))
  43.         else:
  44.             index = len(DP.stacks)
  45.             stack = [-1 for i in range(DP.capacity)]
  46.             DP.stacks.append(stack)
  47.             DP.stacks_index.append(0)
  48.         DP.stacks[index][DP.stacks_index[index]] = val
  49.         DP.stacks_index[index] += 1
  50.     @staticmethod
  51.     def pop():
  52.         if sum(DP.stacks_index) == 0:
  53.             return -1
  54.         else:
  55.             index = DP.first_filled(0,len(DP.stacks))
  56.             DP.stacks_index[index] -= 1
  57.             return DP.stacks[index][DP.stacks_index[index]]
  58.     @staticmethod
  59.     def popAtStack(index):
  60.         if index < len(DP.stacks) and DP.stacks_index[index] > 0:
  61.             DP.stacks_index[index] -= 1
  62.             return DP.stacks[index][DP.stacks_index[index]]
  63.         else:
  64.             return -1
  65.        
  66. DP.DinnerPlates(2)
  67. DP.push(1)
  68. DP.push(2)
  69. DP.push(3)
  70. DP.push(4)
  71. DP.push(5)
  72. print(DP.stacks,DP.stacks_index,DP.capacity)
  73. DP.popAtStack(0)
  74. print(DP.stacks,DP.stacks_index,DP.capacity)
  75. DP.push(20)
  76. print(DP.stacks,DP.stacks_index,DP.capacity)
  77. print(DP.stacks,DP.stacks_index,DP.capacity)
  78. DP.popAtStack(0)
  79. print(DP.stacks,DP.stacks_index,DP.capacity)
  80. DP.push(20)
  81. print(DP.stacks,DP.stacks_index,DP.capacity)
  82. DP.push(21)
  83. print(DP.stacks,DP.stacks_index,DP.capacity)
  84. DP.popAtStack(0)
  85. print(DP.stacks,DP.stacks_index,DP.capacity)
  86. DP.popAtStack(2)
  87. print(DP.stacks,DP.stacks_index,DP.capacity)
  88. DP.pop()
  89. print(DP.stacks,DP.stacks_index,DP.capacity)
  90. DP.pop()
  91. print(DP.stacks,DP.stacks_index,DP.capacity)
  92. DP.pop()
  93. print(DP.stacks,DP.stacks_index,DP.capacity)
  94. DP.pop()
  95. print(DP.stacks,DP.stacks_index,DP.capacity)
  96. DP.pop()
  97. print(DP.stacks,DP.stacks_index,DP.capacity)
复制代码


结果(栈列表,以及各栈的栈顶指针,指针为零,栈内值不设置为-1的,主要看栈顶指针):
栈列表,栈顶指针,最后一个2是容量(这个可以无视)
[[1, 2], [3, 4], [5, -1]] [2, 2, 1] 2
[[1, 2], [3, 4], [5, -1]] [1, 2, 1] 2
[[1, 20], [3, 4], [5, -1]] [2, 2, 1] 2
[[1, 20], [3, 4], [5, -1]] [2, 2, 1] 2
[[1, 20], [3, 4], [5, -1]] [1, 2, 1] 2
[[1, 20], [3, 4], [5, -1]] [2, 2, 1] 2
[[1, 20], [3, 4], [5, 21]] [2, 2, 2] 2
[[1, 20], [3, 4], [5, 21]] [1, 2, 2] 2
[[1, 20], [3, 4], [5, 21]] [1, 2, 1] 2
[[1, 20], [3, 4], [5, 21]] [1, 2, 0] 2
[[1, 20], [3, 4], [5, 21]] [1, 1, 0] 2
[[1, 20], [3, 4], [5, 21]] [1, 0, 0] 2
[[1, 20], [3, 4], [5, 21]] [0, 0, 0] 2
[[1, 20], [3, 4], [5, 21]] [0, 0, 0] 2
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-11 03:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表