--
Jan 2020 : 4 FAANG OFFERS. 2019: 15 FAANG OFFERS
https://www.techbayarea.us/
FB: https://www.facebook.com/techbayarea.us
Discord: https://discord.gg/yWMWptS
meetup : https://www.meetup.com/techbayarea/
google groups: https://groups.google.com/forum/#!forum/leetcode-meetup
---
You received this message because you are subscribed to the Google Groups "leetcode-meetup" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leetcode-meet...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/leetcode-meetup/0cb3d47c-7c94-4f60-a24d-0927cbfcc021o%40googlegroups.com.
| 453 | Minimum Moves to Equal Array Elements | 50.0% | Easy |
| 462 | Minimum Moves to Equal Array Elements II | 53.5% | Medium |
--
Jan 2020 : 4 FAANG OFFERS. 2019: 15 FAANG OFFERS
https://www.techbayarea.us/
FB: https://www.facebook.com/techbayarea.us
Discord: https://discord.gg/yWMWptS
meetup : https://www.meetup.com/techbayarea/
google groups: https://groups.google.com/forum/#!forum/leetcode-meetup
---
You received this message because you are subscribed to the Google Groups "leetcode-meetup" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leetcode-meet...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/leetcode-meetup/341046ad-0951-48b2-93b8-6e80ce60db3bo%40googlegroups.com.
462. Minimum moves to equal array elements II
Using quick select.
Time: O(n), average case.
Space: O(1)
class Solution {
public int minMoves2(int[] nums) {
if (nums == null || nums.length == 0)
return 0;
int k = nums.length/2;
int median = quickSelect(nums, 0, nums.length-1, k);
int dist = 0;
for(int num : nums) {
dist += Math.abs(median-num);
}
return dist;
}
private int quickSelect(int[] nums, int low, int high, int k) {
int partitionIndex = partition(nums, low, high);
if ( k == partitionIndex)
return nums[partitionIndex];
else if (partitionIndex < k)
return quickSelect(nums, partitionIndex + 1, high, k);
else return
quickSelect(nums, low, partitionIndex - 1, k);
}
private int partition (int[] nums, int low, int high) {
int pivot = nums[low + (high-low)/2];
swap(nums, low + (high-low)/2, high);
int i = low;
for (int j = low; j <= high; j++) {
if (nums[j] < pivot){
swap(nums, i, j);
i++;
}
}
swap(nums, i, high);
return i;
}
private void swap(int[] nums, int i, int j){
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
}
| 453 | Minimum Moves to Equal Array Elements | 50.0% | Easy |
| 462 | Minimum Moves to Equal Array Elements II | 53.5% | Medium |
| 295 | Find Median from Data Stream | 43.1% | Hard |
--
Jan 2020 : 4 FAANG OFFERS. 2019: 15 FAANG OFFERS
https://www.techbayarea.us/
FB: https://www.facebook.com/techbayarea.us
Discord: https://discord.gg/yWMWptS
meetup : https://www.meetup.com/techbayarea/
google groups: https://groups.google.com/forum/#!forum/leetcode-meetup
---
You received this message because you are subscribed to the Google Groups "leetcode-meetup" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leetcode-meet...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/leetcode-meetup/51228ad9-c10d-4e78-86e7-6f41751c7678o%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/leetcode-meetup/CAPWrNBiJg-KOV11cEZW%2BinWPVwx-8AkVLjQCipxczQO%3DPoW%2B1g%40mail.gmail.com.