Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal.In one move, you can increment or decrement an element of the array by 1.
Example 1:
Input: nums = [1,2,3]
Output: 2
Explanation:
Only two moves are needed (remember each move increments or decrements one element):
[1,2,3] => [2,2,3] => [2,2,2]
Example 2:Input: nums = [1,10,2,9]
Output: 16
There is no while keyword in go.
func minMoves2(nums []int) int {
var l = 0
var r = len(nums) - 1
var sum = 0
sort.Ints(nums)
for l < r {
sum += nums[r] - nums[l]
l ++;
r --;
}
return sum;
}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Thank you for reading ^^^^^^^^^^^^^^^
NEW! Following my Trail (Upvote or/and Downvote)
Follow me for topics of Algorithms, Blockchain and Cloud.
I am @justyy - a Steem Witness
https://steemyy.com
My contributions
- Video Downloader
- Steem Blockchain Tools
- Free Cryptos API
- VPS Database
- Computing Technology Blog
- A few useless tools
- And some other online software/tools
- Merge Files/Videos
- LOGO Turtle Programming Chrome Extension
- Teaching Kids Programming - Youtube Channel and All Contents
Delegation Service
Support me
If you like my work, please:
- Buy Me a Coffee, Thanks!
- Become my Sponsor, Thanks!
- Voting for me:
https://steemit.com/~witnesses type in justyy and click VOTE
- Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
- Vote @justyy as Witness: https://steemyy.com/witness-voting/?witness=justyy&action=approve
- Set @justyy as Proxy: https://steemyy.com/witness-voting/?witness=justyy&action=proxy
Alternatively, you can vote witness or set proxy here: https://steemit.com/~witnesses
如何创建?不是很懂,请大佬指教。😊😊
[WhereIn Android] (http://www.wherein.io)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
hi brother . you know brother 😉
[WhereIn Android] (http://www.wherein.io)
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Hy @Justyy I knew about you today, and my witness vote surely for you... 🙂
You are doing extra ordinary great work.. more power to you !
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank yoU!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
You are welcome !
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit