My Blog List

Tuesday, May 10, 2016

MaxCupCake in codefights

this is good problem.

https://codefights.com/challenge/X33imTX2FSqhSLTtJ


My code is

        int c, i,t;
 int MaxCupCakes(int N, int[] P, int K) {
  for (; i < P.length; i++ ) {
   t= (P[i] -c -1);
   if(K<=t) break;
   K=K-t;
   c=P[i];
  }  
  return c+K<= N ?c+K : -1;  
 }


best code is here
 int MaxCupCakes(int n, int[] P, int k) {
     for(int i:P) 
         if(i<=k)k++;
           
  return  k>n ?-1 : k ;
 }




Max and Caroline, two girls in their mid-twenties, work at a Brooklyn restaurant as waitresses. Together, they dream of starting up their cupcake business.
One day Max comes with a box of N cupcakes numbered according to their quality from 1 to N. Caroline has a list of cupcakes P that should be removed from the box.
Your task it to find the quality of the Kth cupcake after the cupcakes from the list P are removed from the box.
If it is not possible to get the Kth cupcake, return -1 instead.
Example
For N = 4P = [1] and K = 2, the output should be MaxCupCakes(N, P, K) = 3.
Initially there were cupcakes of the following quality: 1, 2, 3, 4. According to P, the cupcake with quality 1 should be removed, so only the following cupcakes are left: 2, 3, 4. The 2nd cupcake in this list is 3, thus the output should be 3 as well.
  • [input] integer N
    The number of cupcakes, 4 ≤ N ≤ 109.
  • [input] array.integer P
    A sorted array of positive integers, the cupcakes to be removed. 0 ≤ P.length ≤ 500, 1 ≤ P[i] ≤ N.
  • [input] integer K
    A positive integer, the 1-based number of the cupcake to find.
  • [output] integer
    The quality of the Kth cupcake, or -1 if less than K cupcakes are left.

No comments:

Post a Comment