Skip to content

Commit 9804865

Browse files
committed
Add solution #1390
1 parent 6e6ce54 commit 9804865

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,273 LeetCode solutions in JavaScript
1+
# 1,274 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1060,6 +1060,7 @@
10601060
1387|[Sort Integers by The Power Value](./solutions/1387-sort-integers-by-the-power-value.js)|Medium|
10611061
1388|[Pizza With 3n Slices](./solutions/1388-pizza-with-3n-slices.js)|Hard|
10621062
1389|[Create Target Array in the Given Order](./solutions/1389-create-target-array-in-the-given-order.js)|Easy|
1063+
1390|[Four Divisors](./solutions/1390-four-divisors.js)|Medium|
10631064
1400|[Construct K Palindrome Strings](./solutions/1400-construct-k-palindrome-strings.js)|Medium|
10641065
1402|[Reducing Dishes](./solutions/1402-reducing-dishes.js)|Hard|
10651066
1408|[String Matching in an Array](./solutions/1408-string-matching-in-an-array.js)|Easy|

solutions/1390-four-divisors.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 1390. Four Divisors
3+
* https://leetcode.com/problems/four-divisors/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer array nums, return the sum of divisors of the integers in that array
7+
* that have exactly four divisors. If there is no such integer in the array, return 0.
8+
*/
9+
10+
/**
11+
* @param {number[]} nums
12+
* @return {number}
13+
*/
14+
var sumFourDivisors = function(nums) {
15+
function countDivisors(num) {
16+
const divisors = new Set();
17+
for (let i = 1; i * i <= num; i++) {
18+
if (num % i === 0) {
19+
divisors.add(i);
20+
divisors.add(num / i);
21+
}
22+
}
23+
return divisors;
24+
}
25+
26+
let result = 0;
27+
for (const num of nums) {
28+
const divisors = countDivisors(num);
29+
if (divisors.size === 4) {
30+
const sum = [...divisors].reduce((acc, val) => acc + val, 0);
31+
result += sum;
32+
}
33+
}
34+
35+
return result;
36+
};

0 commit comments

Comments
 (0)