Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<int> numMovesStonesII(vector<int>& A)
- {
- int n = A.size();
- sort(A.begin() , A.end());
- int up = max( A[n-1] - A[1] - n + 2 , A[n-2] - n - A[0] + 2 ); // place corner stone to next unoccupied
- int down = n; //place and you will get maximum ....
- int j=0;
- for(int i=0;i<n;i++)
- { // for minimum try to fit all elements in a window of size n.
- while(A[i] - A[j] >= n) j++;
- if((i - j + 1) == n-1 && A[i] - A[j] == n-2)
- down = min(down , 2);
- else
- down = min(down , (n - (i - j +1)));
- }
- return {down , up};
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement