leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1040.cpp (631B)


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