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

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]*n
        t = 0
        for 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]+1
                        pre[i] = j
                if f[i] > f[t]:
                    t = i
        ans = []
        while t != -1:
            ans.append(nums[t])
            t = pre[t]
        return ans

def 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.kler.cn/news/233366.html

相关文章:

  • 初识文件包含漏洞
  • 【OpenHarmony硬件操作】风扇与温湿度模块
  • 【Spring】Spring 对 Ioc 的实现
  • kaggle实战语义分割-Car segmentation(附源码)
  • 数据库管理phpmyadmin
  • Linux C/C++ 原始套接字:打造链路层ping实现
  • 【ESLint】TypeError:this.libOptions.parse is not a function
  • 【从Python基础到深度学习】4. Linux 常用命令
  • MinMaxScaler, StandardScaler数据预处理中常用的两种缩放方法,用于将数据标准化或归一化到特定的范围或分布
  • 【CV论文精读】EarlyBird: Early-Fusion for Multi-View Tracking in the Bird’s Eye View
  • IOS破解软件安装教程
  • 达梦数据库适配Springboot+MybatisPlus+达梦数据库
  • 谷歌 DeepMind 联合斯坦福推出了主从式遥操作双臂机器人系统增强版ALOHA 2
  • 嵌入式单片机中晶振的工作原理
  • laravel distinct查询问题,laravel子查询写法
  • FastAdmin西陆房产系统(xiluHouse)全开源
  • 百面嵌入式专栏(面试题)内存管理相关面试题1.0
  • WebSocket 通信流程,注解和Spring实现WebSocket ,实战多人聊天室系统
  • ChatGPT高效提问—prompt常见用法(续篇五)
  • Flask 入门8:Web 表单
  • 【前端web入门第四天】03 显示模式+综合案例热词与banner效果
  • 使用navicat导出mysql离线数据后,再导入doris的方案
  • 【51单片机Keil+Proteus8.9】门锁控制电路
  • 法国实习面试——计算机相关专业词汇
  • ElasticSearch之倒排索引
  • 车载测试中:如何处理 bug
  • SparkJDBC读写数据库实战
  • c#表达式树(MemberInitExpression)成员初始化表达式
  • 工厂方法模式(Factory Method Pattern)
  • 【开源计算机视觉库OpencV详解——超详细】