Skip to content

Commit 865c9f2

Browse files
add 1390
1 parent df58c54 commit 865c9f2

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1390|[Four Divisors](https://leetcode.com/problems/four-divisors/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1390.java) | |Medium|Math|
1112
|1389|[Create Target Array in the Given Order](https://leetcode.com/problems/create-target-array-in-the-given-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1389.java) | |Easy|Array|
1213
|1388|[Pizza With 3n Slices](https://leetcode.com/problems/pizza-with-3n-slices/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1388.java) | |Hard|DP|
1314
|1387|[Sort Integers by The Power Value](https://leetcode.com/problems/sort-integers-by-the-power-value/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1387.java) | |Medium|Sort, Graph|
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* 1390. Four Divisors
8+
*
9+
* Given an integer array nums, return the sum of divisors of the integers in that array that have exactly four divisors.
10+
* If there is no such integer in the array, return 0.
11+
*
12+
* Example 1:
13+
* Input: nums = [21,4,7]
14+
* Output: 32
15+
* Explanation:
16+
* 21 has 4 divisors: 1, 3, 7, 21
17+
* 4 has 3 divisors: 1, 2, 4
18+
* 7 has 2 divisors: 1, 7
19+
* The answer is the sum of divisors of 21 only.
20+
*
21+
* Constraints:
22+
* 1 <= nums.length <= 10^4
23+
* 1 <= nums[i] <= 10^5
24+
* */
25+
public class _1390 {
26+
public static class Solution1 {
27+
public int sumFourDivisors(int[] nums) {
28+
int sum = 0;
29+
for (int i = 0; i < nums.length; i++) {
30+
List<Integer> divisors = getDivisors(nums[i]);
31+
if (divisors.size() == 4) {
32+
for (int div : divisors) {
33+
sum += div;
34+
}
35+
}
36+
}
37+
return sum;
38+
}
39+
40+
private List<Integer> getDivisors(int num) {
41+
List<Integer> divisors = new ArrayList<>();
42+
for (int i = 1; i <= Math.sqrt(num); ++i) {
43+
if (num % i == 0) {
44+
divisors.add(i);
45+
if (num / i != i) {
46+
divisors.add(num / i);
47+
}
48+
}
49+
}
50+
return divisors;
51+
}
52+
}
53+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1390;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1390Test {
10+
private static _1390.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1390.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(32, solution1.sumFourDivisors(new int[]{21, 4, 7}));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy