feat(leetcode-daily): april 13

This commit is contained in:
Barrett Ruth 2025-04-13 09:51:28 -04:00
parent dcaea06cc0
commit ab42a8b1ee
2 changed files with 62 additions and 4 deletions

View file

@ -35,6 +35,45 @@
<h1 class="post-title">Leetcode Daily</h1>
</header>
<article class="post-article">
<h2>
<a
target="blank"
href="https://leetcode.com/problems/count-good-numbers/submissions/1605647445/?envType=daily-question&envId=2025-04-13"
>count good numbers</a
>
&mdash; 13/13/24
</h2>
<div class="problem-content">
<h3>understanding the problem</h3>
<p>
p is a combinatoric problem at heart. You have some slots for
evens and some for primes, with a limited number of choices for
each. Leverage the multiplication rule, which states that if you
have \(n\) slots with \(x\) choices, you get \(x^n\) possible
outcomes.
</p>
<h3>doing it</h3>
<p>
So, what's the answer? If we know which slots we have and the
number of choices for them, we're done. Since this is leetcode,
they don't let you think&mdash;they just give you the answer. You
have 2 types of slots (even and odd indices) with 5
(\(\{0,2,4,6,8\}\)) and 4 (\(\{2,3,5,7\}\)) choices respectively.
Therefore, the answer is: \(5^{\text{# even slots}}\cdot4^{\text{#
odd slots}}\) By counting or with small cases, we have
\(\lceil\frac{n}{2}\rceil\) even slots and
\(\lfloor\frac{n}{2}\rfloor\) odd slots. Let's submit it!
</p>
<p>
And.... TLE. Checking <i>everything</i> when you submit your
code&mdash;in this case, constraint \(n\leq 10^{16}\) informs us
of something suspect. In the worst case, \(\frac{n}{2}\approx
n^14\). This is far too many multiplications, so we can leverage
binary exponentiation instead (and probably should've been the
whole time!). Don't forget the mod.
</p>
<div class="code" data-file="cgn.cpp"></div>
</div>
<h2>
<a
target="blank"
@ -54,10 +93,10 @@
<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.
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>

View file

@ -0,0 +1,19 @@
class Solution {
public:
static constexpr long long MOD = 1e9 + 7;
long long mpow(long long a, long long b, long long mod=MOD) {
long long ans = 1;
while (b > 0) {
if (b & 1) {
ans = (ans * a) % MOD;
}
a = (a * a) % MOD;
b >>= 1;
}
return ans;
}
int countGoodNumbers(long long n) {
long long even_slots = (n + 1) / 2, odd_slots = n / 2;
return (mpow(5, even_slots) * mpow(4, odd_slots)) % MOD;
}
};