diff --git a/posts/algorithms/leetcode-daily.html b/posts/algorithms/leetcode-daily.html index ff47c2b..04e9789 100644 --- a/posts/algorithms/leetcode-daily.html +++ b/posts/algorithms/leetcode-daily.html @@ -35,6 +35,41 @@

Leetcode Daily

+

+ minimum number of operations to make array distinct + — 9/13/24 +

+
+

understanding the problem

+

+ You can remove elements in groups of 3 solely from the + beginning of the array. Perform this operation until there are no + more duplicates left, returning the number of times you had to + perform the operation. +

+

solution: rephrase the question

+

+ Definitionally, you remove the last duplicate. If such + duplicate is at 0-indexed i, it belongs to the \(\lceil + \frac{i + 1}{3}\rceil\)th chunk of 3 (i.e. operation). Find the last + duplicate by leveraging a frequency map and iterating backwards + through the input. +

+
+

asymptotic complexity

+

+ The solution is optimal, considering the least amount of elements + possible in: +

+ +

& nums) { + vector freq(101, 0); + int i; + for (i = nums.size() - 1; i >= 0; --i) { + if (++freq[nums[i]] == 2) { + return ceil((i + 1) / 3.0); + } + } + return 0; + } +};