当前位置:网站首页>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
- Research Report on hearing health care equipment industry - market status analysis and development prospect prediction
- (lightoj - 1323) billiard balls (thinking)
- (lightoj - 1370) Bi shoe and phi shoe (Euler function tabulation)
- Research Report on market supply and demand and strategy of double drum magnetic separator industry in China
- 腾讯面试算法题
- Specify the format time, and fill in zero before the month and days
- Spark的RDD(弹性分布式数据集)返回大结果集
- 第一章 MapReduce概述
- 第7章 __consumer_offsets topic
猜你喜欢

Kubernetes集群部署

Install Jupiter notebook under Anaconda

去掉input聚焦时的边框

Installation and configuration of MariaDB

Local visualization tools are connected to redis of Alibaba cloud CentOS server

两个礼拜速成软考中级软件设计师经验
![Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)](/img/92/9465a6c9f1ab88c4851a47fabe750c.jpg)
Solve the problem of intel12 generation core CPU [small core full, large core onlookers] (win11)

提交Spark应用的若干问题记录(sparklauncher with cluster deploy mode)

业务系统兼容数据库Oracle/PostgreSQL(openGauss)/MySQL的琐事

Kubernetes cluster deployment
随机推荐
Ffmpeg command line use
Hbuilder X格式化快捷键设置
Chapter 6 datanode
Browser print margin, default / borderless, full 1 page A4
How to insert mathematical formulas in CSDN blog
useEffect,函數組件掛載和卸載時觸發
QT simulates mouse events and realizes clicking, double clicking, moving and dragging
QT realizes window topping, topping state switching, and multi window topping priority relationship
Spark独立集群动态上线下线Worker节点
第5章 消费者组详解
Chapter 5 namenode and secondarynamenode
Research Report on market supply and demand and strategy of double drum magnetic separator industry in China
原生js实现全选和反选的功能 --冯浩的博客
Market trend report, technical innovation and market forecast of tabletop dishwashers in China
Spark independent cluster dynamic online and offline worker node
第2章 HFDS的Shell操作
Anaconda下安装Jupyter notebook
(lightoj - 1370) Bi shoe and phi shoe (Euler function tabulation)
Codeforces Round #797 (Div. 3)无F
音视频开发面试题