当前位置:网站首页>LeetCode 1551. Minimum operand to make all elements in the array equal
LeetCode 1551. Minimum operand to make all elements in the array equal
2022-07-06 16:42:00 【Daylight629】
1551. The minimum operands that make all elements in an array equal
There is a length of n
Array of arr
, among arr[i] = (2 * i) + 1
( 0 <= i < n
).
In one operation , You can choose two subscripts , Write it down as x
and y
( 0 <= x, y < n
) And make arr[x]
subtract 1
、arr[y]
add 1
( namely arr[x] -=1
And arr[y] += 1
). The ultimate goal is to make all elements in the array equal . The title test case will Guarantee : After several steps , All elements in the array can eventually be equal .
Give you an integer n
, That's the length of the array . Please return the array arr
Required for all elements to be equal Minimum operands .
Example 1:
Input :n = 3
Output :2
explain :arr = [1, 3, 5]
Select... For the first operation x = 2 and y = 0, Make array [2, 3, 4]
The second operation continues x = 2 and y = 0, The array will become [3, 3, 3]
Example 2:
Input :n = 6
Output :9
Tips :
1 <= n <= 10^4
Two 、 Method 1
greedy
class Solution {
public int minOperations(int n) {
int res = 0;
for (int i = 0; i < n; i++) {
int x = (2 * i) + 1;
if (x > n) {
res += x - n;
}
}
return res;
}
}
Complexity analysis
Time complexity :O(n).
Spatial complexity :O(1).
3、 ... and 、 Method 2
mathematics
class Solution {
public int minOperations(int n) {
return n * n / 4;
}
}
Complexity analysis
Time complexity :O(1).
Spatial complexity :O(1).
边栏推荐
- Li Kou leetcode 280 weekly match
- MP4格式详解
- (lightoj - 1370) Bi shoe and phi shoe (Euler function tabulation)
- Codeforces Global Round 19
- Effet d'utilisation, déclenché lorsque les composants de la fonction sont montés et déchargés
- Oneforall installation and use
- Problem - 922D、Robot Vacuum Cleaner - Codeforces
- Chapter 1 overview of MapReduce
- 我在字节跳动「修电影」
- QT realizes window topping, topping state switching, and multi window topping priority relationship
猜你喜欢
js封装数组反转的方法--冯浩的博客
Simply try the new amp model of deepfacelab (deepfake)
FLV格式详解
Spark独立集群Worker和Executor的概念
简单尝试DeepFaceLab(DeepFake)的新AMP模型
本地可视化工具连接阿里云centOS服务器的redis
Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)
Sublime text code formatting operation
Story of [Kun Jintong]: talk about Chinese character coding and common character sets
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
随机推荐
Market trend report, technological innovation and market forecast of desktop electric tools in China
Kubernetes集群部署
【锟斤拷】的故事:谈谈汉字编码和常用字符集
Spark独立集群Worker和Executor的概念
第五章 Yarn资源调度器
Codeforces Round #800 (Div. 2)AC
Research Report on market supply and demand and strategy of China's tetraacetylethylenediamine (TAED) industry
(lightoj - 1236) pairs forming LCM (prime unique decomposition theorem)
Useeffect, triggered when function components are mounted and unloaded
Anaconda下安装Jupyter notebook
第三章 MapReduce框架原理
sublime text 代码格式化操作
Market trend report, technical innovation and market forecast of tabletop dishwashers in China
<li>圆点样式 list-style-type
Raspberry pie 4B installation opencv3.4.0
Log statistics (double pointer)
(lightoj - 1354) IP checking (Analog)
Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)
The concept of spark independent cluster worker and executor
Chapter 6 rebalance details