【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)。
相關文章
- Leetcode 453. Minimum Moves to Equal Array ElementsLeetCode
- Minimum Moves to Equal Array Elements 最小移動次數使陣列元素相等陣列
- Find Minimum in Rotated Sorted Array leetcode javaLeetCodeJava
- Leetcode-Find Minimum in Rotated Sorted ArrayLeetCode
- Leetcode-Find Minimum in Rotated Sorted Array IILeetCode
- LeetCode | 153. Find Minimum in Rotated Sorted ArrayLeetCode
- Leetcode 153. Find Minimum in Rotated Sorted ArrayLeetCode
- LeetCode C++ 1464. Maximum Product of Two Elements in an Array【Array/Sort】簡單LeetCodeC++
- [LeetCode] Find Minimum in Rotated Sorted Array (包含遞增和遞減旋轉)LeetCode
- Leetcode - Partition Equal Subset SumLeetCode
- Leetcode Minimum Path SumLeetCode
- Leetcode-Minimum Path SumLeetCode
- [LeetCode] Minimum Size Subarray SumLeetCode
- Leetcode Minimum Window SubstringLeetCode
- Minimum Path Sum leetcode javaLeetCodeJava
- Leetcode Sort ArrayLeetCode
- LeetCode-Top K Frequent ElementsLeetCode
- [LeetCode] 727. Minimum Window SubsequenceLeetCode
- LeetCode-Minimum Size Subarray SumLeetCode
- LeetCode-Minimum Height TreesLeetCode
- Leetcode Minimum Depth of Binary TreeLeetCode
- Leetcode-Minimum Window SubstringLeetCode
- Minimum Window Substring leetcode javaLeetCodeJava
- [LeetCode] 416. Partition Equal Subset SumLeetCode
- [LeetCode] 2831. Find the Longest Equal SubarrayLeetCode
- Leetcode 347. Top K Frequent ElementsLeetCode
- Rotate Array@LeetCodeLeetCode
- Leetcode - Patching ArrayLeetCode
- LeetCode-Rotate ArrayLeetCode
- LeetCode-Shuffle an ArrayLeetCode
- Leetcode-Minimum Depth of Binary TreeLeetCode
- Minimum Depth of Binary Tree leetcode javaLeetCodeJava
- [LeetCode] 2491. Divide Players Into Teams of Equal SkillLeetCodeIDE
- leetcode刷題--Remove Linked List ElementsLeetCodeREM
- LeetCode-Pathcing ArrayLeetCode
- Leetcode Search in Rotated Sorted ArrayLeetCode
- Leetcode Merge Sorted ArrayLeetCode
- leetcode刷題--Rotate ArrayLeetCode