给定一个整数 n > 0,它表示位数,任务是找到本质上不减少的 n 位正整数的总数。
不减整数是从左到右的所有数字都是不减形式的整数。例如:1234、1135、..等。
注意:前导零也计入非递减整数,例如 0000、0001、0023 等也是 4 位非递减整数。
例子 :
Input : n = 1
Output : 10
Numbers are 0, 1, 2, ...9.
Input : n = 2
Output : 55
Input : n = 4
Output : 715
朴素的方法:我们生成所有可能的 n 位数字,然后对于每个数字我们检查它是否不减少。
时间复杂度:(n*10^n),其中 10^n 用于生成所有可能的 n 位数字,n 用于检查特定数字是否不减少。
动态规划:
如果我们从左到右一个一个地填充数字,则以下条件成立。
- 如果当前最后一位数字是 9,我们只能在剩下的地方填充 9。因此,如果当前最后一位数字是 9,则只有一种解决方案是可能的。
- 如果当前最后一位数字小于 9,那么我们可以使用以下公式递归计算计数。
a[i][j] = a[i-1][j] + a[i][j + 1]
For every digit j smaller than 9.
We consider previous length count and count
to be increased by all greater digits.
我们构建了一个矩阵 a[][],其中a[i][j] = 以 j 或大于 j 作为前导数字的所有有效 i 位非递减整数的计数。解决方案基于以下观察结果。我们按列填充这个矩阵,首先计算 a[1][9] 然后使用这个值计算 a[2][8] 等等。
在任何时刻,如果我们希望计算 a[i][j] 表示前导数字为 j 或大于 j 的数字的 i 位非递减整数的个数,我们应该将 a[i-1][j] (应该从 j 或更大的数字开始的 i-1 位整数的数量,因为在这种情况下,如果我们将 j 作为其最左边的数字,那么我们的数字将是 i 位非递减数)和 a[i][j+ 1](应以 digit 开头的 i 位整数的数量等于大于 j+1)。所以, a[i][j] = a[i-1][j] + a[i][j+1] 。
C++
// C++ program for counting n digit numbers with
// non decreasing digits
#include
using namespace std;
// Returns count of non- decreasing numbers with
// n digits.
int nonDecNums(int n)
{
/* a[i][j] = count of all possible number
with i digits having leading digit as j */
int a[n + 1][10];
// Initialization of all 0-digit number
for (int i = 0; i <= 9; i++)
a[0][i] = 1;
/* Initialization of all i-digit
non-decreasing number leading with 9*/
for (int i = 1; i <= n; i++)
a[i][9] = 1;
/* for all digits we should calculate
number of ways depending upon leading
digits*/
for (int i = 1; i <= n; i++)
for (int j = 8; j >= 0; j--)
a[i][j] = a[i - 1][j] + a[i][j + 1];
return a[n][0];
}
// driver program
int main()
{
int n = 2;
cout << "Non-decreasing digits = "
<< nonDecNums(n) << endl;
return 0;
}
Java
// Java program for counting n digit numbers with
// non decreasing digits
import java.io.*;
class GFG {
// Function that returns count of non- decreasing numbers
// with n digits
static int nonDecNums(int n)
{
// a[i][j] = count of all possible number
// with i digits having leading digit as j
int[][] a = new int[n + 1][10];
// Initialization of all 0-digit number
for (int i = 0; i <= 9; i++)
a[0][i] = 1;
// Initialization of all i-digit
// non-decreasing number leading with 9
for (int i = 1; i <= n; i++)
a[i][9] = 1;
// for all digits we should calculate
// number of ways depending upon leading
// digits
for (int i = 1; i <= n; i++)
for (int j = 8; j >= 0; j--)
a[i][j] = a[i - 1][j] + a[i][j + 1];
return a[n][0];
}
// driver program
public static void main(String[] args)
{
int n = 2;
System.out.println("Non-decreasing digits = " + nonDecNums(n));
}
}
// Contributed by Pramod Kumar
Python3
# Python3 program for counting n digit
# numbers with non decreasing digits
import numpy as np
# Returns count of non- decreasing
# numbers with n digits.
def nonDecNums(n) :
# a[i][j] = count of all possible number
# with i digits having leading digit as j
a = np.zeros((n + 1, 10))
# Initialization of all 0-digit number
for i in range(10) :
a[0][i] = 1
# Initialization of all i-digit
# non-decreasing number leading with 9
for i in range(1, n + 1) :
a[i][9] = 1
# for all digits we should calculate
# number of ways depending upon
# leading digits
for i in range(1, n + 1) :
for j in range(8, -1, -1) :
a[i][j] = a[i - 1][j] + a[i][j + 1]
return int(a[n][0])
# Driver Code
if __name__ == "__main__" :
n = 2
print("Non-decreasing digits = ",
nonDecNums(n))
# This code is contributed by Ryuga
C#
// C# function to find number of diagonals
// in n sided convex polygon
using System;
class GFG {
// Function that returns count of non-
// decreasing numbers with n digits
static int nonDecNums(int n)
{
// a[i][j] = count of all possible number
// with i digits having leading digit as j
int[, ] a = new int[n + 1, 10];
// Initialization of all 0-digit number
for (int i = 0; i <= 9; i++)
a[0, i] = 1;
// Initialization of all i-digit
// non-decreasing number leading with 9
for (int i = 1; i <= n; i++)
a[i, 9] = 1;
// for all digits we should calculate
// number of ways depending upon leading
// digits
for (int i = 1; i <= n; i++)
for (int j = 8; j >= 0; j--)
a[i, j] = a[i - 1, j] + a[i, j + 1];
return a[n, 0];
}
// driver program
public static void Main()
{
int n = 2;
Console.WriteLine("Non-decreasing digits = " +
nonDecNums(n));
}
}
// This code is contributed by Sam007
PHP
= 0; $j--)
$a[$i][$j] = $a[$i - 1][$j] +
$a[$i][$j + 1];
return $a[$n][0];
}
// Driver Code
$n = 2;
echo "Non-decreasing digits = ",
nonDecNums($n),"\n";
// This code is contributed by m_kit
?>
Javascript
C++
// CPP program To calculate Number of n-digits non-decreasing integers
//Contributed by Parishrut Kushwaha//
#include
using namespace std;
// Returns factorial of n
long long int fact(int n)
{
long long int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
// returns nCr
long long int nCr(int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
// Driver code
int main()
{
int n = 2;
cout <<"Number of Non-Decreasing digits: "<< nCr(n+9,9);
return 0;
}
输出 :
Non-decreasing digits = 55
时间复杂度: O(10*n) 等价于 O(n)。
另一种方法:
如果我们观察,我们可以看到 0 必须放在 1-9 之前,1 必须放在 2-9 之前,依此类推。当我们被要求找到非递减整数时,111223 是一个有效的非递减整数,这意味着相同的数字可以连续出现。
例 1 :当 N=2 时,我们有 11C9,等于55。
例 2 :当 N=5 时,我们有 14C9,它等于2002。
C++
// CPP program To calculate Number of n-digits non-decreasing integers
//Contributed by Parishrut Kushwaha//
#include
using namespace std;
// Returns factorial of n
long long int fact(int n)
{
long long int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
// returns nCr
long long int nCr(int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
// Driver code
int main()
{
int n = 2;
cout <<"Number of Non-Decreasing digits: "<< nCr(n+9,9);
return 0;
}
输出:
Number of Non-Decreasing digits: 24310
时间复杂度: O(n)。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。