Skip to content

Commit d2af2bb

Browse files
committed
[LeetCode Sync] Runtime - 223 ms (45.55%), Memory - 18.7 MB (97.78%)
1 parent a2ffdb4 commit d2af2bb

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<p>Given an integer array <code>nums</code>, return <em>the sum of divisors of the integers in that array that have exactly four divisors</em>. If there is no such integer in the array, return <code>0</code>.</p>
2+
3+
<p>&nbsp;</p>
4+
<p><strong class="example">Example 1:</strong></p>
5+
6+
<pre>
7+
<strong>Input:</strong> nums = [21,4,7]
8+
<strong>Output:</strong> 32
9+
<strong>Explanation:</strong>
10+
21 has 4 divisors: 1, 3, 7, 21
11+
4 has 3 divisors: 1, 2, 4
12+
7 has 2 divisors: 1, 7
13+
The answer is the sum of divisors of 21 only.
14+
</pre>
15+
16+
<p><strong class="example">Example 2:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> nums = [21,21]
20+
<strong>Output:</strong> 64
21+
</pre>
22+
23+
<p><strong class="example">Example 3:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> nums = [1,2,3,4,5]
27+
<strong>Output:</strong> 0
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
35+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
36+
</ul>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def sumFourDivisors(self, nums: List[int]) -> int:
3+
def checker(val: int) -> int:
4+
i = 2
5+
count, sum_val = 2, val + 1
6+
while i <= val // i:
7+
if val % i == 0:
8+
count += 1
9+
sum_val += i
10+
if i ** 2 != val:
11+
count += 1
12+
sum_val += val // i
13+
i += 1
14+
if count > 4:
15+
return 0
16+
return sum_val if count == 4 else 0
17+
18+
return sum(checker(val) for val in nums)

0 commit comments

Comments
 (0)