当前位置:网站首页>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;}};
边栏推荐
- There is an 8 hour difference between the docker installation of mysql and the host.
- How to simplify the automation of modern e-procurement?
- Tensors - Application Cases
- 10 Convolutional Neural Networks for Deep Learning 3
- JVM笔记
- redis中常见的面试题
- ADC噪声全面分析 -03- 利用噪声分析进行实际设计
- C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.2 我的代码为什么无法运行
- QT 如何识别文件的编码格式
- C Expert Programming Chapter 5 Thinking about Chaining 5.6 Take it easy --- see who's talking: take the Turning quiz
猜你喜欢

深度学习环境配置

3000 words, is take you understand machine learning!

编程大杂烩(四)
![[21 Days Learning Challenge] Image rotation problem (two-dimensional array)](/img/51/fb78f36c71e1eaac665ce9f1ce04ea.png)
[21 Days Learning Challenge] Image rotation problem (two-dimensional array)

数的划分之动态规划

【21天学习挑战赛】图像的旋转问题(二维数组)

Tensors - Application Cases

七夕节,我用代码制作了表白信封

How to keep the source code confidential in the development under the burning scenario

manipulation of file contents
随机推荐
八年软件测试工程师带你了解-测试岗进阶之路
8.Haproxy 搭建Web集群
[One step in place] Jenkins installation, deployment, startup (complete tutorial)
Simple operation of the file system
How to simplify the automation of modern e-procurement?
系统设计.如何设计一个秒杀系统(完整版 转)
如何打造一篇优秀的简历
【21天学习挑战赛】顺序查找
About yolo7 and gpu
7-3 LVS+Keepalived Cluster Description and Deployment
路网编辑器技术预研
中信证券网上开户怎么开的?安全吗?
redis中常见的面试题
42. 接雨水
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.3 什么是声明,什么是定义
The Shell function
使用Loadrunner进行性能测试
C Expert Programming Chapter 5 Thinking about Linking 5.3 5 Special Secrets of Library Linking
el-Select 选择器 底部固定
2022年PMP考试延迟了,该喜该忧?


