Abbreviating the Product of a Range Problem
Description
LeetCode Problem 2117.
You are given two positive integers left and right with left <= right. Calculate the product of all integers in the inclusive range [left, right].
Since the product may be very large, you will abbreviate it following these steps:
- Count all trailing zeros in the product and remove them. Let us denote this count as
C.- For example, there are
3trailing zeros in1000, and there are0trailing zeros in546.
- For example, there are
- Denote the remaining number of digits in the product as
d. Ifd > 10, then express the product as<pre>...<suf>where<pre>denotes the first5digits of the product, and<suf>denotes the last5digits of the product after removing all trailing zeros. Ifd <= 10, we keep it unchanged.- For example, we express
1234567654321as12345...54321, but1234567is represented as1234567.
- For example, we express
- Finally, represent the product as a string
"<pre>...<suf>eC".- For example,
12345678987600000will be represented as"12345...89876e5".
- For example,
Return a string denoting the abbreviated product of all integers in the inclusive range [left, right].
Example 1:
Input: left = 1, right = 4
Output: "24e0"
Explanation: The product is 1 × 2 × 3 × 4 = 24.
There are no trailing zeros, so 24 remains the same. The abbreviation will end with "e0".
Since the number of digits is 2, which is less than 10, we do not have to abbreviate it further.
Thus, the final representation is "24e0".
Example 2:
Input: left = 2, right = 11
Output: "399168e2"
Explanation: The product is 39916800.
There are 2 trailing zeros, which we remove to get 399168. The abbreviation will end with "e2".
The number of digits after removing the trailing zeros is 6, so we do not abbreviate it further.
Hence, the abbreviated product is "399168e2".
Example 3:
Input: left = 371, right = 375
Output: "7219856259e3"
Explanation: The product is 7219856259000.
Constraints:
1 <= left <= right <= 10^4
Solution
Approach: Counting Factors and Tracking Both Ends
Trailing zeros come from pairs of factors 2 and 5, so count both over the range; the number of zeros is c = min(cnt2, cnt5). Then multiply the numbers one by one while keeping two values. The suffix is the product with c factors of 2 and c factors of 5 removed as soon as they appear, kept as an integer modulo 10^10; once it reaches 10^10 we know the product has more than 10 digits. The prefix is a floating-point product that is divided by 10 whenever it exceeds 10^5, so it keeps the leading digits. If the product never grew past 10 digits, print the suffix as is; otherwise print the first five digits, ..., the last five digits of the suffix (padded with zeros) and e followed by c.
Complexity: time O(right - left); space O(1).
class Solution:
def abbreviateProduct(self, left: int, right: int) -> str:
cnt2 = cnt5 = 0
for x in range(left, right + 1):
while x % 2 == 0:
cnt2 += 1
x //= 2
while x % 5 == 0:
cnt5 += 1
x //= 5
c = cnt2 = cnt5 = min(cnt2, cnt5)
pre = suf = 1
gt = False
for x in range(left, right + 1):
suf *= x
while cnt2 and suf % 2 == 0:
suf //= 2
cnt2 -= 1
while cnt5 and suf % 5 == 0:
suf //= 5
cnt5 -= 1
if suf >= 1e10:
gt = True
suf %= int(1e10)
pre *= x
while pre > 1e5:
pre /= 10
if gt:
return str(int(pre)) + "..." + str(suf % int(1e5)).zfill(5) + "e" + str(c)
return str(suf) + "e" + str(c)