当前位置:网站首页>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).
边栏推荐
- Educational Codeforces Round 122 (Rated for Div. 2)
- Use JQ to realize the reverse selection of all and no selection at all - Feng Hao's blog
- Soft music -js find the number of times that character appears in the string - Feng Hao's blog
- Sublime text code formatting operation
- Codeforces Round #800 (Div. 2)AC
- Codeforces Round #798 (Div. 2)A~D
- Spark独立集群动态上线下线Worker节点
- The concept of spark independent cluster worker and executor
- (lightoj - 1354) IP checking (Analog)
- Simply try the new amp model of deepfacelab (deepfake)
猜你喜欢
Click QT button to switch qlineedit focus (including code)
sublime text 代码格式化操作
js封装数组反转的方法--冯浩的博客
ByteDance new programmer's growth secret: those glittering treasures mentors
浏览器打印边距,默认/无边距,占满1页A4
业务系统从Oracle迁移到openGauss数据库的简单记录
<li>圆点样式 list-style-type
Audio and video development interview questions
Chapter III principles of MapReduce framework
Raspberry pie 4B installation opencv3.4.0
随机推荐
Audio and video development interview questions
第2章 HFDS的Shell操作
第7章 __consumer_offsets topic
顺丰科技智慧物流校园技术挑战赛(无t4)
字节跳动新程序员成长秘诀:那些闪闪发光的宝藏mentor们
Investigation report of bench type Brinell hardness tester industry - market status analysis and development prospect prediction
MariaDB的安装与配置
Codeforces Round #802(Div. 2)A~D
新手必会的静态站点生成器——Gridsome
Problem - 1646C. Factorials and Powers of Two - Codeforces
antd upload beforeUpload中禁止触发onchange
FLV格式详解
js时间函数大全 详细的讲解 -----阿浩博客
第6章 Rebalance详解
Market trend report, technological innovation and market forecast of desktop electric tools in China
Oneforall installation and use
Simple records of business system migration from Oracle to opengauss database
Kubernetes cluster deployment
How to insert mathematical formulas in CSDN blog
Sublime text code formatting operation