From 6f992fe49621294e73716a7c068235726d7cccbb Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 13 Feb 2025 08:30:27 +0800 Subject: [PATCH 1/3] feat: add sql solution to lc problem: No.3451 (#4057) No.3451.Find Invalid IP Addresses --- .prettierignore | 1 + .../3451.Find Invalid IP Addresses/README.md | 162 ++++++++++++++++++ .../README_EN.md | 162 ++++++++++++++++++ .../Solution.py | 24 +++ .../Solution.sql | 19 ++ solution/DATABASE_README.md | 1 + solution/DATABASE_README_EN.md | 1 + solution/README.md | 1 + solution/README_EN.md | 1 + 9 files changed, 372 insertions(+) create mode 100644 solution/3400-3499/3451.Find Invalid IP Addresses/README.md create mode 100644 solution/3400-3499/3451.Find Invalid IP Addresses/README_EN.md create mode 100644 solution/3400-3499/3451.Find Invalid IP Addresses/Solution.py create mode 100644 solution/3400-3499/3451.Find Invalid IP Addresses/Solution.sql diff --git a/.prettierignore b/.prettierignore index 13715bc116db8..47b55e9bdf606 100644 --- a/.prettierignore +++ b/.prettierignore @@ -26,3 +26,4 @@ node_modules/ /solution/3100-3199/3150.Invalid Tweets II/Solution.sql /solution/3100-3199/3198.Find Cities in Each State/Solution.sql /solution/3300-3399/3328.Find Cities in Each State II/Solution.sql +/solution/3400-3499/3451.Find Invalid IP Addresses/Solution.sql diff --git a/solution/3400-3499/3451.Find Invalid IP Addresses/README.md b/solution/3400-3499/3451.Find Invalid IP Addresses/README.md new file mode 100644 index 0000000000000..c09c41e732002 --- /dev/null +++ b/solution/3400-3499/3451.Find Invalid IP Addresses/README.md @@ -0,0 +1,162 @@ +--- +comments: true +difficulty: 困难 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3451.Find%20Invalid%20IP%20Addresses/README.md +tags: + - 数据库 +--- + + + +# [3451. Find Invalid IP Addresses](https://leetcode.cn/problems/find-invalid-ip-addresses) + +[English Version](/solution/3400-3499/3451.Find%20Invalid%20IP%20Addresses/README_EN.md) + +## 题目描述 + + + +

Table: logs

+ +
++-------------+---------+
+| Column Name | Type    |
++-------------+---------+
+| log_id      | int     |
+| ip          | varchar |
+| status_code | int     |
++-------------+---------+
+log_id is the unique key for this table.
+Each row contains server access log information including IP address and HTTP status code.
+
+ +

Write a solution to find invalid IP addresses. An IPv4 address is invalid if it meets any of these conditions:

+ + + +

Return the result table ordered by invalid_countip in descending order respectively

+ +

The result format is in the following example.

+ +

 

+

Example:

+ +
+

Input:

+ +

logs table:

+ +
++--------+---------------+-------------+
+| log_id | ip            | status_code |
++--------+---------------+-------------+
+| 1      | 192.168.1.1   | 200         |
+| 2      | 256.1.2.3     | 404         |
+| 3      | 192.168.001.1 | 200         |
+| 4      | 192.168.1.1   | 200         |
+| 5      | 192.168.1     | 500         |
+| 6      | 256.1.2.3     | 404         |
+| 7      | 192.168.001.1 | 200         |
++--------+---------------+-------------+
+
+ +

Output:

+ +
++---------------+--------------+
+| ip            | invalid_count|
++---------------+--------------+
+| 256.1.2.3     | 2            |
+| 192.168.001.1 | 2            |
+| 192.168.1     | 1            |
++---------------+--------------+
+
+ +

Explanation:

+ + + +

The output table is ordered by invalid_count, ip in descending order respectively.

+
+ + + +## 解法 + + + +### 方法一:模拟 + +我们可以根据题意,判断 IP 地址是否不合法,判断的条件有: + +1. IP 地址中的 `.` 的个数不等于 $3$; +2. IP 地址中的某个 octet 以 `0` 开头; +3. IP 地址中的某个 octet 大于 $255$。 + +然后我们将不合法的 IP 地址进行分组,并统计每个不合法的 IP 地址的个数 `invalid_count`,最后按照 `invalid_count` 和 `ip` 降序排序。 + + + +#### MySQL + +```sql +SELECT + ip, + COUNT(*) AS invalid_count +FROM logs +WHERE + LENGTH(ip) - LENGTH(REPLACE(ip, '.', '')) != 3 + OR SUBSTRING_INDEX(ip, '.', 1) REGEXP '^0[0-9]' + OR SUBSTRING_INDEX(SUBSTRING_INDEX(ip, '.', 2), '.', -1) REGEXP '^0[0-9]' + OR SUBSTRING_INDEX(SUBSTRING_INDEX(ip, '.', 3), '.', -1) REGEXP '^0[0-9]' + OR SUBSTRING_INDEX(ip, '.', -1) REGEXP '^0[0-9]' + OR SUBSTRING_INDEX(ip, '.', 1) > 255 + OR SUBSTRING_INDEX(SUBSTRING_INDEX(ip, '.', 2), '.', -1) > 255 + OR SUBSTRING_INDEX(SUBSTRING_INDEX(ip, '.', 3), '.', -1) > 255 + OR SUBSTRING_INDEX(ip, '.', -1) > 255 +GROUP BY 1 +ORDER BY 2 DESC, 1 DESC; +``` + +#### Pandas + +```python +import pandas as pd + + +def find_invalid_ips(logs: pd.DataFrame) -> pd.DataFrame: + def is_valid_ip(ip: str) -> bool: + octets = ip.split(".") + if len(octets) != 4: + return False + for octet in octets: + if not octet.isdigit(): + return False + value = int(octet) + if not 0 <= value <= 255 or octet != str(value): + return False + return True + + logs["is_valid"] = logs["ip"].apply(is_valid_ip) + invalid_ips = logs[~logs["is_valid"]] + invalid_count = invalid_ips["ip"].value_counts().reset_index() + invalid_count.columns = ["ip", "invalid_count"] + result = invalid_count.sort_values( + by=["invalid_count", "ip"], ascending=[False, False] + ) + return result +``` + + + + + + diff --git a/solution/3400-3499/3451.Find Invalid IP Addresses/README_EN.md b/solution/3400-3499/3451.Find Invalid IP Addresses/README_EN.md new file mode 100644 index 0000000000000..354279f8570c3 --- /dev/null +++ b/solution/3400-3499/3451.Find Invalid IP Addresses/README_EN.md @@ -0,0 +1,162 @@ +--- +comments: true +difficulty: Hard +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3451.Find%20Invalid%20IP%20Addresses/README_EN.md +tags: + - Database +--- + + + +# [3451. Find Invalid IP Addresses](https://leetcode.com/problems/find-invalid-ip-addresses) + +[中文文档](/solution/3400-3499/3451.Find%20Invalid%20IP%20Addresses/README.md) + +## Description + + + +

Table: logs

+ +
++-------------+---------+
+| Column Name | Type    |
++-------------+---------+
+| log_id      | int     |
+| ip          | varchar |
+| status_code | int     |
++-------------+---------+
+log_id is the unique key for this table.
+Each row contains server access log information including IP address and HTTP status code.
+
+ +

Write a solution to find invalid IP addresses. An IPv4 address is invalid if it meets any of these conditions:

+ + + +

Return the result table ordered by invalid_countip in descending order respectively

+ +

The result format is in the following example.

+ +

 

+

Example:

+ +
+

Input:

+ +

logs table:

+ +
++--------+---------------+-------------+
+| log_id | ip            | status_code |
++--------+---------------+-------------+
+| 1      | 192.168.1.1   | 200         |
+| 2      | 256.1.2.3     | 404         |
+| 3      | 192.168.001.1 | 200         |
+| 4      | 192.168.1.1   | 200         |
+| 5      | 192.168.1     | 500         |
+| 6      | 256.1.2.3     | 404         |
+| 7      | 192.168.001.1 | 200         |
++--------+---------------+-------------+
+
+ +

Output:

+ +
++---------------+--------------+
+| ip            | invalid_count|
++---------------+--------------+
+| 256.1.2.3     | 2            |
+| 192.168.001.1 | 2            |
+| 192.168.1     | 1            |
++---------------+--------------+
+
+ +

Explanation:

+ + + +

The output table is ordered by invalid_count, ip in descending order respectively.

+
+ + + +## Solutions + + + +### Solution 1: Simulation + +We can determine if an IP address is invalid based on the following conditions: + +1. The number of `.` in the IP address is not equal to $3$; +2. Any octet in the IP address starts with `0`; +3. Any octet in the IP address is greater than $255$. + +Then we group the invalid IP addresses and count the occurrences of each invalid IP address `invalid_count`, and finally sort by `invalid_count` and `ip` in descending order. + + + +#### MySQL + +```sql +SELECT + ip, + COUNT(*) AS invalid_count +FROM logs +WHERE + LENGTH(ip) - LENGTH(REPLACE(ip, '.', '')) != 3 + OR SUBSTRING_INDEX(ip, '.', 1) REGEXP '^0[0-9]' + OR SUBSTRING_INDEX(SUBSTRING_INDEX(ip, '.', 2), '.', -1) REGEXP '^0[0-9]' + OR SUBSTRING_INDEX(SUBSTRING_INDEX(ip, '.', 3), '.', -1) REGEXP '^0[0-9]' + OR SUBSTRING_INDEX(ip, '.', -1) REGEXP '^0[0-9]' + OR SUBSTRING_INDEX(ip, '.', 1) > 255 + OR SUBSTRING_INDEX(SUBSTRING_INDEX(ip, '.', 2), '.', -1) > 255 + OR SUBSTRING_INDEX(SUBSTRING_INDEX(ip, '.', 3), '.', -1) > 255 + OR SUBSTRING_INDEX(ip, '.', -1) > 255 +GROUP BY 1 +ORDER BY 2 DESC, 1 DESC; +``` + +#### Pandas + +```python +import pandas as pd + + +def find_invalid_ips(logs: pd.DataFrame) -> pd.DataFrame: + def is_valid_ip(ip: str) -> bool: + octets = ip.split(".") + if len(octets) != 4: + return False + for octet in octets: + if not octet.isdigit(): + return False + value = int(octet) + if not 0 <= value <= 255 or octet != str(value): + return False + return True + + logs["is_valid"] = logs["ip"].apply(is_valid_ip) + invalid_ips = logs[~logs["is_valid"]] + invalid_count = invalid_ips["ip"].value_counts().reset_index() + invalid_count.columns = ["ip", "invalid_count"] + result = invalid_count.sort_values( + by=["invalid_count", "ip"], ascending=[False, False] + ) + return result +``` + + + + + + diff --git a/solution/3400-3499/3451.Find Invalid IP Addresses/Solution.py b/solution/3400-3499/3451.Find Invalid IP Addresses/Solution.py new file mode 100644 index 0000000000000..e42b6e3f70e56 --- /dev/null +++ b/solution/3400-3499/3451.Find Invalid IP Addresses/Solution.py @@ -0,0 +1,24 @@ +import pandas as pd + + +def find_invalid_ips(logs: pd.DataFrame) -> pd.DataFrame: + def is_valid_ip(ip: str) -> bool: + octets = ip.split(".") + if len(octets) != 4: + return False + for octet in octets: + if not octet.isdigit(): + return False + value = int(octet) + if not 0 <= value <= 255 or octet != str(value): + return False + return True + + logs["is_valid"] = logs["ip"].apply(is_valid_ip) + invalid_ips = logs[~logs["is_valid"]] + invalid_count = invalid_ips["ip"].value_counts().reset_index() + invalid_count.columns = ["ip", "invalid_count"] + result = invalid_count.sort_values( + by=["invalid_count", "ip"], ascending=[False, False] + ) + return result diff --git a/solution/3400-3499/3451.Find Invalid IP Addresses/Solution.sql b/solution/3400-3499/3451.Find Invalid IP Addresses/Solution.sql new file mode 100644 index 0000000000000..bc805b2627642 --- /dev/null +++ b/solution/3400-3499/3451.Find Invalid IP Addresses/Solution.sql @@ -0,0 +1,19 @@ +SELECT + ip, + COUNT(*) AS invalid_count +FROM logs +WHERE + LENGTH(ip) - LENGTH(REPLACE(ip, '.', '')) != 3 + + OR SUBSTRING_INDEX(ip, '.', 1) REGEXP '^0[0-9]' + OR SUBSTRING_INDEX(SUBSTRING_INDEX(ip, '.', 2), '.', -1) REGEXP '^0[0-9]' + OR SUBSTRING_INDEX(SUBSTRING_INDEX(ip, '.', 3), '.', -1) REGEXP '^0[0-9]' + OR SUBSTRING_INDEX(ip, '.', -1) REGEXP '^0[0-9]' + + OR SUBSTRING_INDEX(ip, '.', 1) > 255 + OR SUBSTRING_INDEX(SUBSTRING_INDEX(ip, '.', 2), '.', -1) > 255 + OR SUBSTRING_INDEX(SUBSTRING_INDEX(ip, '.', 3), '.', -1) > 255 + OR SUBSTRING_INDEX(ip, '.', -1) > 255 + +GROUP BY 1 +ORDER BY 2 DESC, 1 DESC; diff --git a/solution/DATABASE_README.md b/solution/DATABASE_README.md index a0b774b80f5b0..511eed53830c1 100644 --- a/solution/DATABASE_README.md +++ b/solution/DATABASE_README.md @@ -309,6 +309,7 @@ | 3415 | [查找具有三个连续数字的产品](/solution/3400-3499/3415.Find%20Products%20with%20Three%20Consecutive%20Digits/README.md) | `数据库` | 简单 | 🔒 | | 3421 | [查找进步的学生](/solution/3400-3499/3421.Find%20Students%20Who%20Improved/README.md) | `数据库` | 中等 | | | 3436 | [查找合法邮箱](/solution/3400-3499/3436.Find%20Valid%20Emails/README.md) | `数据库` | 简单 | | +| 3451 | [Find Invalid IP Addresses](/solution/3400-3499/3451.Find%20Invalid%20IP%20Addresses/README.md) | | 困难 | | ## 版权 diff --git a/solution/DATABASE_README_EN.md b/solution/DATABASE_README_EN.md index bddcbccb696cd..77a7de88cae67 100644 --- a/solution/DATABASE_README_EN.md +++ b/solution/DATABASE_README_EN.md @@ -307,6 +307,7 @@ Press Control + F(or Command + F on | 3415 | [Find Products with Three Consecutive Digits](/solution/3400-3499/3415.Find%20Products%20with%20Three%20Consecutive%20Digits/README_EN.md) | `Database` | Easy | 🔒 | | 3421 | [Find Students Who Improved](/solution/3400-3499/3421.Find%20Students%20Who%20Improved/README_EN.md) | `Database` | Medium | | | 3436 | [Find Valid Emails](/solution/3400-3499/3436.Find%20Valid%20Emails/README_EN.md) | `Database` | Easy | | +| 3451 | [Find Invalid IP Addresses](/solution/3400-3499/3451.Find%20Invalid%20IP%20Addresses/README_EN.md) | | Hard | | ## Copyright diff --git a/solution/README.md b/solution/README.md index e72fac6f5073b..2ddadf109c1da 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3461,6 +3461,7 @@ | 3448 | [统计可以被最后一个数位整除的子字符串数目](/solution/3400-3499/3448.Count%20Substrings%20Divisible%20By%20Last%20Digit/README.md) | | 困难 | 第 436 场周赛 | | 3449 | [最大化游戏分数的最小值](/solution/3400-3499/3449.Maximize%20the%20Minimum%20Game%20Score/README.md) | | 困难 | 第 436 场周赛 | | 3450 | [一张长椅的上最多学生](/solution/3400-3499/3450.Maximum%20Students%20on%20a%20Single%20Bench/README.md) | | 简单 | 🔒 | +| 3451 | [Find Invalid IP Addresses](/solution/3400-3499/3451.Find%20Invalid%20IP%20Addresses/README.md) | | 困难 | | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index d9f44db234e4a..afdb9763b21e2 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3459,6 +3459,7 @@ Press Control + F(or Command + F on | 3448 | [Count Substrings Divisible By Last Digit](/solution/3400-3499/3448.Count%20Substrings%20Divisible%20By%20Last%20Digit/README_EN.md) | | Hard | Weekly Contest 436 | | 3449 | [Maximize the Minimum Game Score](/solution/3400-3499/3449.Maximize%20the%20Minimum%20Game%20Score/README_EN.md) | | Hard | Weekly Contest 436 | | 3450 | [Maximum Students on a Single Bench](/solution/3400-3499/3450.Maximum%20Students%20on%20a%20Single%20Bench/README_EN.md) | | Easy | 🔒 | +| 3451 | [Find Invalid IP Addresses](/solution/3400-3499/3451.Find%20Invalid%20IP%20Addresses/README_EN.md) | | Hard | | ## Copyright From 697263f41ecb402993abc6accd42a4f5e6163cac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Velimir=20=C4=90urkovi=C4=87?= Date: Thu, 13 Feb 2025 01:30:52 +0100 Subject: [PATCH 2/3] feat: add solution to lc problem: No.0027 (#4056) --- solution/0000-0099/0027.Remove Element/README.md | 16 ++++++++++++++++ .../0000-0099/0027.Remove Element/README_EN.md | 16 ++++++++++++++++ .../0000-0099/0027.Remove Element/Solution.cs | 11 +++++++++++ 3 files changed, 43 insertions(+) create mode 100644 solution/0000-0099/0027.Remove Element/Solution.cs diff --git a/solution/0000-0099/0027.Remove Element/README.md b/solution/0000-0099/0027.Remove Element/README.md index 66512c052c070..e0a104c1eb0d9 100644 --- a/solution/0000-0099/0027.Remove Element/README.md +++ b/solution/0000-0099/0027.Remove Element/README.md @@ -205,6 +205,22 @@ var removeElement = function (nums, val) { }; ``` +#### C# + +```cs +public class Solution { + public int RemoveElement(int[] nums, int val) { + int k = 0; + foreach (int x in nums) { + if (x != val) { + nums[k++] = x; + } + } + return k; + } +} +``` + #### PHP ```php diff --git a/solution/0000-0099/0027.Remove Element/README_EN.md b/solution/0000-0099/0027.Remove Element/README_EN.md index e033fd507fbbd..6bbc22e167933 100644 --- a/solution/0000-0099/0027.Remove Element/README_EN.md +++ b/solution/0000-0099/0027.Remove Element/README_EN.md @@ -205,6 +205,22 @@ var removeElement = function (nums, val) { }; ``` +#### C# + +```cs +public class Solution { + public int RemoveElement(int[] nums, int val) { + int k = 0; + foreach (int x in nums) { + if (x != val) { + nums[k++] = x; + } + } + return k; + } +} +``` + #### PHP ```php diff --git a/solution/0000-0099/0027.Remove Element/Solution.cs b/solution/0000-0099/0027.Remove Element/Solution.cs new file mode 100644 index 0000000000000..1ded258b8a3eb --- /dev/null +++ b/solution/0000-0099/0027.Remove Element/Solution.cs @@ -0,0 +1,11 @@ +public class Solution { + public int RemoveElement(int[] nums, int val) { + int k = 0; + foreach (int x in nums) { + if (x != val) { + nums[k++] = x; + } + } + return k; + } +} \ No newline at end of file From 47462d9235538689669411ec883b02ca04ae6102 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Thu, 13 Feb 2025 09:25:47 +0800 Subject: [PATCH 3/3] feat: add solutions to lc problem: No.1742 (#4058) No.1742.Maximum Number of Balls in a Box --- .../README.md | 67 ++++++++++++++++++- .../README_EN.md | 67 ++++++++++++++++++- .../Solution.cs | 15 +++++ .../Solution.js | 16 +++++ .../Solution.rs | 15 +++++ 5 files changed, 174 insertions(+), 6 deletions(-) create mode 100644 solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.cs create mode 100644 solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.js create mode 100644 solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.rs diff --git a/solution/1700-1799/1742.Maximum Number of Balls in a Box/README.md b/solution/1700-1799/1742.Maximum Number of Balls in a Box/README.md index 201650a5221bf..fdbf53dd7d59c 100644 --- a/solution/1700-1799/1742.Maximum Number of Balls in a Box/README.md +++ b/solution/1700-1799/1742.Maximum Number of Balls in a Box/README.md @@ -76,11 +76,11 @@ tags: ### 方法一:数组 + 模拟 -观察题目的数据范围,小球的编号最大不超过 $10^5$,那么每个编号的各个位数之和的最大值小于 $50$。因此,我们可以直接开一个长度为 $50$ 的数组 $cnt$ 来统计每个编号的各个位数之和的数量。 +观察题目的数据范围,小球的编号最大不超过 $10^5$,那么每个编号的各个位数之和的最大值小于 $50$。因此,我们可以直接开一个长度为 $50$ 的数组 $\textit{cnt}$ 来统计每个编号的各个位数之和的数量。 -答案就是数组 $cnt$ 中的最大值。 +答案就是数组 $\textit{cnt}$ 中的最大值。 -时间复杂度 $O(n \times \log_{10}m)$。其中 $n = highLimit - lowLimit + 1$,而 $m = highLimit$。 +时间复杂度 $O(n \times \log_{10}m)$。其中 $n = \textit{highLimit} - \textit{lowLimit} + 1$,而 $m = \textit{highLimit}$。 @@ -172,6 +172,67 @@ function countBalls(lowLimit: number, highLimit: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn count_balls(low_limit: i32, high_limit: i32) -> i32 { + let mut cnt = vec![0; 50]; + for x in low_limit..=high_limit { + let mut y = 0; + let mut n = x; + while n > 0 { + y += n % 10; + n /= 10; + } + cnt[y as usize] += 1; + } + *cnt.iter().max().unwrap() + } +} +``` + +#### JavaScript + +```js +/** + * @param {number} lowLimit + * @param {number} highLimit + * @return {number} + */ +var countBalls = function (lowLimit, highLimit) { + const cnt = Array(50).fill(0); + for (let i = lowLimit; i <= highLimit; ++i) { + let y = 0; + for (let x = i; x; x = Math.floor(x / 10)) { + y += x % 10; + } + ++cnt[y]; + } + return Math.max(...cnt); +}; +``` + +#### C# + +```cs +public class Solution { + public int CountBalls(int lowLimit, int highLimit) { + int[] cnt = new int[50]; + for (int x = lowLimit; x <= highLimit; x++) { + int y = 0; + int n = x; + while (n > 0) { + y += n % 10; + n /= 10; + } + cnt[y]++; + } + return cnt.Max(); + } +} +``` + diff --git a/solution/1700-1799/1742.Maximum Number of Balls in a Box/README_EN.md b/solution/1700-1799/1742.Maximum Number of Balls in a Box/README_EN.md index f76f6e7f9877a..519ee846cc15e 100644 --- a/solution/1700-1799/1742.Maximum Number of Balls in a Box/README_EN.md +++ b/solution/1700-1799/1742.Maximum Number of Balls in a Box/README_EN.md @@ -74,11 +74,11 @@ Box 10 has the most number of balls with 2 balls. ### Solution 1: Array + Simulation -Observing the data range of the problem, the maximum number of the ball does not exceed $10^5$, so the maximum value of the sum of each digit of the number is less than $50$. Therefore, we can directly create an array $cnt$ with a length of $50$ to count the number of each digit sum of each number. +Observing the problem's data range, the maximum number of balls does not exceed $10^5$, so the maximum sum of the digits of each number is less than $50$. Therefore, we can directly create an array $\textit{cnt}$ of length $50$ to count the number of occurrences of each digit sum. -The answer is the maximum value in the array $cnt$. +The answer is the maximum value in the array $\textit{cnt}$. -The time complexity is $O(n \times \log_{10}m)$. Here, $n = highLimit - lowLimit + 1$, and $m = highLimit$. +The time complexity is $O(n \times \log_{10}m)$. Here, $n = \textit{highLimit} - \textit{lowLimit} + 1$, and $m = \textit{highLimit}$. @@ -170,6 +170,67 @@ function countBalls(lowLimit: number, highLimit: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn count_balls(low_limit: i32, high_limit: i32) -> i32 { + let mut cnt = vec![0; 50]; + for x in low_limit..=high_limit { + let mut y = 0; + let mut n = x; + while n > 0 { + y += n % 10; + n /= 10; + } + cnt[y as usize] += 1; + } + *cnt.iter().max().unwrap() + } +} +``` + +#### JavaScript + +```js +/** + * @param {number} lowLimit + * @param {number} highLimit + * @return {number} + */ +var countBalls = function (lowLimit, highLimit) { + const cnt = Array(50).fill(0); + for (let i = lowLimit; i <= highLimit; ++i) { + let y = 0; + for (let x = i; x; x = Math.floor(x / 10)) { + y += x % 10; + } + ++cnt[y]; + } + return Math.max(...cnt); +}; +``` + +#### C# + +```cs +public class Solution { + public int CountBalls(int lowLimit, int highLimit) { + int[] cnt = new int[50]; + for (int x = lowLimit; x <= highLimit; x++) { + int y = 0; + int n = x; + while (n > 0) { + y += n % 10; + n /= 10; + } + cnt[y]++; + } + return cnt.Max(); + } +} +``` + diff --git a/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.cs b/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.cs new file mode 100644 index 0000000000000..70de08a91ab94 --- /dev/null +++ b/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.cs @@ -0,0 +1,15 @@ +public class Solution { + public int CountBalls(int lowLimit, int highLimit) { + int[] cnt = new int[50]; + for (int x = lowLimit; x <= highLimit; x++) { + int y = 0; + int n = x; + while (n > 0) { + y += n % 10; + n /= 10; + } + cnt[y]++; + } + return cnt.Max(); + } +} diff --git a/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.js b/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.js new file mode 100644 index 0000000000000..53c0f0670b57e --- /dev/null +++ b/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.js @@ -0,0 +1,16 @@ +/** + * @param {number} lowLimit + * @param {number} highLimit + * @return {number} + */ +var countBalls = function (lowLimit, highLimit) { + const cnt = Array(50).fill(0); + for (let i = lowLimit; i <= highLimit; ++i) { + let y = 0; + for (let x = i; x; x = Math.floor(x / 10)) { + y += x % 10; + } + ++cnt[y]; + } + return Math.max(...cnt); +}; diff --git a/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.rs b/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.rs new file mode 100644 index 0000000000000..8d0cd508c82ac --- /dev/null +++ b/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.rs @@ -0,0 +1,15 @@ +impl Solution { + pub fn count_balls(low_limit: i32, high_limit: i32) -> i32 { + let mut cnt = vec![0; 50]; + for x in low_limit..=high_limit { + let mut y = 0; + let mut n = x; + while n > 0 { + y += n % 10; + n /= 10; + } + cnt[y as usize] += 1; + } + *cnt.iter().max().unwrap() + } +}