feat(leetcode-daily): today
This commit is contained in:
parent
98d99a017d
commit
0ea685e154
2 changed files with 48 additions and 0 deletions
|
|
@ -35,6 +35,41 @@
|
||||||
<h1 class="post-title">Leetcode Daily</h1>
|
<h1 class="post-title">Leetcode Daily</h1>
|
||||||
</header>
|
</header>
|
||||||
<article class="post-article">
|
<article class="post-article">
|
||||||
|
<h2>
|
||||||
|
<a
|
||||||
|
target="blank"
|
||||||
|
href="https://leetcode.com/problems/minimum-number-of-operations-to-make-elements-in-array-distinc"
|
||||||
|
>minimum number of operations to make array distinct</a
|
||||||
|
>
|
||||||
|
— 9/13/24
|
||||||
|
</h2>
|
||||||
|
<div class="problem-content">
|
||||||
|
<h3>understanding the problem</h3>
|
||||||
|
<p>
|
||||||
|
You can remove elements in groups of 3 <i>solely</i> 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.
|
||||||
|
</p>
|
||||||
|
<h3>solution: rephrase the question</h3>
|
||||||
|
<p>
|
||||||
|
Definitionally, you remove the <i>last</i> duplicate. If such
|
||||||
|
duplicate is at 0-indexed <code>i</code>, 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.
|
||||||
|
</p>
|
||||||
|
<div class="code" data-file="mnootmad.cpp"></div>
|
||||||
|
<h3>asymptotic complexity</h3>
|
||||||
|
<p>
|
||||||
|
The solution is optimal, considering the least amount of elements
|
||||||
|
possible in:
|
||||||
|
</p>
|
||||||
|
<ul>
|
||||||
|
<li><u>Time Complexity</u>: \(\O(n)\)</li>
|
||||||
|
<li><u>Space Complexity</u>: \(\Theta(1)\)</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
<h2>
|
<h2>
|
||||||
<a
|
<a
|
||||||
target="blank"
|
target="blank"
|
||||||
|
|
|
||||||
13
public/code/algorithms/leetcode-daily/mnootmad.cpp
Normal file
13
public/code/algorithms/leetcode-daily/mnootmad.cpp
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int minimumOperations(vector<int>& nums) {
|
||||||
|
vector<int> 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;
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue