Skip to content

Commit 36af37e

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.8 MB (72.12%)
1 parent 20dcde5 commit 36af37e

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<p>You are given three integers <code>start</code>, <code>finish</code>, and <code>limit</code>. You are also given a <strong>0-indexed</strong> string <code>s</code> representing a <strong>positive</strong> integer.</p>
2+
3+
<p>A <strong>positive</strong> integer <code>x</code> is called <strong>powerful</strong> if it ends with <code>s</code> (in other words, <code>s</code> is a <strong>suffix</strong> of <code>x</code>) and each digit in <code>x</code> is at most <code>limit</code>.</p>
4+
5+
<p>Return <em>the <strong>total</strong> number of powerful integers in the range</em> <code>[start..finish]</code>.</p>
6+
7+
<p>A string <code>x</code> is a suffix of a string <code>y</code> if and only if <code>x</code> is a substring of <code>y</code> that starts from some index (<strong>including </strong><code>0</code>) in <code>y</code> and extends to the index <code>y.length - 1</code>. For example, <code>25</code> is a suffix of <code>5125</code> whereas <code>512</code> is not.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre>
13+
<strong>Input:</strong> start = 1, finish = 6000, limit = 4, s = &quot;124&quot;
14+
<strong>Output:</strong> 5
15+
<strong>Explanation:</strong> The powerful integers in the range [1..6000] are 124, 1124, 2124, 3124, and, 4124. All these integers have each digit &lt;= 4, and &quot;124&quot; as a suffix. Note that 5124 is not a powerful integer because the first digit is 5 which is greater than 4.
16+
It can be shown that there are only 5 powerful integers in this range.
17+
</pre>
18+
19+
<p><strong class="example">Example 2:</strong></p>
20+
21+
<pre>
22+
<strong>Input:</strong> start = 15, finish = 215, limit = 6, s = &quot;10&quot;
23+
<strong>Output:</strong> 2
24+
<strong>Explanation:</strong> The powerful integers in the range [15..215] are 110 and 210. All these integers have each digit &lt;= 6, and &quot;10&quot; as a suffix.
25+
It can be shown that there are only 2 powerful integers in this range.
26+
</pre>
27+
28+
<p><strong class="example">Example 3:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> start = 1000, finish = 2000, limit = 4, s = &quot;3000&quot;
32+
<strong>Output:</strong> 0
33+
<strong>Explanation:</strong> All integers in the range [1000..2000] are smaller than 3000, hence &quot;3000&quot; cannot be a suffix of any integer in this range.
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>1 &lt;= start &lt;= finish &lt;= 10<sup>15</sup></code></li>
41+
<li><code>1 &lt;= limit &lt;= 9</code></li>
42+
<li><code>1 &lt;= s.length &lt;= floor(log<sub>10</sub>(finish)) + 1</code></li>
43+
<li><code>s</code> only consists of numeric digits which are at most <code>limit</code>.</li>
44+
<li><code>s</code> does not have leading zeros.</li>
45+
</ul>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Approach 2: Combinatorial mathematics
2+
3+
# Time: O(log(finish))
4+
# Space: O(log(finish))
5+
6+
class Solution:
7+
def numberOfPowerfulInt(self, start: int, finish: int, limit: int, s: str) -> int:
8+
start_ = str(start - 1)
9+
finish_ = str(finish)
10+
11+
return self.calculate(finish_, s, limit) - self.calculate(start_, s, limit)
12+
13+
def calculate(self, x, s, limit):
14+
if len(x) < len(s):
15+
return 0
16+
17+
if len(x) == len(s):
18+
return 1 if x >= s else 0
19+
20+
suffix = x[len(x) - len(s) :]
21+
count = 0
22+
pre_len = len(x) - len(s)
23+
24+
for i in range(pre_len):
25+
if limit < int(x[i]):
26+
count += (limit + 1) ** (pre_len - i)
27+
return count
28+
count += int(x[i]) * (limit + 1) ** (pre_len - 1 - i)
29+
30+
if suffix >= s:
31+
count += 1
32+
33+
return count
34+
35+

0 commit comments

Comments
 (0)