当前位置:网站首页>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).
边栏推荐
- Use JQ to realize the reverse selection of all and no selection at all - Feng Hao's blog
- Investigation report of bench type Brinell hardness tester industry - market status analysis and development prospect prediction
- Click QT button to switch qlineedit focus (including code)
- Acwing: the 56th weekly match
- Raspberry pie 4B installation opencv3.4.0
- 音视频开发面试题
- Basic principles of video compression coding and audio compression coding
- Educational Codeforces Round 130 (Rated for Div. 2)A~C
- 第5章 NameNode和SecondaryNameNode
- Codeforces Round #801 (Div. 2)A~C
猜你喜欢
随机推荐
Audio and video development interview questions
Submit several problem records of spark application (sparklauncher with cluster deploy mode)
input 只能输入数字,限定输入
MariaDB的安装与配置
QT implementation fillet window
300th weekly match - leetcode
QT style settings of qcobobox controls (rounded corners, drop-down boxes, up expansion, editable, internal layout, etc.)
Summary of FTP function implemented by qnetworkaccessmanager
Hbuilder X格式化快捷键设置
Educational Codeforces Round 122 (Rated for Div. 2)
Soft music -js find the number of times that character appears in the string - Feng Hao's blog
第6章 Rebalance详解
Chapter 5 detailed explanation of consumer groups
Research Report on hearing health care equipment industry - market status analysis and development prospect prediction
第五章 Yarn资源调度器
Chapter 1 overview of MapReduce
Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
Acwing - game 55 of the week
Classic application of stack -- bracket matching problem
【锟斤拷】的故事:谈谈汉字编码和常用字符集









