7月24号python:库存管理
题目:
仓库管理员以数组 stock
形式记录商品库存表。stock[i]
表示商品 id
,可能存在重复。原库存表按商品 id
升序排列。现因突发情况需要进行商品紧急调拨,管理员将这批商品 id
提前依次整理至库存表最后。请你找到并返回库存表中编号的 最小的元素 以便及时记录本次调拨。
示例 1:
输入:stock = [4,5,8,3,4]
输出:3
示例 2:
输入:stock = [5,7,9,1,2]
输出:1
提示:
- 1 <= stock.length <= 5000
- -5000 <= stock[i] <= 5000
代码补全:
class Solution:
def stockManagement(self, stock: List[int]) -> int:
答案:
class Solution:
def stockManagement(self, stock: List[int]) -> int:
if not stock:return None
min=stock[0]
for s in stock[1:]:
if s<min:
min=s
return min
最快的解法:
class Solution:
def stockManagement(self, stock: List[int]) -> int:
return min(stock)
有一点技术的:
class Solution:
def stockManagement(self, stock: List[int]) -> int:
left = 0
right = len(stock)-1
while left < right:
mid = left + (right-left )//2
if stock[mid]>stock[right]:
left = mid +1
elif stock[mid]<stock[right]:
right = mid
else:
right = right -1
return stock[left]
最少人想到的:
class Solution:
def stockManagement(self, stock: List[int]) -> int:
tmp = stock[-1]
for i in stock[::-1]:
if i > tmp:
return tmp
else:
tmp = i
return tmp
最多使用的
class Solution:
def stockManagement(self, stock: List[int]) -> int:
last = -5001
for s in stock:
if s >= last:
last = s
else:
return s
return stock[0]
标签:24,库存,return,python,List,Solution,int,stockManagement,stock
From: https://www.cnblogs.com/guixiangyyds/p/18321735/python2