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

Leetcode 3336. Find the Number of Subsequences With Equal GCD

  • Leetcode 3336. Find the Number of Subsequences With Equal GCD
    • 1. 解题思路
    • 2. 代码实现
  • 题目链接:3336. Find the Number of Subsequences With Equal GCD

1. 解题思路

这一题没能自力搞定,挺伤心的,看大佬的代码之后发现思路上是一个非常暴力的动态规划,就是不断地考察每一个元素加入到第一个数组、第二个数组以及不做操作时,所有可能的两个数组的最大公约数pair的种类和数目变化。

然后考察最终所有非空且最大公约数相同的pair的个数即可。

2. 代码实现

给出python代码实现如下:

MOD = 10**9+7class Solution:def subsequencePairCount(self, nums: List[int]) -> int:dp = defaultdict(int)dp[(0, 0)] = 1ans = 0for num in nums:ndp = deepcopy(dp)for (gcd1, gcd2), cnt in list(dp.items()):_gcd = num if gcd1 == 0 else gcd(num, gcd1)ndp[(_gcd, gcd2)] = (cnt + ndp[(_gcd, gcd2)]) % MOD_gcd = num if gcd2 == 0 else gcd(num, gcd2)ndp[(gcd1, _gcd)] = (cnt + ndp[(gcd1, _gcd)]) % MODdp = ndpfor (gcd1, gcd2), cnt in dp.items():if gcd1 != 0 and gcd1 == gcd2:ans = (ans + cnt) % MODreturn ans

提交代码评测得到:耗时7264ms,占用内存23.9MB。


http://www.mrgr.cn/news/60168.html

相关文章:

  • Python实现摇号系统
  • 苍穹外卖 根据id查询套餐接口
  • 基于Arduino的LED亮灭按键控制
  • 【编程语言】正则表达式:POSIX 与 PCRE 的全面比较及应用
  • 【Python爬虫】获取汽车之家车型配置附代码(2024.10)
  • MySQL 查看有哪些表
  • Leetcode 3337. Total Characters in String After Transformations II
  • Leetcode 3332. Maximum Points Tourist Can Earn
  • Google DeepMind的研究人员提出了Talker-Reasoner框架
  • 【SpringMVC】web服务器,访问失败的问题,SpringMVC,建立连接,请求
  • 【ChatGP】让ChatGPT解释和简化复杂的技术概念
  • 108.SAP MII功能详解(20)Workbench-DisplayTemplate(i5Grid)
  • 开源视频生成 Pyramid Flow 本地部署实测
  • 前端css-媒体查询@media以及常见使用例子
  • 探索基础设施即代码(IaC):Terraform 与 CloudFormation 的应用
  • 目标检测数据集 - 新能源车车牌检测数据集下载「包含VOC、COCO、YOLO三种格式」
  • linux 中文实用型手册 基于RHEL(红帽系)
  • 【linux网络编程】| 网络套接字socket | 初识网络开发
  • 什么是全自动虫情测报灯
  • 应用快速启动工具 Biniware Run v7.0.1.0 中文绿色版
  • 【NOI】C++函数入门二(自定义函数)
  • Django入门教程——员工数据管理
  • 面向应用型人才的中药炮制教学实训方案
  • 掌握 Golang 性能调优:深入理解 `runtime/debug` 包
  • 分布式储能监控系统在某5MW分布式储能项目中的应用
  • 【源码+文档】基于SpringBoot+Vue健康饮食智慧销售系统【提供源码+答辩PPT+参考文档+项目部署】