【Leetcode】453. Minimum Moves to Equal Array Elements
題目地址:
https://leetcode.com/problems/minimum-moves-to-equal-array-elements/
給定一個長 n n n陣列 A A A,每次操作允許將其中的 n − 1 n − 1 n−1個數進行加 1 1 1的操作,問至少進行多少次操作可以使得 A A A的所有數都相等。
參考https://blog.csdn.net/qq_46105170/article/details/110678718。程式碼如下:
public class Solution {
public int minMoves(int[] nums) {
int res = 0, min = Integer.MAX_VALUE;
for (int num : nums) {
min = Math.min(min, num);
}
for (int num : nums) {
res += num - min;
}
return res;
}
}
時間複雜度 O ( n ) O(n) O(n),空間 O ( 1 ) O(1) O(1)。
相關文章
- Minimum Moves to Equal Array Elements 最小移動次數使陣列元素相等陣列
- LeetCode | 153. Find Minimum in Rotated Sorted ArrayLeetCode
- LeetCode C++ 1464. Maximum Product of Two Elements in an Array【Array/Sort】簡單LeetCodeC++
- Find Minimum in Rotated Sorted Array I & II
- [LeetCode] 2684. Maximum Number of Moves in a GridLeetCode
- [LeetCode] 727. Minimum Window SubsequenceLeetCode
- [LeetCode] 416. Partition Equal Subset SumLeetCode
- [LeetCode] 2831. Find the Longest Equal SubarrayLeetCode
- Leetcode Sort ArrayLeetCode
- LeetCode のminimum-depth-of-binary-treeLeetCode
- LeetCode 209. Minimum Size Subarray Sum ?LeetCode
- Leetcode 209. Minimum Size Subarray SumLeetCode
- [LeetCode] 2491. Divide Players Into Teams of Equal SkillLeetCodeIDE
- Rotate Array@LeetCodeLeetCode
- [LeetCode] 857. Minimum Cost to Hire K WorkersLeetCode
- [LeetCode] 671. Second Minimum Node In a Binary TreeLeetCode
- [LeetCode] 3096. Minimum Levels to Gain More PointsLeetCodeAI
- [LeetCode] 2406. Divide Intervals Into Minimum Number of GroupsLeetCodeIDE
- [leetcode] 1624. Largest Substring Between Two Equal CharactersLeetCode
- Leetcode 347. Top K Frequent ElementsLeetCode
- Leetcode 203. Remove Linked List ElementsLeetCodeREM
- LeetCode | 203. Remove Linked List ElementsLeetCodeREM
- LeetCode Kth Largest Element in an ArrayLeetCode
- LeetCode Patching Array All In OneLeetCode
- [LeetCode] 3226. Number of Bit Changes to Make Two Integers EqualLeetCode
- LeetCode 1305 All Elements in Two Binary Search TreesLeetCode
- [LeetCode] 1750. Minimum Length of String After Deleting Similar EndsLeetCodeMILA
- LeetCode 452. Minimum Number of Arrows to Burst Balloons Sort/MediumLeetCode
- Leetcode 33 Search in Rotated Sorted ArrayLeetCode
- LeetCode之Squares of a Sorted Array(Kotlin)LeetCodeKotlin
- LeetCode之Sort Array By Parity(Kotlin)LeetCodeKotlin
- Leetcode 88. Merge Sorted ArrayLeetCode
- [LeetCode] 3152. Special Array IILeetCode
- [LeetCode] 3239. Minimum Number of Flips to Make Binary Grid Palindromic ILeetCode
- Leetcode 26 Remove Duplicates from Sorted ArrayLeetCodeREM
- [LeetCode] 852. Peak Index in a Mountain ArrayLeetCodeIndexAI
- LeetCode Shuffle an Array(打亂陣列)LeetCode陣列
- Leetcode 442. Find All Duplicates in an ArrayLeetCode