classSolution: defminMoves2(self, nums: List[int]) -> int: nums.sort() returnsum(abs(num - nums[len(nums) // 2]) for num in nums)
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11
classSolution { public: intminMoves2(vector<int>& nums){ sort(nums.begin(), nums.end()); int n = nums.size(), ret = 0, x = nums[n / 2]; for (int i = 0; i < n; i++) { ret += abs(nums[i] - x); } return ret; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10
classSolution { publicintminMoves2(int[] nums) { Arrays.sort(nums); intn= nums.length, ret = 0, x = nums[n / 2]; for (inti=0; i < n; i++) { ret += Math.abs(nums[i] - x); } return ret; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10
publicclassSolution { publicintMinMoves2(int[] nums) { Array.Sort(nums); int n = nums.Length, ret = 0, x = nums[n / 2]; for (int i = 0; i < n; i++) { ret += Math.Abs(nums[i] - x); } return ret; } }
intminMoves2(int* nums, int numsSize){ qsort(nums, numsSize, sizeof(int), cmp); int ret = 0, x = nums[numsSize / 2]; for (int i = 0; i < numsSize; i++) { ret += abs(nums[i] - x); } return ret; }
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
funcminMoves2(nums []int) (ans int) { sort.Ints(nums) x := nums[len(nums)/2] for _, num := range nums { ans += abs(num - x) } return }
funcabs(x int)int { if x < 0 { return -x } return x }
[sol1-JavaScript]
1 2 3 4 5 6 7 8
var minMoves2 = function(nums) { nums.sort((a, b) => a - b); let n = nums.length, ret = 0, x = nums[Math.floor(n / 2)]; for (let i = 0; i < n; i++) { ret += Math.abs(nums[i] - x); } return ret; };
classSolution { public: intquickSelect(vector<int>& nums, int left, int right, int index){ int q = randomPartition(nums, left, right); if (q == index) { return nums[q]; } else { return q < index ? quickSelect(nums, q + 1, right, index) : quickSelect(nums, left, q - 1, index); } }
inlineintrandomPartition(vector<int>& nums, int left, int right){ int i = rand() % (right - left + 1) + left; swap(nums[i], nums[right]); returnpartition(nums, left, right); }
inlineintpartition(vector<int>& nums, int left, int right){ int x = nums[right], i = left - 1; for (int j = left; j < right; ++j) { if (nums[j] <= x) { swap(nums[++i], nums[j]); } } swap(nums[i + 1], nums[right]); return i + 1; }
intminMoves2(vector<int>& nums){ srand(time(0)); int n = nums.size(), x = quickSelect(nums, 0, n - 1, n / 2), ret = 0; for (int i = 0; i < n; ++i) { ret += abs(nums[i] - x); } return ret; } };
publicintminMoves2(int[] nums) { intn= nums.length, x = quickSelect(nums, 0, n - 1, n / 2), ret = 0; for (inti=0; i < n; ++i) { ret += Math.abs(nums[i] - x); } return ret; }
publicintquickSelect(int[] nums, int left, int right, int index) { intq= randomPartition(nums, left, right); if (q == index) { return nums[q]; } else { return q < index ? quickSelect(nums, q + 1, right, index) : quickSelect(nums, left, q - 1, index); } }
publicintrandomPartition(int[] nums, int left, int right) { inti= random.nextInt(right - left + 1) + left; swap(nums, i, right); return partition(nums, left, right); }
publicintpartition(int[] nums, int left, int right) { intx= nums[right], i = left - 1; for (intj= left; j < right; ++j) { if (nums[j] <= x) { ++i; swap(nums, i, j); } } swap(nums, i + 1, right); return i + 1; }
publicvoidswap(int[] nums, int index1, int index2) { inttemp= nums[index1]; nums[index1] = nums[index2]; nums[index2] = temp; } }
publicclassSolution { Random random = new Random();
publicintMinMoves2(int[] nums) { int n = nums.Length, x = QuickSelect(nums, 0, n - 1, n / 2), ret = 0; for (int i = 0; i < n; ++i) { ret += Math.Abs(nums[i] - x); } return ret; }
publicintQuickSelect(int[] nums, int left, int right, int index) { int q = RandomPartition(nums, left, right); if (q == index) { return nums[q]; } else { return q < index ? QuickSelect(nums, q + 1, right, index) : QuickSelect(nums, left, q - 1, index); } }
publicintRandomPartition(int[] nums, int left, int right) { int i = random.Next(right - left + 1) + left; Swap(nums, i, right); return partition(nums, left, right); }
publicintpartition(int[] nums, int left, int right) { int x = nums[right], i = left - 1; for (int j = left; j < right; ++j) { if (nums[j] <= x) { ++i; Swap(nums, i, j); } } Swap(nums, i + 1, right); return i + 1; }
publicvoidSwap(int[] nums, int index1, int index2) { int temp = nums[index1]; nums[index1] = nums[index2]; nums[index2] = temp; } }
staticinlinevoidswap(int *a, int *b) { int c = *a; *a = *b; *b = c; }
staticinlineintpartition(int *nums, int left, int right) { int x = nums[right], i = left - 1; for (int j = left; j < right; ++j) { if (nums[j] <= x) { swap(&nums[++i], &nums[j]); } } swap(&nums[i + 1], &nums[right]); return i + 1; }
staticinlineintrandomPartition(int *nums, int left, int right) { int i = rand() % (right - left + 1) + left; swap(&nums[i], &nums[right]); return partition(nums, left, right); }
staticintquickSelect(int *nums, int left, int right, int index) { int q = randomPartition(nums, left, right); if (q == index) { return nums[q]; } else { return q < index ? quickSelect(nums, q + 1, right, index) : quickSelect(nums, left, q - 1, index); } }
intminMoves2(int* nums, int numsSize){ srand(time(0)); int n = numsSize, x = quickSelect(nums, 0, n - 1, n / 2), ret = 0; for (int i = 0; i < n; ++i) { ret += abs(nums[i] - x); } return ret; }
var minMoves2 = function(nums) { let n = nums.length, x = quickSelect(nums, 0, n - 1, Math.floor(n / 2)), ret = 0; for (let i = 0; i < n; ++i) { ret += Math.abs(nums[i] - x); } return ret; }