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)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!