Saturday, March 25, 2017

#codingexercise
Count all increasing subsequences in a given integer array
Void  Combine(ref List<int> digits, ref List<int> b, int start, int level, ref int count) 
for (int I =start; I < digits.length; I++) 
 
   if (b.contains(digits[i])== false){ 
     b[level] = digits[i]; 
     count += GetIS(b); 
  if (I < digits.length) 
    Combine(ref digits, ref b, m, start+1, level+1, ref count); 
  b[level] = '/0'; 
int GetIS(List<int> A)
{
count = 0;
for (int i = 0; i < A.count; i++)
    for (int j = start; j < i; j++)
       if (A[i] < A[j])
           return count;
return 1;
}
Alternatively,
int GetCountIS(List<int> A, int n)
{
    int result[n];
    for (int i = 0; i < n; i++ )
      result[i] = 1;
    for (int i = 1; i < n; i++ )
         for (int j = 0; j < i; j++ )
             if ( A[i] > A[j])
               result[i] += result[j];

    int count = 0;
   
    for (int i = 0; i < n; i++ )
          count  += result[i];
    return count
}

No comments:

Post a Comment