Skip to content

Commit 6030037

Browse files
authored
Create reduction-operations-to-make-the-array-elements-equal.cpp
1 parent 7b49cb4 commit 6030037

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Time: O(nlogn)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int reductionOperations(vector<int>& nums) {
7+
sort(begin(nums), end(nums));
8+
int result = 0, curr = 0;
9+
for (int i = 1; i < size(nums); ++i) {
10+
if (nums[i - 1] < nums[i]) {
11+
++curr;
12+
}
13+
result += curr;
14+
}
15+
return result;
16+
}
17+
};

0 commit comments

Comments
 (0)