Skip to content

Commit

Permalink
Update sort-array-by-moving-items-to-empty-space.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Nov 4, 2022
1 parent 1f9cee5 commit d6a72dc
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions C++/sort-array-by-moving-items-to-empty-space.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ class Solution {
public:
int sortArray(vector<int>& nums) {
const auto& min_moves = [&](int d) {
const auto& index = [&](int x, int d) {
const auto& index = [&](int x) {
return (x == 0) ? d * (size(nums) - 1) : x - d;
};
vector<int> a(nums);
int result = 0;
for (int i = 0; i < size(a); ++i) {
int l = 1;
bool has_zero = (a[i] == 0);
for (; index(a[i], d) != i; has_zero |= (a[i] == 0), ++l) {
swap(a[i], a[index(a[i], d)]);
for (; index(a[i]) != i; has_zero |= (a[i] == 0), ++l) {
swap(a[i], a[index(a[i])]);
}
if (l >= 2) {
result += has_zero ? l - 1 : l + 1;
Expand Down

0 comments on commit d6a72dc

Please sign in to comment.