1040.cpp (631B)
1 class Solution { 2 public: 3 vector<int> numMovesStonesII(vector<int> &stones) const { 4 const int n = size(stones); 5 vector<int> res(2, n); 6 sort(begin(stones), end(stones)); 7 8 int i = 0; 9 for (int j = 0; j < n; j++) { 10 while (stones[j] - stones[i] >= n) 11 i++; 12 if (j - i + 1 == n - 1 && stones[j] - stones[i] == n - 2) 13 res[0] = min(res[0], 2); 14 else 15 res[0] = min(res[0], n - (j - i + 1)); 16 } 17 18 res[1] = 2 + max(stones[n - 2] - stones[0], stones[n - 1] - stones[1]) - n; 19 return res; 20 } 21 };