当前位置:网站首页>Dynamic programming of the division of numbers
Dynamic programming of the division of numbers
2022-08-04 05:00:00 【a boy in the mountains】
1. Corresponding OJ link:
Division of Numbers_Niu Ke Tiba_Nioke Network(nowcoder.com).com)
2. Title description:
3. Problem solving ideas:
Method 1: Direct violent recursion
Try from the index position to n, please see the code for details.
Corresponding code:
class Solution {public:/*** The class name, method name and parameter name in the code have been specified, please do not modify it, just return the value specified by the method*** @param n int the number to be divided* @param k int into k parts* @return int*/int divideNumber(int n, int k) {// write code herereturn process(n, k, 1);}//Recursive meaning: the number of methods to divide n into k points starting from the index positionint process(int n, int k, int index) {if (k < 0) {return 0;}//If there are 0 points left, then there is only one division result when k is 0if (k == 0) {return n == 0 ? 1 : 0;}int ways = 0;for (int i = index; i <= n; i++) {// try the number at position iways += process(n - i, k - 1, i);}return ways;}};
Unfortunately this method will still time out even if it is changed to memoized search and dp.Let's look at the following method.It feels very good:
We can imagine k as having k boxes. According to the meaning of the title, each box cannot be empty, so we can put a 1 in each box in advance so that the box will not be empty.Then we can put the remaining n-k balls into k boxes. At this time, there are many possibilities. n-k balls can be placed in the first box, which can be placed in 1 and 2.The two boxes can also be placed in the three boxes 1 to 3, and they can also be placed in the boxes 1 to k.
Corresponding code:
class Solution {public:/*** The class name, method name and parameter name in the code have been specified, please do not modify it, just return the value specified by the method*** @param n int the number to be divided* @param k int into k parts* @return int*/vector>dp;int mod=1000000000+7;int divideNumber(int n, int k) {// write code here// dp.resize(n+1,vector >(k+1,vector (n+1,-1)));dp.resize(n+1,vector (k+1,-1));return process(n,k);}//The number of ways to divide n into k pointsint process(int n, int k){if(n==k){return 1;}if(k>n){return 0;}if(dp[n][k]!=-1){return dp[n][k];}int ways=0;//The possibility of placing the remaining n-k balls in k boxes is enumeratedfor(int i=1;i<=k;i++){ways=(ways+process(n-k,i))%mod;}dp[n][k]=ways;return ways;}};
Slope optimization:
Corresponding code:
class Solution {public:/*** The class name, method name and parameter name in the code have been specified, please do not modify it, just return the value specified by the method*** @param n int the number to be divided* @param k int into k parts* @return int*/vector>dp;int mod=1000000000+7;int divideNumber(int n, int k) {// write code here// dp.resize(n+1,vector >(k+1,vector (n+1,-1)));dp.resize(n+1,vector (k+1,-1));return process(n,k);}//The number of ways to divide n into k pointsint process(int n, int k){if(k==0){return n==0?1:0;}if(k==n){return 1;}if(k>n){return 0;}if(dp[n][k]!=-1){return dp[n][k];}int ways=process(n-1,k-1);ways=(ways+process(n-k,k))%mod;dp[n][k]=ways;return ways;}};
边栏推荐
- leetcode 12. 整数转罗马数字
- 2023年PMP考试会用新版教材吗?回复来了!
- 基于gRPC编写golang简单C2远控
- For Qixi Festival, I made a confession envelope with code
- Simple operation of the file system
- 系统设计.如何设计一个秒杀系统(完整版 转)
- System design. Seckill system
- Reproduce 20-character short domain name bypass
- 【一步到位】Jenkins的安装、部署、启动(完整教程)
- 技术解析|如何将 Pulsar 数据快速且无缝接入 Apache Doris
猜你喜欢

【云原生--Kubernetes】Pod资源管理与探针检测

Simple operation of the file system
![[C language advanced] program environment and preprocessing](/img/ac/a13dd2cc47136d4938b6fc7fad660c.png)
[C language advanced] program environment and preprocessing
![[Cloud Native--Kubernetes] Pod Resource Management and Probe Detection](/img/1a/b3bdf9b62c82b0fc4d913045981d94.png)
[Cloud Native--Kubernetes] Pod Resource Management and Probe Detection

编程大杂烩(三)

Get the selected content of the radio box

触觉智能分享-SSD20X实现升级显示进度条

Tensors - Application Cases

烧录场景下开发如何进行源代码保密工作

某母婴小程序加密参数解密
随机推荐
Jenkins 导出、导入 Job Pipeline
Use Patroni callback script to bind VIP pit
2023年PMP考试会用新版教材吗?回复来了!
7-2 LVS+DR概述与部署
【机器学习】21天挑战赛学习笔记(一)
【流程图】
C Expert Programming Chapter 4 The Shocking Fact: Arrays and Pointers Are Not the Same 4.3 What is a Declaration and What is a Definition
信息学奥赛一本通 1312:【例3.4】昆虫繁殖
ADC噪声全面分析 -03- 利用噪声分析进行实际设计
如何简化现代电子采购的自动化?
看DevExpress丰富图表样式,如何为基金公司业务创新赋能
C专家编程 第5章 对链接的思考 5.3 函数库链接的5个特殊秘密
C专家编程 第5章 对链接的思考 5.2 动态链接的优点
【云原生--Kubernetes】Pod资源管理与探针检测
【21天学习挑战赛】顺序查找
C专家编程 第5章 对链接的思考 5.4 警惕Interpositioning
小程序 + 电商,玩转新零售
go module的介绍与应用
技术解析|如何将 Pulsar 数据快速且无缝接入 Apache Doris
文件系统的简单操作



