Thursday, May 28, 2015

L Question: Sequence for Particular digits

public class DFLSeq {
 public int numSeq(int ones, int twos, int threes, int fours){
  System.out.println("numSeq " + ones + " " + twos + " " + threes + " " +fours);

   return numSeqHelper(ones, twos, threes, fours, 0) ; 
 }
 public static void main(String[] args) {
  int[] n = {4, 1, 1, 1};
  DFLSeq  d = new DFLSeq();
  System.out.println( d.numSeq(n[0],n[1],n[2],n[3]));
 }
 public int numSeqHelper(int ones, int twos, int threes, int fours, int last){
  if(ones ==0 && twos ==0 && threes ==0 && fours ==0)
   return 1; 
  System.out.println("" + ones + " " + twos + " " + threes + " " +fours + " last=" +last);
  int onesSeq = 0; 
  int twosSeq = 0;
  int threesSeq = 0;
  int foursSeq = 0;

  if(last!= 1 && ones != 0)
   onesSeq = numSeqHelper(ones-1, twos, threes, fours, 1); 
  if(last!= 2 && twos != 0)
   twosSeq = numSeqHelper(ones, twos-1, threes, fours, 2); 
  if(last!= 3 && threes != 0)
   threesSeq = numSeqHelper(ones, twos, threes-1, fours, 3); 
  if(last!= 4 && fours != 0)
   foursSeq = numSeqHelper(ones, twos, threes, fours-1, 4); 

  int res= onesSeq +twosSeq+ threesSeq + foursSeq;  
  System.out.println("res="+res);
  return res;
 }
}

No comments:

Post a Comment