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

Python 小白的 Leetcode Daily Challenge 刷题计划 - 20240209(除夕)

368. Largest Divisible Subset

难度:Medium

  • 动态规划 + 方案还原

Yesterday's Daily Challenge can be reduced to the problem of shortest path in an unweighted graph while today's daily challenge can be reduced to the problem of longest path in an unweighted graph.
Happy Chinese New Year!

class Solution:def largestDivisibleSubset(self, nums: list[int]) -> list[int]:n = len(nums)nums.sort()f, pre = [1]*n, [-1]*nt = 0for i in range(n):for j in range(i):if nums[i] % nums[j] == 0:if f[j]+1 > f[i]:f[i] = f[j]+1pre[i] = jif f[i] > f[t]:t = ians = []while t != -1:ans.append(nums[t])t = pre[t]return ansdef test():samples = [[1,2,3],[1,2,4,8]]sol = Solution()for nums in samples:print(sol.largestDivisibleSubset(nums))if __name__ == '__main__':test()

http://www.lryc.cn/news/297233.html

相关文章:

  • BFS——双向广搜+A—star
  • LLM之LangChain(七)| 使用LangChain,LangSmith实现Prompt工程ToT
  • 新零售的升维体验,摸索华为云GaussDB如何实现数据赋能
  • vscode +git +gitee 文件管理
  • 【力扣】用栈判断有效的括号
  • 【目录】CSAPP的实验简介与解法总结(已包含Attack/Link/Architecture/Cache)
  • 【机器学习】数据清洗之识别缺失点
  • 【Vue】Vue基础入门
  • 正点原子-STM32通用定时器学习笔记(1)
  • Redis篇之redis是单线程
  • 随机MM引流源码PHP开源版
  • 【C++修行之道】(引用、函数提高)
  • 从零开始手写mmo游戏从框架到爆炸(十一)— 注册与登录
  • 【SpringBoot】Redis集中管理Session和自定义用户参数解决登录状态及校验问题
  • 【0256】揭晓pg内核中MyBackendId的分配机制(后端进程Id,BackendId)(二)
  • eclipse4.28.0版本如何安装FatJar插件
  • 查大数据检测到风险等级太高是怎么回事?
  • Leetcode 30天高效刷数据结构和算法 Day1 两数之和 —— 无序数组
  • Hair Tool for Blender3D
  • 【最详解】如何进行点云的凹凸缺陷检测(opene3D)(完成度80%)
  • 海外云手机——平台引流的重要媒介
  • 数据库-计算机三级学习记录-4DBAS功能概要设计
  • JVM-虚拟机栈
  • linux系统上tomcat简介以及安装tomcat
  • 树莓派的pip安装时候添加清华源
  • 共享网盘系统PHP源码
  • unity-ios-解决内购商品在Appstore上面已配置,但在手机测试时却无法显示的问题
  • flask的基本使用 token插件(二)
  • 云计算、Docker、K8S问题
  • 【Iceberg学习二】Branch和Tag在Iceberg中的应用