-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1281-subtract-the-product-and-sum-of-digits-of-an-integer.js
57 lines (44 loc) · 1.64 KB
/
1281-subtract-the-product-and-sum-of-digits-of-an-integer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// 1281. Subtract the Product and Sum of Digits of an Integer
// https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/
/*
Given an integer number n, return the difference between the product of its
digits and the sum of its digits.
*/
import { strictEqual } from 'assert';
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 52 ms, faster than 82.72% of JavaScript online submissions
// Memory Usage: 34 MB, less than 100.00% of JavaScript online submissions
// /**
// * @param {number} n
// * @return {number}
// */
// const subtractProductAndSum = n =>
// [...`${n}`].reduce((acc, curr) => acc * Number(curr), 1) -
// [...`${n}`].reduce((acc, curr) => acc + Number(curr), 0);
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 48 ms, faster than 94.38% of JavaScript online submissions
// Memory Usage: 33.9 MB, less than 100.00% of JavaScript online submissions
/**
* @param {number} n
* @return {number}
*/
const subtractProductAndSum = n =>
[...`${n}`]
.reduce((acc, curr) => [acc[0] * Number(curr), acc[1] + Number(curr)], [
1,
0,
])
.reduce((acc, curr) => acc - curr);
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Example 1:
strictEqual(subtractProductAndSum(234), 15);
// Explanation:
// Product of digits = 2 * 3 * 4 = 24
// Sum of digits = 2 + 3 + 4 = 9
// Result = 24 - 9 = 15
// Example 2:
strictEqual(subtractProductAndSum(4421), 21);
// Explanation:
// Product of digits = 4 * 4 * 2 * 1 = 32
// Sum of digits = 4 + 4 + 2 + 1 = 11
// Result = 32 - 11 = 21