云计算简单算法练习题

云计算简单算法练习题

No1

LCR 128. 库存管理 I

仓库管理员以数组 stock 形式记录商品库存表。stock[i] 表示商品 id,可能存在重复。原库存表按商品 id 升序排列。现因突发情况需要进行商品紧急调拨,管理员将这批商品 id 提前依次整理至库存表最后。请你找到并返回库存表中编号的 最小的元素 以便及时记录本次调拨。

示例 1:

输入:stock = [4,5,8,3,4]
输出:3

示例 2:

输入:stock = [5,7,9,1,2]
输出:1
class Solution:
    def inventoryManagement(self, stock: List[int]) -> int:
# author: 王贵祥
# date: 2025-10-21
# description: 库存管理
class Solution:
    def inventoryManagement(self, stock: List[int]) -> int:
        # 判断库存是否为空
        if not stock:
            return 0
        ans = stock[-1]
        # 返回库存中数量最少的商品数量
        for _ in reversed(stock):
            if _ <= ans:
                ans = _
            if _ > ans:
                break
        return ans

No2

LCR 139. 训练计划 I

教练使用整数数组 actions 记录一系列核心肌群训练项目编号。为增强训练趣味性,需要将所有奇数编号训练项目调整至偶数编号训练项目之前。请将调整后的训练项目编号以 数组 形式返回。

示例 1:

输入:actions = [1,2,3,4,5]
输出:[1,3,5,2,4] 
解释:为正确答案之一
class Solution:
    def trainingPlan(self, actions: List[int]) -> List[int]:
# author: 王贵祥
# date: 2025-10-21
# description: 训练计划调整
from typing import List
# class Solution:
#     def trainingPlan(self, action: List[int]) -> List[int]:
#         # 判断动作列表是否为空
#         if not action:
#             return []
#         lenth = len(action)
#         if lenth == 1:
#             return action
#         for i in range(1, lenth, 2):
#             tmp = action[i]
#             if i+1 >= lenth:
#                 break
#             action[i] = action[i+1]
#             action[i+1] = tmp
#         return action
# # 测试用例
# if __name__ == "__main__":
#     s = Solution()
#     print(s.trainingPlan([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]))  # 输出: [1, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10]


#刚刚写错了
class Solution:
    def trainingPlan(self, action: List[int]) -> List[int]:
        # 判断动作列表是否为空
        if not action:
            return []
        list_even = []
        list_odd = []
        for i in range(len(action)):
            if action[i]%2==0:
                list_even.append(action[i])
            else:
                list_odd.append(action[i])
        result = list_odd + list_even
        return result
# 测试用例
if __name__ == "__main__":
    s = Solution()
    print(s.trainingPlan([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]))  # 输出: [1, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10]
posted @ 2025-10-21 22:11  guixiang  阅读(1)  评论(0)    收藏  举报