Sort by

recency

|

15 Discussions

|

  • + 0 comments

    Here is my solution in java, javascript, python, C, C++, Csharp HackerRank Extremum Permutations Solution

  • + 0 comments

    Here is Extremum Permutations problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-extremum-permutations-problem-solution.html

  • + 0 comments
    import sys
    
    N, K, L = map(int,input().split())
    mins = list(map(int, input().split()))
    maxs = list(map(int, input().split()))
    
    M = int(1e9 + 7)
    ANY = 0
    UP = 1
    DOWN = -1
    direction = [0] * (N + 1)
    
    for i in mins:
        if direction[i - 1] == UP or direction[i] == DOWN:
            print("0")
            sys.exit(0)
        direction[i - 1] = DOWN
        direction[i] = UP
    for i in maxs:
        if direction[i - 1] == DOWN or direction[i] == UP:
            print("0")
            sys.exit(0)
        direction[i - 1] = UP
        direction[i] = DOWN
    f = []
    for i in range(N + 1):
        f.append([0] * (N + 1))
    
    def interval(a, b):
        if a <= b:
            return range(a, b + 1)
        else:
            return range(a, b - 1, -1)
    
    f[N][0] = 1
    i = N - 1
    while i > 0:
        if direction[i] == UP:
            f[i][0] = 0
            for n in interval(1, N - i):
                f[i][n] = (f[i][n - 1] + f[i + 1][n - 1]) % M       
        elif direction[i] == DOWN:
            f[i][N - i] = 0
            for n in interval(N - i - 1, 0):
                m = N - i - n
                f[i][n] = (f[i][n + 1] + f[i + 1][n]) % M  
        elif direction[i] == ANY:
            s = 0
            for k in interval(0, N - (i + 1)):
                s += f[i + 1][k]
                s %= M
            for n in interval(0, N - i):
                f[i][n] = s
        i -= 1
    
    ret = 0
    for k in interval(0, N - 1):
        ret += f[1][k]
        ret %= M
    print(ret)
    
  • + 0 comments

    My code generates permutations in a recursive way. While a new element is being added to the current permutation, my code checks whether it complients or not to the rules. As far 4 testcases are green, the others got timeout. I don't know how could I fast up my solution. As I see I cannot share my solution here anymore. There is some regulation here.

  • + 1 comment
    In order to solve this problem, first you need to know how to use dp to solve permuation problem.
    ex: N=3, dp[i][j], i means total numbers, j means last element. (j<=i)
    
    dp[1][1] = 1			//1
    
    dp[2][1] = sum(dp[1]);		//(1) 1	=> 2 1 (replace 1 with 2)
    dp[2][2] = sum(dp[1]);		//1 2
    
    dp[3][1] = sum(dp[2]);		//2 (1) 1 => 2 3 1 (replace 1 with 3)
    				//(1) 2 1 => 3 2 1 (replace 1 with 3)
    								
    dp[3][2] = sum(dp[2]);		//(2) 1 2 => 3 1 2 (replace 2 with 3)
    				//1 (2) 2 => 1 3 2 (replace 2 with 3)
    								
    dp[3][3] = sum(dp[2]);		//2 1 3
    				//1 2 3
    								
    total permutation is sum(dp[3]) = 6.
    
    
    Now we need to think about constraints a and b. 
    We can define as following:
    a(i)=1 as a has ith position element
    b(i)=1 as b has ith position element.
    
    There are total three situations for dp[i][j]:
    
    if a(i) == 1 or b(i-1) == 1		(include only bigger sets)
    	sum(dp[i-1][j] + dp[i-1][j+1] ... dp[i-1][i-1])
    else if b(i) == 1 or a(i-1) == 1	(include only smaller sets)
    	sum(dp[i-1][1] + dp[i-1][2] ... dp[i-1][j-1])
    else
    	sum(dp[i-1])
    
    
    Ex: N = 4 a1 = 2 and b1 = 3. The 5 permutations of {1,2,3,4} that satisfy the condition are
    2 1 4 3
    3 2 4 1
    4 2 3 1
    3 1 4 2
    4 1 3 2  
    
    
    dp[1][1] = 1			//1
    --------------------------------------------------------------
    a(2) = 1
    dp[2][2] = 0	    		//j=2, no bigger sets
    dp[2][1] = dp[1][1]		//(1) 1 => 2 1
    ---------------------------------------------------------------
    b(3) = 1
    dp[3][3] = dp[2][2]		//0
    		   + dp[2][1]  	//2 1 3
    		 
    dp[3][2] = dp[2][1]		//(2) 1 2 => 3 1 2
    
    dp[3][1] = 0			//j=1, no smaller sets
    ---------------------------------------------------------------
    b(3) = 1
    dp[4][4] = 0			//j=4, no bigger sets
    
    dp[4][3] = dp[3][3]		//2 1 (3) 3 => 2 1 4 3
    
    dp[4][2] = dp[3][3]		//(2) 1 3 2 => 4 1 3 2
    		   +dp[3][2]	//3 1 (2) 2 => 3 1 4 2
    		   
    dp[4][1] = dp[3][3]		//2 (1) 3 1 => 2 4 3 1 => 4 2 3 1
    		   +dp[3][2]	//3 (1) 2 1 => 3 4 2 1 => 3 2 4 1
    		   +dp[3][1]	//0
    
    You may realize we can always change order of dp[i-1], because total permutation doesn't change.
    ex:   2 1 3, we have relationship 2 > 1 < 3
    after change to 2 4 3, we can always have same relationship 4 > 2 < 3.
    
    result sum(dp[4]) = 5