Advertisement
knakul853

Untitled

Sep 23rd, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     vector<int> numMovesStonesII(vector<int>& A)
  4.     {
  5.         int n = A.size();
  6.         sort(A.begin() , A.end());
  7.         int up = max( A[n-1] - A[1] - n + 2 , A[n-2] - n - A[0] + 2 ); // place corner stone to next unoccupied
  8.         int down = n;                                       //place and you will get maximum ....
  9.         int j=0;
  10.         for(int i=0;i<n;i++)
  11.         {                       // for minimum try to fit all elements in a window of size n.
  12.             while(A[i] - A[j] >= n) j++;
  13.            
  14.             if((i - j + 1) == n-1 && A[i] - A[j] == n-2)
  15.                 down = min(down , 2);
  16.             else
  17.                 down = min(down , (n - (i - j +1)));
  18.         }
  19.         return {down , up};
  20.        
  21.     }
  22. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement