Sort by

recency

|

13 Discussions

|

  • + 0 comments

    scala:

    import scala.io.StdIn
    
    object Solution {
      private val modulo = math.pow(10, 8).toInt + 7
    
      private def catalanNumber(n: Int): BigInt = {
        if (n < 1)
          return 1
        def factorial(end: Int, start: Int = 0, current: BigInt = 1): BigInt = {
          current * ((start + 1) to end).map(BigInt.apply).product
        }
        val fn = factorial(n)
        val fn1 = factorial(n + 1, start = n, current = fn)
        val f2n = factorial(2 * n, start = n + 1, current = fn1)
        f2n / (fn1 * fn)
      }
    
      def main(args: Array[String]): Unit = {
        val in = Iterator
          .continually(StdIn.readLine())
          .takeWhile(null != _)
          .map(_.trim)
        val t = in.next().toInt
        (1 to t)
          .map(_ => in.next().toInt)
          .map(catalanNumber)
          .map(_ % modulo)
          .foreach(println)
      }
    }
    
  • + 0 comments

    here is a java solution:

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    
    class Result {
    
    	
    	private static final int MOD = 1000000007;
    	private static final int MOD_10_8 = (int)(Math.pow(10, 8)+7);
    	
        public static void numberBST(List<Integer> numbers) {
        	
        	for(int i=0;i<numbers.size();i++){
        		int numNodesTmp = numbers.get(i);
    
        		System.out.println(Result.totalNumberBTS(numNodesTmp));
        		
        	}
        	
        }
        
        static Map<Integer, Integer> memoization = new HashMap<Integer, Integer>();
        
        /**
         * The recursive function employing memoization estrategy.
         * 
         * Lets define t(n) as total number of BST combinations for n nodes as next:
         * t(n) = sum(t(i-1)*t(n-i)) being 1<=i<=n and n=numberOfNodes 
         * 
         * The formula means that i is the root in turn of the BinarySearchTree (BST), and i has to take the value of each of the n nodes.
         * To the left of i there are t(i-1) BST combinations in which all nodes are less (equal) than i.
         * To the right of i there are t(n-i) BST combinations in which all nodes are greater (equal) than i.
         * 
         * NOTE1:
         * the nodes have to be in order, but as we ony receive the number of nodes n and we iterate over them we do it in order.
         * 
         * NOTE2:
         * the problem specifies to use modulo 10^8+7 instead of 10^9+7, its very possible its an error in the problem specification.
         * 
         * @param numNodes
         * @return
         */
        public static int totalNumberBTS(int numNodes){
        	
        	if(memoization.containsKey(numNodes)){
        		return memoization.get(numNodes);
        	}
        	if(numNodes==0){
        		return 1;
        	}
        	if(numNodes==1){
        		return 1;
        	}
        	
    		long combinaciones = 0;
    		for(int j=1;j<=numNodes;j++){
    
    			//combinaciones = (combinaciones + ((long)Result.totalNumberBTS(j-1)*Result.totalNumberBTS(numNodes-j))%(Result.MOD))%(Result.MOD);
    			
    			//We use 10^8+7 instead of 10^9+7, its very possible its an error in the problem specification
    			combinaciones = (combinaciones + ((long)Result.totalNumberBTS(j-1)*Result.totalNumberBTS(numNodes-j))%(Result.MOD_10_8))%(Result.MOD_10_8);
    			
    		}
    		memoization.put(numNodes,(int)combinaciones);
        	return (int)combinaciones;
        	
        }
        
        
    }
    
    public class Solution {
        public static void main(String[] args) throws IOException {
            //BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            BufferedReader bufferedReader = new BufferedReader(new FileReader("in_bst.txt"));
            
            int numbersCount = Integer.parseInt(bufferedReader.readLine().trim());
    
            List<Integer> numbers = new ArrayList<>();
    
            for (int i = 0; i < numbersCount; i++) {
                int numbersItem = Integer.parseInt(bufferedReader.readLine().trim());
                numbers.add(numbersItem);
            }
    
            Result.numberBST(numbers);
    
            bufferedReader.close();
        }
    }
    
  • + 1 comment

    Solution in Haskell with memoization

    bst :: Int -> Integer
    bst = (map f [0..] !!)
        where
            f 0 = 1
            f 1 = 1
            f n = sum [ bst (i-1) * bst (n-i) | i <- [1..n]] `mod` (10^8 + 7) 
            
    main :: IO()
    main = do
        raw <- getContents
        mapM_ print $ map bst $ tail $ map (read :: String -> Int) $ lines raw
    
  • + 0 comments

    any solution in java ?

  • + 0 comments

    Could you please add Python to the languages?