Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 25d7607

Browse files
committed
Add solution #2117
1 parent 458edeb commit 25d7607

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-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,755 LeetCode solutions in JavaScript
1+
# 1,756 LeetCode solutions in JavaScript
22

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

@@ -1621,6 +1621,7 @@
16211621
2114|[Maximum Number of Words Found in Sentences](./solutions/2114-maximum-number-of-words-found-in-sentences.js)|Easy|
16221622
2115|[Find All Possible Recipes from Given Supplies](./solutions/2115-find-all-possible-recipes-from-given-supplies.js)|Medium|
16231623
2116|[Check if a Parentheses String Can Be Valid](./solutions/2116-check-if-a-parentheses-string-can-be-valid.js)|Medium|
1624+
2117|[Abbreviating the Product of a Range](./solutions/2117-abbreviating-the-product-of-a-range.js)|Hard|
16241625
2127|[Maximum Employees to Be Invited to a Meeting](./solutions/2127-maximum-employees-to-be-invited-to-a-meeting.js)|Hard|
16251626
2129|[Capitalize the Title](./solutions/2129-capitalize-the-title.js)|Easy|
16261627
2130|[Maximum Twin Sum of a Linked List](./solutions/2130-maximum-twin-sum-of-a-linked-list.js)|Medium|
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* 2117. Abbreviating the Product of a Range
3+
* https://leetcode.com/problems/abbreviating-the-product-of-a-range/
4+
* Difficulty: Hard
5+
*
6+
* You are given two positive integers left and right with left <= right. Calculate the product of
7+
* all integers in the inclusive range [left, right].
8+
*
9+
* Since the product may be very large, you will abbreviate it following these steps:
10+
* 1. Count all trailing zeros in the product and remove them. Let us denote this count as C.
11+
* - For example, there are 3 trailing zeros in 1000, and there are 0 trailing zeros in 546.
12+
* 2. Denote the remaining number of digits in the product as d. If d > 10, then express the product
13+
* as <pre>...<suf> where <pre> denotes the first 5 digits of the product, and <suf> denotes the
14+
* last 5 digits of the product after removing all trailing zeros. If d <= 10, we keep it
15+
* unchanged.
16+
* - For example, we express 1234567654321 as 12345...54321, but 1234567 is represented as
17+
* 1234567.
18+
* 3. Finally, represent the product as a string "<pre>...<suf>eC".
19+
* - For example, 12345678987600000 will be represented as "12345...89876e5".
20+
*
21+
* Return a string denoting the abbreviated product of all integers in the inclusive range [left,
22+
* right].
23+
*/
24+
25+
/**
26+
* @param {number} left
27+
* @param {number} right
28+
* @return {string}
29+
*/
30+
var abbreviateProduct = function(left, right) {
31+
let zeros = 0;
32+
let count2 = 0;
33+
let count5 = 0;
34+
35+
for (let i = left; i <= right; i++) {
36+
let n = i;
37+
while (n % 2 === 0) {
38+
count2++;
39+
n = Math.floor(n / 2);
40+
}
41+
n = i;
42+
while (n % 5 === 0) {
43+
count5++;
44+
n = Math.floor(n / 5);
45+
}
46+
}
47+
zeros = Math.min(count2, count5);
48+
49+
let digits = 0;
50+
for (let i = left; i <= right; i++) {
51+
digits += Math.log10(i);
52+
}
53+
digits = Math.floor(digits) + 1;
54+
55+
if (digits - zeros <= 10) {
56+
let product = 1n;
57+
for (let i = left; i <= right; i++) {
58+
product *= BigInt(i);
59+
}
60+
for (let i = 0; i < zeros; i++) {
61+
product /= 10n;
62+
}
63+
return product.toString() + 'e' + zeros;
64+
}
65+
66+
let prefix = 1;
67+
for (let i = left; i <= right; i++) {
68+
prefix *= i;
69+
while (prefix >= 1e10) {
70+
prefix /= 10;
71+
}
72+
}
73+
prefix = prefix.toString().slice(0, 5);
74+
75+
let suffix = 1n;
76+
for (let i = right; i >= left; i--) {
77+
suffix = (suffix * BigInt(i));
78+
while (suffix % 10n === 0n) {
79+
suffix /= 10n;
80+
}
81+
suffix = suffix % (10n ** 15n);
82+
}
83+
84+
suffix = suffix.toString();
85+
while (suffix.length < 5) {
86+
suffix = '0' + suffix;
87+
}
88+
suffix = suffix.slice(-5);
89+
90+
return prefix + '...' + suffix + 'e' + zeros;
91+
};

0 commit comments

Comments
 (0)