From 68168cc67feb4a426a69b69e86f373b93966e942 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 21 Oct 2023 17:43:37 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.2117 --- .../0175.Combine Two Tables/README.md | 5 +- .../0175.Combine Two Tables/README_EN.md | 4 +- .../0176.Second Highest Salary/README.md | 3 +- .../0176.Second Highest Salary/README_EN.md | 3 +- .../0177.Nth Highest Salary/README.md | 3 +- .../0177.Nth Highest Salary/README_EN.md | 3 +- solution/0100-0199/0178.Rank Scores/README.md | 3 +- .../0100-0199/0178.Rank Scores/README_EN.md | 3 +- .../0180.Consecutive Numbers/README.md | 3 +- .../0180.Consecutive Numbers/README_EN.md | 3 +- .../README.md | 3 +- .../README_EN.md | 2 + .../0100-0199/0182.Duplicate Emails/README.md | 3 +- .../0182.Duplicate Emails/README_EN.md | 3 +- .../0183.Customers Who Never Order/README.md | 3 +- .../README_EN.md | 3 +- .../README.md | 195 +++++++++++++++++- .../README_EN.md | 193 +++++++++++++++++ .../Solution.cpp | 40 ++++ .../Solution.go | 41 ++++ .../Solution.java | 38 ++++ .../Solution.py | 30 +++ .../README.md | 14 +- .../README_EN.md | 18 +- .../Solution.go | 4 +- .../Solution.ts | 6 +- 26 files changed, 591 insertions(+), 38 deletions(-) create mode 100644 solution/2100-2199/2117.Abbreviating the Product of a Range/Solution.cpp create mode 100644 solution/2100-2199/2117.Abbreviating the Product of a Range/Solution.go create mode 100644 solution/2100-2199/2117.Abbreviating the Product of a Range/Solution.java create mode 100644 solution/2100-2199/2117.Abbreviating the Product of a Range/Solution.py diff --git a/solution/0100-0199/0175.Combine Two Tables/README.md b/solution/0100-0199/0175.Combine Two Tables/README.md index 6330f7c78f8f0..e6c97f4f66d3a 100644 --- a/solution/0100-0199/0175.Combine Two Tables/README.md +++ b/solution/0100-0199/0175.Combine Two Tables/README.md @@ -96,7 +96,9 @@ FROM LEFT JOIN Address USING (personId); ``` -```pandas +### **Pandas** + +```python import pandas as pd @@ -104,7 +106,6 @@ def combine_two_tables(person: pd.DataFrame, address: pd.DataFrame) -> pd.DataFr return pd.merge(left=person, right=address, how="left", on="personId")[ ["firstName", "lastName", "city", "state"] ] - ``` diff --git a/solution/0100-0199/0175.Combine Two Tables/README_EN.md b/solution/0100-0199/0175.Combine Two Tables/README_EN.md index 8356f8440b9e5..690d28b9cbf2d 100644 --- a/solution/0100-0199/0175.Combine Two Tables/README_EN.md +++ b/solution/0100-0199/0175.Combine Two Tables/README_EN.md @@ -92,7 +92,9 @@ FROM LEFT JOIN Address USING (personId); ``` -```pandas +### **Pandas** + +```python import pandas as pd diff --git a/solution/0100-0199/0176.Second Highest Salary/README.md b/solution/0100-0199/0176.Second Highest Salary/README.md index 575055bc6a533..ca8b5820d9978 100644 --- a/solution/0100-0199/0176.Second Highest Salary/README.md +++ b/solution/0100-0199/0176.Second Highest Salary/README.md @@ -113,6 +113,8 @@ WITH T AS (SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rk FROM Em SELECT (SELECT DISTINCT salary FROM T WHERE rk = 2) AS SecondHighestSalary; ``` +### **Pandas** + ```python import pandas as pd @@ -134,7 +136,6 @@ def second_highest_salary(employee: pd.DataFrame) -> pd.DataFrame: result_df = pd.DataFrame({"SecondHighestSalary": [second_highest]}) return result_df - ``` diff --git a/solution/0100-0199/0176.Second Highest Salary/README_EN.md b/solution/0100-0199/0176.Second Highest Salary/README_EN.md index 8140c9eafcd6c..10f92131487bb 100644 --- a/solution/0100-0199/0176.Second Highest Salary/README_EN.md +++ b/solution/0100-0199/0176.Second Highest Salary/README_EN.md @@ -98,6 +98,8 @@ WITH T AS (SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rk FROM Em SELECT (SELECT DISTINCT salary FROM T WHERE rk = 2) AS SecondHighestSalary; ``` +### **Pandas** + ```python import pandas as pd @@ -119,7 +121,6 @@ def second_highest_salary(employee: pd.DataFrame) -> pd.DataFrame: result_df = pd.DataFrame({"SecondHighestSalary": [second_highest]}) return result_df - ``` diff --git a/solution/0100-0199/0177.Nth Highest Salary/README.md b/solution/0100-0199/0177.Nth Highest Salary/README.md index 10d32136eacdf..759fa5735f347 100644 --- a/solution/0100-0199/0177.Nth Highest Salary/README.md +++ b/solution/0100-0199/0177.Nth Highest Salary/README.md @@ -94,6 +94,8 @@ BEGIN END ``` +### **Pandas** + ```python import pandas as pd @@ -105,7 +107,6 @@ def nth_highest_salary(employee: pd.DataFrame, N: int) -> pd.DataFrame: else: salary = sorted(unique_salaries, reverse=True)[N - 1] return pd.DataFrame([salary], columns=[f"getNthHighestSalary({N})"]) - ``` diff --git a/solution/0100-0199/0177.Nth Highest Salary/README_EN.md b/solution/0100-0199/0177.Nth Highest Salary/README_EN.md index aa7620135200d..79d7e5df11925 100644 --- a/solution/0100-0199/0177.Nth Highest Salary/README_EN.md +++ b/solution/0100-0199/0177.Nth Highest Salary/README_EN.md @@ -86,6 +86,8 @@ BEGIN END ``` +### **Pandas** + ```python import pandas as pd @@ -97,7 +99,6 @@ def nth_highest_salary(employee: pd.DataFrame, N: int) -> pd.DataFrame: else: salary = sorted(unique_salaries, reverse=True)[N - 1] return pd.DataFrame([salary], columns=[f"getNthHighestSalary({N})"]) - ``` diff --git a/solution/0100-0199/0178.Rank Scores/README.md b/solution/0100-0199/0178.Rank Scores/README.md index e66598d93bb16..a6dbb3bff20b9 100644 --- a/solution/0100-0199/0178.Rank Scores/README.md +++ b/solution/0100-0199/0178.Rank Scores/README.md @@ -122,6 +122,8 @@ FROM ) s; ``` +### **Pandas** + ```python import pandas as pd @@ -134,7 +136,6 @@ def order_scores(scores: pd.DataFrame) -> pd.DataFrame: result_df = scores.drop("id", axis=1).sort_values(by="score", ascending=False) return result_df - ``` diff --git a/solution/0100-0199/0178.Rank Scores/README_EN.md b/solution/0100-0199/0178.Rank Scores/README_EN.md index 91e79b5f5cc5e..506bac7fa42ec 100644 --- a/solution/0100-0199/0178.Rank Scores/README_EN.md +++ b/solution/0100-0199/0178.Rank Scores/README_EN.md @@ -113,6 +113,8 @@ FROM ) s; ``` +### **Pandas** + ```python import pandas as pd @@ -125,7 +127,6 @@ def order_scores(scores: pd.DataFrame) -> pd.DataFrame: result_df = scores.drop("id", axis=1).sort_values(by="score", ascending=False) return result_df - ``` diff --git a/solution/0100-0199/0180.Consecutive Numbers/README.md b/solution/0100-0199/0180.Consecutive Numbers/README.md index f1ea2771a7b55..911fbce488071 100644 --- a/solution/0100-0199/0180.Consecutive Numbers/README.md +++ b/solution/0100-0199/0180.Consecutive Numbers/README.md @@ -116,6 +116,8 @@ GROUP BY p HAVING COUNT(1) >= 3; ``` +### **Pandas** + ```python import pandas as pd @@ -130,7 +132,6 @@ def consecutive_numbers(logs: pd.DataFrame) -> pd.DataFrame: .drop_duplicates() .rename(columns={"num": "ConsecutiveNums"}) ) - ``` diff --git a/solution/0100-0199/0180.Consecutive Numbers/README_EN.md b/solution/0100-0199/0180.Consecutive Numbers/README_EN.md index 86e3c7daf945c..402e19ca9cb56 100644 --- a/solution/0100-0199/0180.Consecutive Numbers/README_EN.md +++ b/solution/0100-0199/0180.Consecutive Numbers/README_EN.md @@ -112,6 +112,8 @@ GROUP BY p HAVING COUNT(1) >= 3; ``` +### **Pandas** + ```python import pandas as pd @@ -126,7 +128,6 @@ def consecutive_numbers(logs: pd.DataFrame) -> pd.DataFrame: .drop_duplicates() .rename(columns={"num": "ConsecutiveNums"}) ) - ``` diff --git a/solution/0100-0199/0181.Employees Earning More Than Their Managers/README.md b/solution/0100-0199/0181.Employees Earning More Than Their Managers/README.md index 282a9d4b8413f..98d39c81a10f4 100644 --- a/solution/0100-0199/0181.Employees Earning More Than Their Managers/README.md +++ b/solution/0100-0199/0181.Employees Earning More Than Their Managers/README.md @@ -81,6 +81,8 @@ FROM WHERE e1.salary > e2.salary; ``` +### **Pandas** + ```python import pandas as pd @@ -90,7 +92,6 @@ def find_employees(employee: pd.DataFrame) -> pd.DataFrame: emp = df[df["salary_x"] > df["salary_y"]]["name_x"] return pd.DataFrame({"Employee": emp}) - ``` diff --git a/solution/0100-0199/0181.Employees Earning More Than Their Managers/README_EN.md b/solution/0100-0199/0181.Employees Earning More Than Their Managers/README_EN.md index 8cd49e900a806..82ce7348eeaf1 100644 --- a/solution/0100-0199/0181.Employees Earning More Than Their Managers/README_EN.md +++ b/solution/0100-0199/0181.Employees Earning More Than Their Managers/README_EN.md @@ -77,6 +77,8 @@ FROM WHERE e1.salary > e2.salary; ``` +### **Pandas** + ```python import pandas as pd diff --git a/solution/0100-0199/0182.Duplicate Emails/README.md b/solution/0100-0199/0182.Duplicate Emails/README.md index 37bdf151e0074..de856d137ffee 100644 --- a/solution/0100-0199/0182.Duplicate Emails/README.md +++ b/solution/0100-0199/0182.Duplicate Emails/README.md @@ -83,6 +83,8 @@ FROM WHERE p1.id != p2.id AND p1.email = p2.email; ``` +### **Pandas** + ```python import pandas as pd @@ -93,7 +95,6 @@ def duplicate_emails(person: pd.DataFrame) -> pd.DataFrame: results = person.loc[person.duplicated(subset=["email"]), ["email"]] return results.drop_duplicates() - ``` diff --git a/solution/0100-0199/0182.Duplicate Emails/README_EN.md b/solution/0100-0199/0182.Duplicate Emails/README_EN.md index d99bc88925ac9..4a3b34af72cde 100644 --- a/solution/0100-0199/0182.Duplicate Emails/README_EN.md +++ b/solution/0100-0199/0182.Duplicate Emails/README_EN.md @@ -77,6 +77,8 @@ FROM WHERE p1.id != p2.id AND p1.email = p2.email; ``` +### **Pandas** + ```python import pandas as pd @@ -87,7 +89,6 @@ def duplicate_emails(person: pd.DataFrame) -> pd.DataFrame: results = person.loc[person.duplicated(subset=["email"]), ["email"]] return results.drop_duplicates() - ``` diff --git a/solution/0100-0199/0183.Customers Who Never Order/README.md b/solution/0100-0199/0183.Customers Who Never Order/README.md index a6e7013c4f880..606d73069cada 100644 --- a/solution/0100-0199/0183.Customers Who Never Order/README.md +++ b/solution/0100-0199/0183.Customers Who Never Order/README.md @@ -105,6 +105,8 @@ FROM WHERE o.id IS NULL; ``` +### **Pandas** + ```python import pandas as pd @@ -117,7 +119,6 @@ def find_customers(customers: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFram df = df[["name"]].rename(columns={"name": "Customers"}) return df - ``` diff --git a/solution/0100-0199/0183.Customers Who Never Order/README_EN.md b/solution/0100-0199/0183.Customers Who Never Order/README_EN.md index 98f3dc91ab920..1992c74855e8c 100644 --- a/solution/0100-0199/0183.Customers Who Never Order/README_EN.md +++ b/solution/0100-0199/0183.Customers Who Never Order/README_EN.md @@ -105,6 +105,8 @@ FROM WHERE o.id IS NULL; ``` +### **Pandas** + ```python import pandas as pd @@ -117,7 +119,6 @@ def find_customers(customers: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFram df = df[["name"]].rename(columns={"name": "Customers"}) return df - ``` diff --git a/solution/2100-2199/2117.Abbreviating the Product of a Range/README.md b/solution/2100-2199/2117.Abbreviating the Product of a Range/README.md index 64f9a3de88014..b47d7a108c166 100644 --- a/solution/2100-2199/2117.Abbreviating the Product of a Range/README.md +++ b/solution/2100-2199/2117.Abbreviating the Product of a Range/README.md @@ -82,7 +82,72 @@ ```python +import numpy + + +class Solution: + def abbreviateProduct(self, left: int, right: int) -> str: + cnt2 = cnt5 = 0 + z = numpy.float128(0) + for x in range(left, right + 1): + z += numpy.log10(x) + while x % 2 == 0: + x //= 2 + cnt2 += 1 + while x % 5 == 0: + x //= 5 + cnt5 += 1 + c = cnt2 = cnt5 = min(cnt2, cnt5) + suf = y = 1 + gt = False + for x in range(left, right + 1): + while cnt2 and x % 2 == 0: + x //= 2 + cnt2 -= 1 + while cnt5 and x % 5 == 0: + x //= 5 + cnt5 -= 1 + suf = suf * x % 100000 + if not gt: + y *= x + gt = y >= 1e10 + if not gt: + return str(y) + "e" + str(c) + pre = int(pow(10, z - int(z) + 4)) + return str(pre) + "..." + str(suf).zfill(5) + "e" + str(c) +``` +```python +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) ``` ### **Java** @@ -90,12 +155,138 @@ ```java +class Solution { + + public String abbreviateProduct(int left, int right) { + int cnt2 = 0, cnt5 = 0; + for (int i = left; i <= right; ++i) { + int x = i; + for (; x % 2 == 0; x /= 2) { + ++cnt2; + } + for (; x % 5 == 0; x /= 5) { + ++cnt5; + } + } + int c = Math.min(cnt2, cnt5); + cnt2 = cnt5 = c; + long suf = 1; + double pre = 1; + boolean gt = false; + for (int i = left; i <= right; ++i) { + for (suf *= i; cnt2 > 0 && suf % 2 == 0; suf /= 2) { + --cnt2; + } + for (; cnt5 > 0 && suf % 5 == 0; suf /= 5) { + --cnt5; + } + if (suf >= (long) 1e10) { + gt = true; + suf %= (long) 1e10; + } + for (pre *= i; pre > 1e5; pre /= 10) { + } + } + if (gt) { + return (int) pre + "..." + String.format("%05d", suf % (int) 1e5) + "e" + c; + } + return suf + "e" + c; + } +} +``` +### **C++** + +```cpp +class Solution { +public: + string abbreviateProduct(int left, int right) { + int cnt2 = 0, cnt5 = 0; + for (int i = left; i <= right; ++i) { + int x = i; + for (; x % 2 == 0; x /= 2) { + ++cnt2; + } + for (; x % 5 == 0; x /= 5) { + ++cnt5; + } + } + int c = min(cnt2, cnt5); + cnt2 = cnt5 = c; + long long suf = 1; + long double pre = 1; + bool gt = false; + for (int i = left; i <= right; ++i) { + for (suf *= i; cnt2 && suf % 2 == 0; suf /= 2) { + --cnt2; + } + for (; cnt5 && suf % 5 == 0; suf /= 5) { + --cnt5; + } + if (suf >= 1e10) { + gt = true; + suf %= (long long) 1e10; + } + for (pre *= i; pre > 1e5; pre /= 10) { + } + } + if (gt) { + char buf[10]; + snprintf(buf, sizeof(buf), "%0*lld", 5, suf % (int) 1e5); + return to_string((int) pre) + "..." + string(buf) + "e" + to_string(c); + } + return to_string(suf) + "e" + to_string(c); + } +}; ``` -### **TypeScript** +### **Go** + +```go +func abbreviateProduct(left int, right int) string { + cnt2, cnt5 := 0, 0 + for i := left; i <= right; i++ { + x := i + for x%2 == 0 { + cnt2++ + x /= 2 + } + for x%5 == 0 { + cnt5++ + x /= 5 + } + } + c := int(math.Min(float64(cnt2), float64(cnt5))) + cnt2 = c + cnt5 = c + suf := int64(1) + pre := float64(1) + gt := false + for i := left; i <= right; i++ { + for suf *= int64(i); cnt2 > 0 && suf%2 == 0; { + cnt2-- + suf /= int64(2) + } + for cnt5 > 0 && suf%5 == 0 { + cnt5-- + suf /= int64(5) + } + if float64(suf) >= 1e10 { + gt = true + suf %= int64(1e10) + } + for pre *= float64(i); pre > 1e5; { + pre /= 10 + } + } + if gt { + return fmt.Sprintf("%05d...%05de%d", int(pre), int(suf)%int(1e5), c) + } + return fmt.Sprintf("%de%d", suf, c) +} +``` - +### **TypeScript** ```ts diff --git a/solution/2100-2199/2117.Abbreviating the Product of a Range/README_EN.md b/solution/2100-2199/2117.Abbreviating the Product of a Range/README_EN.md index 24a2c5fd67bf8..5cf3f64d4c065 100644 --- a/solution/2100-2199/2117.Abbreviating the Product of a Range/README_EN.md +++ b/solution/2100-2199/2117.Abbreviating the Product of a Range/README_EN.md @@ -73,13 +73,206 @@ Hence, the abbreviated product is "399168e2". ### **Python3** ```python +import numpy + +class Solution: + def abbreviateProduct(self, left: int, right: int) -> str: + cnt2 = cnt5 = 0 + z = numpy.float128(0) + for x in range(left, right + 1): + z += numpy.log10(x) + while x % 2 == 0: + x //= 2 + cnt2 += 1 + while x % 5 == 0: + x //= 5 + cnt5 += 1 + c = cnt2 = cnt5 = min(cnt2, cnt5) + suf = y = 1 + gt = False + for x in range(left, right + 1): + while cnt2 and x % 2 == 0: + x //= 2 + cnt2 -= 1 + while cnt5 and x % 5 == 0: + x //= 5 + cnt5 -= 1 + suf = suf * x % 100000 + if not gt: + y *= x + gt = y >= 1e10 + if not gt: + return str(y) + "e" + str(c) + pre = int(pow(10, z - int(z) + 4)) + return str(pre) + "..." + str(suf).zfill(5) + "e" + str(c) +``` + +```python +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) ``` ### **Java** ```java +class Solution { + + public String abbreviateProduct(int left, int right) { + int cnt2 = 0, cnt5 = 0; + for (int i = left; i <= right; ++i) { + int x = i; + for (; x % 2 == 0; x /= 2) { + ++cnt2; + } + for (; x % 5 == 0; x /= 5) { + ++cnt5; + } + } + int c = Math.min(cnt2, cnt5); + cnt2 = cnt5 = c; + long suf = 1; + double pre = 1; + boolean gt = false; + for (int i = left; i <= right; ++i) { + for (suf *= i; cnt2 > 0 && suf % 2 == 0; suf /= 2) { + --cnt2; + } + for (; cnt5 > 0 && suf % 5 == 0; suf /= 5) { + --cnt5; + } + if (suf >= (long) 1e10) { + gt = true; + suf %= (long) 1e10; + } + for (pre *= i; pre > 1e5; pre /= 10) { + } + } + if (gt) { + return (int) pre + "..." + String.format("%05d", suf % (int) 1e5) + "e" + c; + } + return suf + "e" + c; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + string abbreviateProduct(int left, int right) { + int cnt2 = 0, cnt5 = 0; + for (int i = left; i <= right; ++i) { + int x = i; + for (; x % 2 == 0; x /= 2) { + ++cnt2; + } + for (; x % 5 == 0; x /= 5) { + ++cnt5; + } + } + int c = min(cnt2, cnt5); + cnt2 = cnt5 = c; + long long suf = 1; + long double pre = 1; + bool gt = false; + for (int i = left; i <= right; ++i) { + for (suf *= i; cnt2 && suf % 2 == 0; suf /= 2) { + --cnt2; + } + for (; cnt5 && suf % 5 == 0; suf /= 5) { + --cnt5; + } + if (suf >= 1e10) { + gt = true; + suf %= (long long) 1e10; + } + for (pre *= i; pre > 1e5; pre /= 10) { + } + } + if (gt) { + char buf[10]; + snprintf(buf, sizeof(buf), "%0*lld", 5, suf % (int) 1e5); + return to_string((int) pre) + "..." + string(buf) + "e" + to_string(c); + } + return to_string(suf) + "e" + to_string(c); + } +}; +``` + +### **Go** +```go +func abbreviateProduct(left int, right int) string { + cnt2, cnt5 := 0, 0 + for i := left; i <= right; i++ { + x := i + for x%2 == 0 { + cnt2++ + x /= 2 + } + for x%5 == 0 { + cnt5++ + x /= 5 + } + } + c := int(math.Min(float64(cnt2), float64(cnt5))) + cnt2 = c + cnt5 = c + suf := int64(1) + pre := float64(1) + gt := false + for i := left; i <= right; i++ { + for suf *= int64(i); cnt2 > 0 && suf%2 == 0; { + cnt2-- + suf /= int64(2) + } + for cnt5 > 0 && suf%5 == 0 { + cnt5-- + suf /= int64(5) + } + if float64(suf) >= 1e10 { + gt = true + suf %= int64(1e10) + } + for pre *= float64(i); pre > 1e5; { + pre /= 10 + } + } + if gt { + return fmt.Sprintf("%05d...%05de%d", int(pre), int(suf)%int(1e5), c) + } + return fmt.Sprintf("%de%d", suf, c) +} ``` ### **TypeScript** diff --git a/solution/2100-2199/2117.Abbreviating the Product of a Range/Solution.cpp b/solution/2100-2199/2117.Abbreviating the Product of a Range/Solution.cpp new file mode 100644 index 0000000000000..dfe97f5cf9a61 --- /dev/null +++ b/solution/2100-2199/2117.Abbreviating the Product of a Range/Solution.cpp @@ -0,0 +1,40 @@ +class Solution { +public: + string abbreviateProduct(int left, int right) { + int cnt2 = 0, cnt5 = 0; + for (int i = left; i <= right; ++i) { + int x = i; + for (; x % 2 == 0; x /= 2) { + ++cnt2; + } + for (; x % 5 == 0; x /= 5) { + ++cnt5; + } + } + int c = min(cnt2, cnt5); + cnt2 = cnt5 = c; + long long suf = 1; + long double pre = 1; + bool gt = false; + for (int i = left; i <= right; ++i) { + for (suf *= i; cnt2 && suf % 2 == 0; suf /= 2) { + --cnt2; + } + for (; cnt5 && suf % 5 == 0; suf /= 5) { + --cnt5; + } + if (suf >= 1e10) { + gt = true; + suf %= (long long) 1e10; + } + for (pre *= i; pre > 1e5; pre /= 10) { + } + } + if (gt) { + char buf[10]; + snprintf(buf, sizeof(buf), "%0*lld", 5, suf % (int) 1e5); + return to_string((int) pre) + "..." + string(buf) + "e" + to_string(c); + } + return to_string(suf) + "e" + to_string(c); + } +}; \ No newline at end of file diff --git a/solution/2100-2199/2117.Abbreviating the Product of a Range/Solution.go b/solution/2100-2199/2117.Abbreviating the Product of a Range/Solution.go new file mode 100644 index 0000000000000..544e39c14888f --- /dev/null +++ b/solution/2100-2199/2117.Abbreviating the Product of a Range/Solution.go @@ -0,0 +1,41 @@ +func abbreviateProduct(left int, right int) string { + cnt2, cnt5 := 0, 0 + for i := left; i <= right; i++ { + x := i + for x%2 == 0 { + cnt2++ + x /= 2 + } + for x%5 == 0 { + cnt5++ + x /= 5 + } + } + c := int(math.Min(float64(cnt2), float64(cnt5))) + cnt2 = c + cnt5 = c + suf := int64(1) + pre := float64(1) + gt := false + for i := left; i <= right; i++ { + for suf *= int64(i); cnt2 > 0 && suf%2 == 0; { + cnt2-- + suf /= int64(2) + } + for cnt5 > 0 && suf%5 == 0 { + cnt5-- + suf /= int64(5) + } + if float64(suf) >= 1e10 { + gt = true + suf %= int64(1e10) + } + for pre *= float64(i); pre > 1e5; { + pre /= 10 + } + } + if gt { + return fmt.Sprintf("%05d...%05de%d", int(pre), int(suf)%int(1e5), c) + } + return fmt.Sprintf("%de%d", suf, c) +} \ No newline at end of file diff --git a/solution/2100-2199/2117.Abbreviating the Product of a Range/Solution.java b/solution/2100-2199/2117.Abbreviating the Product of a Range/Solution.java new file mode 100644 index 0000000000000..82745fb60a9bb --- /dev/null +++ b/solution/2100-2199/2117.Abbreviating the Product of a Range/Solution.java @@ -0,0 +1,38 @@ +class Solution { + + public String abbreviateProduct(int left, int right) { + int cnt2 = 0, cnt5 = 0; + for (int i = left; i <= right; ++i) { + int x = i; + for (; x % 2 == 0; x /= 2) { + ++cnt2; + } + for (; x % 5 == 0; x /= 5) { + ++cnt5; + } + } + int c = Math.min(cnt2, cnt5); + cnt2 = cnt5 = c; + long suf = 1; + double pre = 1; + boolean gt = false; + for (int i = left; i <= right; ++i) { + for (suf *= i; cnt2 > 0 && suf % 2 == 0; suf /= 2) { + --cnt2; + } + for (; cnt5 > 0 && suf % 5 == 0; suf /= 5) { + --cnt5; + } + if (suf >= (long) 1e10) { + gt = true; + suf %= (long) 1e10; + } + for (pre *= i; pre > 1e5; pre /= 10) { + } + } + if (gt) { + return (int) pre + "..." + String.format("%05d", suf % (int) 1e5) + "e" + c; + } + return suf + "e" + c; + } +} \ No newline at end of file diff --git a/solution/2100-2199/2117.Abbreviating the Product of a Range/Solution.py b/solution/2100-2199/2117.Abbreviating the Product of a Range/Solution.py new file mode 100644 index 0000000000000..d5737a6158fe0 --- /dev/null +++ b/solution/2100-2199/2117.Abbreviating the Product of a Range/Solution.py @@ -0,0 +1,30 @@ +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) diff --git a/solution/2500-2599/2558.Take Gifts From the Richest Pile/README.md b/solution/2500-2599/2558.Take Gifts From the Richest Pile/README.md index 129834bebf29e..ea086a1832624 100644 --- a/solution/2500-2599/2558.Take Gifts From the Richest Pile/README.md +++ b/solution/2500-2599/2558.Take Gifts From the Richest Pile/README.md @@ -59,11 +59,11 @@ **方法一:优先队列(大根堆)** -我们将数组 `gifts` 转存到大根堆中,然后循环 $k$ 次,每次取出堆顶元素,将堆顶元素开根号的结果再放入堆中。 +我们将数组 $gifts$ 转存到大根堆中,然后循环 $k$ 次,每次取出堆顶元素,将堆顶元素开根号的结果再放入堆中。 最后累加堆中所有元素之和作为答案。 -时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 `gifts` 的长度。 +时间复杂度 $O(n + k \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $gifts$ 的长度。 @@ -140,8 +140,8 @@ func pickGifts(gifts []int, k int) (ans int64) { type hp struct{ sort.IntSlice } func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (hp) Pop() (_ interface{}) { return } -func (hp) Push(interface{}) {} +func (hp) Pop() (_ any) { return } +func (hp) Push(any) {} ``` ### **TypeScript** @@ -149,13 +149,11 @@ func (hp) Push(interface{}) {} ```ts function pickGifts(gifts: number[], k: number): number { const pq = new MaxPriorityQueue(); - for (const v of gifts) { - pq.enqueue(v, v); - } + gifts.forEach(v => pq.enqueue(v)); while (k--) { let v = pq.dequeue().element; v = Math.floor(Math.sqrt(v)); - pq.enqueue(v, v); + pq.enqueue(v); } let ans = 0; while (!pq.isEmpty()) { diff --git a/solution/2500-2599/2558.Take Gifts From the Richest Pile/README_EN.md b/solution/2500-2599/2558.Take Gifts From the Richest Pile/README_EN.md index c4876191c99b4..f5c818f573f3e 100644 --- a/solution/2500-2599/2558.Take Gifts From the Richest Pile/README_EN.md +++ b/solution/2500-2599/2558.Take Gifts From the Richest Pile/README_EN.md @@ -51,6 +51,14 @@ So, the total gifts remaining are 4. ## Solutions +**Solution 1: Priority Queue (Max Heap)** + +We can store the array $gifts$ in a max heap, and then loop $k$ times, each time taking out the top element of the heap, taking the square root of it, and putting the result back into the heap. + +Finally, we add up all the elements in the heap as the answer. + +The time complexity is $O(n + k \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $gifts$. + ### **Python3** @@ -122,8 +130,8 @@ func pickGifts(gifts []int, k int) (ans int64) { type hp struct{ sort.IntSlice } func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (hp) Pop() (_ interface{}) { return } -func (hp) Push(interface{}) {} +func (hp) Pop() (_ any) { return } +func (hp) Push(any) {} ``` ### **TypeScript** @@ -131,13 +139,11 @@ func (hp) Push(interface{}) {} ```ts function pickGifts(gifts: number[], k: number): number { const pq = new MaxPriorityQueue(); - for (const v of gifts) { - pq.enqueue(v, v); - } + gifts.forEach(v => pq.enqueue(v)); while (k--) { let v = pq.dequeue().element; v = Math.floor(Math.sqrt(v)); - pq.enqueue(v, v); + pq.enqueue(v); } let ans = 0; while (!pq.isEmpty()) { diff --git a/solution/2500-2599/2558.Take Gifts From the Richest Pile/Solution.go b/solution/2500-2599/2558.Take Gifts From the Richest Pile/Solution.go index 4e513a0e51ced..c309e9e2647c2 100644 --- a/solution/2500-2599/2558.Take Gifts From the Richest Pile/Solution.go +++ b/solution/2500-2599/2558.Take Gifts From the Richest Pile/Solution.go @@ -14,5 +14,5 @@ func pickGifts(gifts []int, k int) (ans int64) { type hp struct{ sort.IntSlice } func (h hp) Less(i, j int) bool { return h.IntSlice[i] > h.IntSlice[j] } -func (hp) Pop() (_ interface{}) { return } -func (hp) Push(interface{}) {} \ No newline at end of file +func (hp) Pop() (_ any) { return } +func (hp) Push(any) {} \ No newline at end of file diff --git a/solution/2500-2599/2558.Take Gifts From the Richest Pile/Solution.ts b/solution/2500-2599/2558.Take Gifts From the Richest Pile/Solution.ts index 5179a74c4d843..0daa1348ea64b 100644 --- a/solution/2500-2599/2558.Take Gifts From the Richest Pile/Solution.ts +++ b/solution/2500-2599/2558.Take Gifts From the Richest Pile/Solution.ts @@ -1,12 +1,10 @@ function pickGifts(gifts: number[], k: number): number { const pq = new MaxPriorityQueue(); - for (const v of gifts) { - pq.enqueue(v, v); - } + gifts.forEach(v => pq.enqueue(v)); while (k--) { let v = pq.dequeue().element; v = Math.floor(Math.sqrt(v)); - pq.enqueue(v, v); + pq.enqueue(v); } let ans = 0; while (!pq.isEmpty()) {