From fa3241fc7d587666b727107b3d7db6ad7bf322bb Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Fri, 14 Jun 2024 13:23:47 +0530 Subject: [PATCH] Create 945. Minimum Increment to Make Array Unique --- 945. Minimum Increment to Make Array Unique | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 945. Minimum Increment to Make Array Unique diff --git a/945. Minimum Increment to Make Array Unique b/945. Minimum Increment to Make Array Unique new file mode 100644 index 0000000..62e2124 --- /dev/null +++ b/945. Minimum Increment to Make Array Unique @@ -0,0 +1,19 @@ +class Solution { +public: + int minIncrementForUnique(std::vector& nums) { + if (nums.empty()) return 0; + int moves = 0,maxVal = 0; + for(auto c:nums)maxVal=max(maxVal,c); + int maxLength = nums.size() + maxVal; + vector count(maxLength, 0); + for (int c : nums) count[c]++; + for (int i = 0; i < maxLength; ++i) { + if (count[i] > 1) { + int tmp = count[i] - 1; + count[i + 1] += tmp; + moves += tmp; + } + } + return moves; + } +};