当前位置: 首页 > news >正文

云计算简单算法练习题

云计算简单算法练习题

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 0ans = stock[-1]# 返回库存中数量最少的商品数量for _ in reversed(stock):if _ <= ans:ans = _if _ > ans:breakreturn 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_evenreturn 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]
http://www.rkmt.cn/news/26751.html

相关文章:

  • Java三大特性
  • 高级程序设计第二次作业
  • 10月21日日记
  • idea快捷键和注释、关键字、数据类型
  • Windows版本的Emacs如何选择字体(Linux也一样,KIMI)
  • 理想婚姻
  • 蛋白表达技术概述
  • 二叉树的中序遍历- 递归原理 - MKT
  • 二叉树的中序遍历- 递归和栈 - MKT
  • 友链测试
  • English writing practice in diary.
  • 以此文记我的国漫生活
  • 接下来的目标
  • 敬启,致那时的我
  • 清楚标签默认样式,内容溢出盒子时的处理
  • 用 大模型 和 Gradio 构建一个 AI 反向词典
  • 1279. 红绿灯路口
  • python概念详解
  • 用户消费行为数据分析(随笔)
  • 「LG6596-How Many of Them」题解
  • 骗我呢
  • 手搓文件管理系统(持续开发中)
  • AGC001~030 合集
  • AGC 合集 1.0
  • 深入BERT内核:用数学解密掩码语言模型的工作原理
  • [论文笔记] Precision-Guided Context Sensitivity for Pointer Analysis
  • 朋友圈文案不会写?这个AI指令可能帮得上忙
  • 职责分离的艺术:剖析主从Reactor模型如何实现极致的并发性能
  • 数学题刷题记录(数学、数论、组合数学)
  • 记录一次raid恢复之后数据库故障处理(ora-01200,ORA-26101,ORA-600)---惜分飞