|
1 |
| -# **Date**: June 13, 2024 |
| 1 | +# **Date**: June 14, 2024 |
2 | 2 |
|
3 |
| -# 2037. Minimum Number of Moves to Seat Everyone |
| 3 | +# 945. Minimum Increment to Make Array Unique |
4 | 4 |
|
5 |
| -**Difficulty**:  |
6 |
| -**Related Topics**:   |
| 5 | +**Difficulty**:  |
| 6 | +**Related Topics**:   |
7 | 7 |
|
8 | 8 | <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;"> |
10 | 10 | <img src="https://img.shields.io/badge/All%20Problem%20Solutions-green" alt="All Problem Solutions">
|
11 | 11 | </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/"> |
13 | 13 | <img src="https://img.shields.io/badge/Link%20To%20The%20Question-blue" alt="Link To The Question">
|
14 | 14 | </a>
|
15 | 15 | </p>
|
16 | 16 |
|
17 | 17 | ## Editorial
|
18 | 18 |
|
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. |
20 | 20 |
|
21 | 21 | ### Solution Explanation
|
22 | 22 |
|
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: |
24 | 24 |
|
25 | 25 | #### 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. |
28 | 28 |
|
29 | 29 | ### Code
|
30 | 30 |
|
31 | 31 | ```cpp
|
32 | 32 | class Solution {
|
33 | 33 | 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 | + } |
45 | 49 | }
|
46 |
| - |
47 |
| - return ans; |
| 50 | + |
| 51 | + return cnt; |
48 | 52 | }
|
49 | 53 | };
|
50 | 54 | ```
|
51 | 55 | ## Explanation of Code
|
52 | 56 |
|
53 | 57 | ### 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. |
58 | 59 |
|
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. |
61 | 63 |
|
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. |
63 | 65 |
|
64 | 66 | ## Like and Upvote
|
65 | 67 |
|
|
0 commit comments