Share my Java concise solution

  • 0
    public class Solution {
        public int minMoves2(int[] nums) {
            int n = nums.length;
            int pivot = nums[n / 2];
            int count = 0;
            for (int num : nums) count += Math.abs(num - pivot);
            return count;

  • 0

    Pretty cool. It's not very easy to see why they must always meet at the median though.
    Can you prove it? How would you justify that during an interview?

  • 1

    @rmn Hi rmn, I think this problem is just a 1D version of Best Meeting Point. Imagine if we have two persons on a straight line, then any point in between is the best meet point(including these two points). If we have three people on a straight line, the we should meet at the middle point. You can try if there are many persons on a line.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.