Skip to content

Commit 1cf848b

Browse files
committed
14-06-2024
1 parent 5a2b61e commit 1cf848b

File tree

2 files changed

+55
-30
lines changed

2 files changed

+55
-30
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int minIncrementForUnique(vector<int>& nums) {
4+
5+
int n = nums.size();
6+
7+
sort(nums.begin() , nums.end());
8+
9+
int cnt = 0;
10+
11+
for(int i = 1 ; i < n ; i++)
12+
{
13+
if(nums[i] <= nums[i-1])
14+
{
15+
int temp = nums[i];
16+
nums[i] = nums[i-1]+1;
17+
cnt += nums[i] - temp;
18+
}
19+
}
20+
21+
return cnt;
22+
}
23+
};

readme.md

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,67 @@
1-
# **Date**: June 13, 2024
1+
# **Date**: June 14, 2024
22

3-
# 2037. Minimum Number of Moves to Seat Everyone
3+
# 945. Minimum Increment to Make Array Unique
44

5-
**Difficulty**: ![Easy](https://img.shields.io/badge/Easy-brightgreen)
6-
**Related Topics**: ![Greedy](https://img.shields.io/badge/Greedy-blue) ![Sorting](https://img.shields.io/badge/Sorting-blue)
5+
**Difficulty**: ![Medium](https://img.shields.io/badge/Medium-yellow)
6+
**Related Topics**: ![Greedy](https://img.shields.io/badge/Greedy-blue) ![Array](https://img.shields.io/badge/Array-blue)
77

88
<p>
9-
<a href="https://github.com/Hasheditz/Leetcode-CSES-GFG-Codeforces-Coding-Solutions?tab=readme-ov-file#minimum-number-of-moves-to-seat-everyone" style="margin-right: 5px;">
9+
<a href="https://github.com/Hasheditz/Leetcode-CSES-GFG-Codeforces-Coding-Solutions?tab=readme-ov-file#minimum-increment-to-make-array-unique" style="margin-right: 5px;">
1010
<img src="https://img.shields.io/badge/All%20Problem%20Solutions-green" alt="All Problem Solutions">
1111
</a>
12-
<a href="https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/">
12+
<a href="https://leetcode.com/problems/minimum-increment-to-make-array-unique/">
1313
<img src="https://img.shields.io/badge/Link%20To%20The%20Question-blue" alt="Link To The Question">
1414
</a>
1515
</p>
1616

1717
## Editorial
1818

19-
This problem requires us to determine the minimum number of moves required to seat each student in an available seat such that the sum of the absolute differences between the positions of the students and the seats they occupy is minimized.
19+
To solve the problem of making all elements in an array unique with the minimum increment, it helps to have experience with a similar problem: [Minimum Operations to Make the Array Increasing](https://leetcode.com/problems/minimum-operations-to-make-the-array-increasing/). Understanding how to increment elements to maintain a specific property of the array will aid in tackling this problem.
2020

2121
### Solution Explanation
2222

23-
To solve this problem efficiently, we can break down the solution into the following steps:
23+
The goal is to make every element in the array unique by incrementing some of the elements with the minimum number of operations. Here's how to achieve this:
2424

2525
#### Key Steps:
26-
1. **Sort the Arrays**: Sort both the `seat` and `student` arrays.
27-
2. **Calculate Minimum Moves**: Iterate through the sorted arrays and sum the absolute differences between the corresponding elements of the `seat` and `student` arrays.
26+
1. **Sort the Array**: Start by sorting the array. This allows us to easily detect duplicates.
27+
2. **Increment to Make Unique**: Iterate through the sorted array, and for each duplicate, increment it until it becomes unique. Keep track of the total increments made.
2828

2929
### Code
3030

3131
```cpp
3232
class Solution {
3333
public:
34-
int minMovesToSeat(vector<int>& seat, vector<int>& student) {
35-
// Step 1: Sort the seat and student arrays
36-
sort(seat.begin(), seat.end());
37-
sort(student.begin(), student.end());
38-
39-
// Step 2: Initialize the answer variable
40-
int ans = 0;
41-
42-
// Step 3: Calculate the total minimum moves
43-
for(int i = 0; i < seat.size(); i++) {
44-
ans += abs(seat[i] - student[i]);
34+
int minIncrementForUnique(vector<int>& nums) {
35+
int n = nums.size();
36+
37+
// Step 1: Sort the array
38+
sort(nums.begin(), nums.end());
39+
40+
int cnt = 0;
41+
42+
// Step 2: Increment duplicates to make them unique
43+
for (int i = 1; i < n; i++) {
44+
if (nums[i] <= nums[i - 1]) {
45+
int temp = nums[i];
46+
nums[i] = nums[i - 1] + 1;
47+
cnt += nums[i] - temp;
48+
}
4549
}
46-
47-
return ans;
50+
51+
return cnt;
4852
}
4953
};
5054
```
5155
## Explanation of Code
5256
5357
### Initialization:
54-
We initialize the answer variable `ans` to 0 to keep track of the total minimum moves.
55-
56-
### Sort the Arrays:
57-
We sort both the `seat` and `student` arrays to ensure that the closest seats and students are paired.
58+
We sort the array `nums` to facilitate detecting and handling duplicates easily.
5859
59-
### Calculate Minimum Moves:
60-
We iterate through the sorted arrays and calculate the absolute difference between each pair of seat and student. These differences are accumulated in the `ans` variable to get the total minimum moves required.
60+
### Increment to Make Unique:
61+
We iterate through the sorted array. For each element that is not greater than the previous element, we increment it to be one more than the previous element. This ensures all elements are unique.
62+
We keep a count of all the increments made to achieve this.
6163
62-
This approach ensures an efficient and optimal pairing of seats and students to minimize the total moves.
64+
This approach ensures that all elements in the array are unique with the minimum number of increments.
6365
6466
## Like and Upvote
6567

0 commit comments

Comments
 (0)