|
发表于 2020-6-19 19:32:56
|
显示全部楼层
你可能用的是C,怎么语句后面都是分号:
用python写了个,没有用python的heapq
- #DinnerPlates(int capacity) - 给出栈的最大容量 capacity。
- #void push(int val) - 将给出的正整数 val 推入 从左往右第一个 没有满的栈。
- #int pop() - 返回 从右往左第一个 非空栈顶部的值,并将其从栈中删除;如果所有的栈都是空的,请返回 -1。
- #int popAtStack(int index) - 返回编号 index 的栈顶部的值,并将其从栈中删除;如果编号 index 的栈是空的,请返回 -1。
- class DP:
- stacks = []
- stacks_index = []
- capacity = 5
- @staticmethod
- def DinnerPlates(capacity):
- DP.capacity = capacity
- @staticmethod
- def free_pos(start=None, end =None):
- if start == None:
- start,end = 0,len(DP.stacks)
- selected = DP.stacks_index[start:end]
- return (end-start)* DP.capacity - sum(DP.stacks_index)
-
- @staticmethod
- def first_free(start,end):
- if start == end-1:
- if DP.stacks_index[start] < DP.capacity:
- return start
- else:
- return -1
- middle = (start + end)//2
- part1 = DP.first_free(start,middle)
- return part1 if part1 !=-1 else DP.first_free(middle,end)
- @staticmethod
- def first_filled(start,end):
- if start == end-1:
- if DP.stacks_index[start] > 0:
- return start
- else:
- return -1
- middle = (start + end)//2
- part1 = DP.first_filled(middle,end)
- return part1 if part1 !=-1 else DP.first_filled(start,middle)
- @staticmethod
- def push(val):
- if DP.free_pos()>0:
- index = DP.first_free(0,len(DP.stacks))
- else:
- index = len(DP.stacks)
- stack = [-1 for i in range(DP.capacity)]
- DP.stacks.append(stack)
- DP.stacks_index.append(0)
- DP.stacks[index][DP.stacks_index[index]] = val
- DP.stacks_index[index] += 1
- @staticmethod
- def pop():
- if sum(DP.stacks_index) == 0:
- return -1
- else:
- index = DP.first_filled(0,len(DP.stacks))
- DP.stacks_index[index] -= 1
- return DP.stacks[index][DP.stacks_index[index]]
- @staticmethod
- def popAtStack(index):
- if index < len(DP.stacks) and DP.stacks_index[index] > 0:
- DP.stacks_index[index] -= 1
- return DP.stacks[index][DP.stacks_index[index]]
- else:
- return -1
-
- DP.DinnerPlates(2)
- DP.push(1)
- DP.push(2)
- DP.push(3)
- DP.push(4)
- DP.push(5)
- print(DP.stacks,DP.stacks_index,DP.capacity)
- DP.popAtStack(0)
- print(DP.stacks,DP.stacks_index,DP.capacity)
- DP.push(20)
- print(DP.stacks,DP.stacks_index,DP.capacity)
- print(DP.stacks,DP.stacks_index,DP.capacity)
- DP.popAtStack(0)
- print(DP.stacks,DP.stacks_index,DP.capacity)
- DP.push(20)
- print(DP.stacks,DP.stacks_index,DP.capacity)
- DP.push(21)
- print(DP.stacks,DP.stacks_index,DP.capacity)
- DP.popAtStack(0)
- print(DP.stacks,DP.stacks_index,DP.capacity)
- DP.popAtStack(2)
- print(DP.stacks,DP.stacks_index,DP.capacity)
- DP.pop()
- print(DP.stacks,DP.stacks_index,DP.capacity)
- DP.pop()
- print(DP.stacks,DP.stacks_index,DP.capacity)
- DP.pop()
- print(DP.stacks,DP.stacks_index,DP.capacity)
- DP.pop()
- print(DP.stacks,DP.stacks_index,DP.capacity)
- DP.pop()
- 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 |
|