Number of decimal numbers of length k, that are strict monotone
Last Updated :
28 Dec, 2022
We call a decimal number strict monotone if: D[i-1] < D[i], 0 < i < |D|
Write a program which takes positive number n on input and returns number of decimal numbers of length n that are strict monotone. Number can’t start with 0. Examples :
Input : 2
Output : 36
Numbers are 12, 13, ... 19, 23
24, ... 29, .... 89.
Input : 3
Output : 84
Explanations of this problem follows the same rules as applied on: Number of decimal numbers of length k, that are monotone The only difference is that now we cannot take duplicates, so previously computed values are the one on the left and left top diagonal.
C++
#include <cstring>
#include <iostream>
int static const DP_s = 9;
int getNumStrictMonotone( int len)
{
int DP[len][DP_s];
memset (DP, 0, sizeof (DP));
for ( int i = 0; i < DP_s; ++i)
DP[0][i] = i + 1;
for ( int i = 1; i < len; ++i)
for ( int j = 1; j < DP_s; ++j)
DP[i][j] = DP[i - 1][j - 1] + DP[i][j - 1];
return DP[len - 1][DP_s - 1];
}
int main()
{
std::cout << getNumStrictMonotone(2);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int DP_s = 9 ;
static int getNumStrictMonotone( int len)
{
int [][] DP = new int [len][DP_s];
for ( int i = 0 ; i < DP_s; ++i)
DP[ 0 ][i] = i + 1 ;
for ( int i = 1 ; i < len; ++i)
for ( int j = 1 ; j < DP_s; ++j)
DP[i][j] = DP[i - 1 ][j - 1 ]
+ DP[i][j - 1 ];
return DP[len - 1 ][DP_s - 1 ];
}
public static void main(String[] args)
{
int n = 2 ;
System.out.println(getNumStrictMonotone(n));
}
}
|
Python3
DP_s = 9
def getNumStrictMonotone(ln):
DP = [[ 0 ] * DP_s for _ in range (ln)]
for i in range (DP_s):
DP[ 0 ][i] = i + 1
for i in range ( 1 , ln):
for j in range ( 1 , DP_s):
DP[i][j] = DP[i - 1 ][j - 1 ] + DP[i][j - 1 ]
return DP[ln - 1 ][DP_s - 1 ]
print (getNumStrictMonotone( 2 ))
|
C#
using System;
class GFG {
static int DP_s = 9;
static int getNumStrictMonotone( int len)
{
int [,] DP = new int [len,DP_s];
for ( int i = 0; i < DP_s; ++i)
DP[0,i] = i + 1;
for ( int i = 1; i < len; ++i)
for ( int j = 1; j < DP_s; ++j)
DP[i,j] = DP[i - 1,j - 1]
+ DP[i,j - 1];
return DP[len - 1,DP_s - 1];
}
public static void Main()
{
int n = 2;
Console.WriteLine(getNumStrictMonotone(n));
}
}
|
PHP
<?php
$DP_s = 9;
function getNumStrictMonotone( $len )
{
global $DP_s ;
$DP = array ( array ());
for ( $i = 0; $i < $len ; $i ++)
{
for ( $j = 0; $j < $DP_s ; $j ++)
$DP [ $i ][ $j ] = 0;
}
for ( $i = 0; $i < $DP_s ; ++ $i )
$DP [0][ $i ] = $i + 1;
for ( $i = 1; $i < $len ; ++ $i )
for ( $j = 1; $j < $DP_s ; ++ $j )
$DP [ $i ][ $j ] = $DP [ $i - 1][ $j - 1] +
$DP [ $i ][ $j - 1];
return $DP [ $len - 1][ $DP_s - 1];
}
echo (getNumStrictMonotone(2));
?>
|
Javascript
<script>
const DP_s = 9;
function getNumStrictMonotone(len)
{
let DP = new Array(len);
for (let i = 0; i < len; i++){
DP[i] = new Array(DP_s).fill(0);
}
for (let i = 0; i < DP_s; ++i)
DP[0][i] = i + 1;
for (let i = 1; i < len; ++i)
for (let j = 1; j < DP_s; ++j)
DP[i][j] = DP[i - 1][j - 1] + DP[i][j - 1];
return DP[len - 1][DP_s - 1];
}
document.write(getNumStrictMonotone(2));
</script>
|
Time Complexity: O(k) where k is the given length
Auxiliary Space: O(k)
Share your thoughts in the comments
Please Login to comment...