Skip to content

Commit a5a094c

Browse files
committed
feat: add solutions to lc problems: No.2496~2503
* No.2496.Maximum Value of a String in an Array * No.2497.Maximum Star Sum of a Graph * No.2498.Frog Jump II * No.2500.Delete Greatest Value in Each Row * No.2501.Longest Square Streak in an Array * No.2502.Design Memory Allocator * No.2503.Maximum Number of Points From Grid Queries
1 parent 7f70735 commit a5a094c

File tree

62 files changed

+3383
-29
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+3383
-29
lines changed

solution/0100-0199/0129.Sum Root to Leaf Numbers/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class Solution:
7373
if root.left is None and root.right is None:
7474
return s
7575
return dfs(root.left, s) + dfs(root.right, s)
76-
76+
7777
return dfs(root, 0)
7878
```
7979

solution/0300-0399/0374.Guess Number Higher or Lower/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func guessNumber(n int) int {
216216
### **C#**
217217

218218
```cs
219-
/**
219+
/**
220220
* Forward declaration of guess API.
221221
* @param num your guess
222222
* @return -1 if num is higher than the picked number

solution/0300-0399/0374.Guess Number Higher or Lower/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func guessNumber(n int) int {
192192
### **C#**
193193

194194
```cs
195-
/**
195+
/**
196196
* Forward declaration of guess API.
197197
* @param num your guess
198198
* @return -1 if num is higher than the picked number

solution/2200-2299/2291.Maximum Profit From Trading Stocks/README.md

+18-24
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,44 @@
1-
# [2291. Maximum Profit From Trading Stocks](https://leetcode.cn/problems/maximum-profit-from-trading-stocks)
1+
# [2291. 最大股票收益](https://leetcode.cn/problems/maximum-profit-from-trading-stocks)
22

33
[English Version](/solution/2200-2299/2291.Maximum%20Profit%20From%20Trading%20Stocks/README_EN.md)
44

55
## 题目描述
66

77
<!-- 这里写题目描述 -->
88

9-
<p>You are given two <strong>0-indexed</strong> integer arrays of the same length <code>present</code> and <code>future</code> where <code>present[i]</code> is the current price of the <code>i<sup>th</sup></code> stock and <code>future[i]</code> is the price of the <code>i<sup>th</sup></code> stock a year in the future. You may buy each stock at most <strong>once</strong>. You are also given an integer <code>budget</code> representing the amount of money you currently have.</p>
9+
<p>给你两个下标从 <strong>0</strong>&nbsp;开始的数组 <code>present</code> <code>future</code> <code>present[i]</code> <code>future[i]</code> 分别代表第 <code>i</code> 支股票现在和将来的价格。每支股票你最多购买 <strong>一次</strong> ,你的预算为 <code>budget</code> </p>
1010

11-
<p>Return <em>the maximum amount of profit you can make.</em></p>
11+
<p>求最大的收益。</p>
1212

1313
<p>&nbsp;</p>
14-
<p><strong class="example">Example 1:</strong></p>
14+
15+
<p><strong>示例 1:</strong></p>
1516

1617
<pre>
17-
<strong>Input:</strong> present = [5,4,6,2,3], future = [8,5,4,3,5], budget = 10
18-
<strong>Output:</strong> 6
19-
<strong>Explanation:</strong> One possible way to maximize your profit is to:
20-
Buy the 0<sup>th</sup>, 3<sup>rd</sup>, and 4<sup>th</sup> stocks for a total of 5 + 2 + 3 = 10.
21-
Next year, sell all three stocks for a total of 8 + 3 + 5 = 16.
22-
The profit you made is 16 - 10 = 6.
23-
It can be shown that the maximum profit you can make is 6.
18+
<strong>输入:</strong>present = [5,4,6,2,3], future = [8,5,4,3,5], budget = 10
19+
<strong>输出:</strong>6
20+
<strong>解释:</strong>你可以选择购买第 0,3,4 支股票获得最大收益:6 。总开销为:5 + 2 + 3 = 10 , 总收益是: 8 + 3 + 5 - 10 = 6 。
2421
</pre>
2522

26-
<p><strong class="example">Example 2:</strong></p>
23+
<p><strong>示例 2:</strong></p>
2724

2825
<pre>
29-
<strong>Input:</strong> present = [2,2,5], future = [3,4,10], budget = 6
30-
<strong>Output:</strong> 5
31-
<strong>Explanation:</strong> The only possible way to maximize your profit is to:
32-
Buy the 2<sup>nd</sup> stock, and make a profit of 10 - 5 = 5.
33-
It can be shown that the maximum profit you can make is 5.
26+
<strong>输入:</strong>present = [2,2,5], future = [3,4,10], budget = 6
27+
<strong>输出:</strong>5
28+
<strong>解释:</strong>你可以选择购买第 2 支股票获得最大收益:5 。总开销为:5 , 总收益是: 10 - 5 = 5 。
3429
</pre>
3530

36-
<p><strong class="example">Example 3:</strong></p>
31+
<p><strong>示例 3:</strong></p>
3732

3833
<pre>
39-
<strong>Input:</strong> present = [3,3,12], future = [0,3,15], budget = 10
40-
<strong>Output:</strong> 0
41-
<strong>Explanation:</strong> One possible way to maximize your profit is to:
42-
Buy the 1<sup>st</sup> stock, and make a profit of 3 - 3 = 0.
43-
It can be shown that the maximum profit you can make is 0.
34+
<strong>输入:</strong>present = [3,3,12], future = [0,3,15], budget = 10
35+
<strong>输出:</strong>0
36+
<strong>解释:</strong>你无法购买唯一一支正收益股票 2 ,因此你的收益是 0 。
4437
</pre>
4538

4639
<p>&nbsp;</p>
47-
<p><strong>Constraints:</strong></p>
40+
41+
<p><strong>提示:</strong></p>
4842

4943
<ul>
5044
<li><code>n == present.length == future.length</code></li>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# [2496. 数组中字符串的最大值](https://leetcode.cn/problems/maximum-value-of-a-string-in-an-array)
2+
3+
[English Version](/solution/2400-2499/2496.Maximum%20Value%20of%20a%20String%20in%20an%20Array/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>一个由字母和数字组成的字符串的 <strong>值</strong>&nbsp;定义如下:</p>
10+
11+
<ul>
12+
<li>如果字符串 <strong>只</strong> 包含数字,那么值为该字符串在 <code>10</code>&nbsp;进制下的所表示的数字。</li>
13+
<li>否则,值为字符串的 <strong>长度&nbsp;</strong>。</li>
14+
</ul>
15+
16+
<p>给你一个字符串数组&nbsp;<code>strs</code>&nbsp;,每个字符串都只由字母和数字组成,请你返回 <code>strs</code>&nbsp;中字符串的 <strong>最大值</strong>&nbsp;。</p>
17+
18+
<p>&nbsp;</p>
19+
20+
<p><strong>示例 1:</strong></p>
21+
22+
<pre>
23+
<strong>输入:</strong>strs = ["alic3","bob","3","4","00000"]
24+
<b>输出:</b>5
25+
<b>解释:</b>
26+
- "alic3" 包含字母和数字,所以值为长度 5 。
27+
- "bob" 只包含字母,所以值为长度 3 。
28+
- "3" 只包含数字,所以值为 3 。
29+
- "4" 只包含数字,所以值为 4 。
30+
- "00000" 只包含数字,所以值为 0 。
31+
所以最大的值为 5 ,是字符串 "alic3" 的值。
32+
</pre>
33+
34+
<p><strong>示例 2:</strong></p>
35+
36+
<pre>
37+
<b>输入:</b>strs = ["1","01","001","0001"]
38+
<b>输出:</b>1
39+
<b>解释:</b>
40+
数组中所有字符串的值都是 1 ,所以我们返回 1 。</pre>
41+
42+
<p>&nbsp;</p>
43+
44+
<p><strong>提示:</strong></p>
45+
46+
<ul>
47+
<li><code>1 &lt;= strs.length &lt;= 100</code></li>
48+
<li><code>1 &lt;= strs[i].length &lt;= 9</code></li>
49+
<li><code>strs[i]</code>&nbsp;只包含小写英文字母和数字。</li>
50+
</ul>
51+
52+
## 解法
53+
54+
<!-- 这里可写通用的实现逻辑 -->
55+
56+
**方法一:模拟**
57+
58+
根据题意模拟即可。
59+
60+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是数组 `strs` 的长度。
61+
62+
<!-- tabs:start -->
63+
64+
### **Python3**
65+
66+
<!-- 这里可写当前语言的特殊实现逻辑 -->
67+
68+
```python
69+
class Solution:
70+
def maximumValue(self, strs: List[str]) -> int:
71+
def f(s):
72+
return int(s) if all(c.isdigit() for c in s) else len(s)
73+
74+
return max(f(s) for s in strs)
75+
```
76+
77+
### **Java**
78+
79+
<!-- 这里可写当前语言的特殊实现逻辑 -->
80+
81+
```java
82+
class Solution {
83+
public int maximumValue(String[] strs) {
84+
int ans = 0;
85+
for (String s : strs) {
86+
ans = Math.max(ans, f(s));
87+
}
88+
return ans;
89+
}
90+
91+
private int f(String s) {
92+
for (int i = 0; i < s.length(); ++i) {
93+
if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
94+
return s.length();
95+
}
96+
}
97+
return Integer.parseInt(s);
98+
}
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
int maximumValue(vector<string>& strs) {
108+
auto f = [](string& s) {
109+
int n = s.size(), m = 0;
110+
for (char& c : s) {
111+
if (!isdigit(c)) return n;
112+
m = m * 10 + (c - '0');
113+
}
114+
return m;
115+
};
116+
int ans = 0;
117+
for (auto& s : strs) ans = max(ans, f(s));
118+
return ans;
119+
}
120+
};
121+
```
122+
123+
### **Go**
124+
125+
```go
126+
func maximumValue(strs []string) (ans int) {
127+
f := func(s string) int {
128+
n, m := len(s), 0
129+
for _, c := range s {
130+
if c >= 'a' && c <= 'z' {
131+
return n
132+
}
133+
m = m*10 + int(c-'0')
134+
}
135+
return m
136+
}
137+
for _, s := range strs {
138+
if t := f(s); ans < t {
139+
ans = t
140+
}
141+
}
142+
return
143+
}
144+
```
145+
146+
### **...**
147+
148+
```
149+
150+
```
151+
152+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# [2496. Maximum Value of a String in an Array](https://leetcode.com/problems/maximum-value-of-a-string-in-an-array)
2+
3+
[中文文档](/solution/2400-2499/2496.Maximum%20Value%20of%20a%20String%20in%20an%20Array/README.md)
4+
5+
## Description
6+
7+
<p>The <strong>value</strong> of an alphanumeric string can be defined as:</p>
8+
9+
<ul>
10+
<li>The <strong>numeric</strong> representation of the string in base <code>10</code>, if it comprises of digits <strong>only</strong>.</li>
11+
<li>The <strong>length</strong> of the string, otherwise.</li>
12+
</ul>
13+
14+
<p>Given an array <code>strs</code> of alphanumeric strings, return <em>the <strong>maximum value</strong> of any string in </em><code>strs</code>.</p>
15+
16+
<p>&nbsp;</p>
17+
<p><strong class="example">Example 1:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> strs = [&quot;alic3&quot;,&quot;bob&quot;,&quot;3&quot;,&quot;4&quot;,&quot;00000&quot;]
21+
<strong>Output:</strong> 5
22+
<strong>Explanation:</strong>
23+
- &quot;alic3&quot; consists of both letters and digits, so its value is its length, i.e. 5.
24+
- &quot;bob&quot; consists only of letters, so its value is also its length, i.e. 3.
25+
- &quot;3&quot; consists only of digits, so its value is its numeric equivalent, i.e. 3.
26+
- &quot;4&quot; also consists only of digits, so its value is 4.
27+
- &quot;00000&quot; consists only of digits, so its value is 0.
28+
Hence, the maximum value is 5, of &quot;alic3&quot;.
29+
</pre>
30+
31+
<p><strong class="example">Example 2:</strong></p>
32+
33+
<pre>
34+
<strong>Input:</strong> strs = [&quot;1&quot;,&quot;01&quot;,&quot;001&quot;,&quot;0001&quot;]
35+
<strong>Output:</strong> 1
36+
<strong>Explanation:</strong>
37+
Each string in the array has value 1. Hence, we return 1.
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>1 &lt;= strs.length &lt;= 100</code></li>
45+
<li><code>1 &lt;= strs[i].length &lt;= 9</code></li>
46+
<li><code>strs[i]</code> consists of only lowercase English letters and digits.</li>
47+
</ul>
48+
49+
## Solutions
50+
51+
<!-- tabs:start -->
52+
53+
### **Python3**
54+
55+
```python
56+
class Solution:
57+
def maximumValue(self, strs: List[str]) -> int:
58+
def f(s):
59+
return int(s) if all(c.isdigit() for c in s) else len(s)
60+
61+
return max(f(s) for s in strs)
62+
```
63+
64+
### **Java**
65+
66+
```java
67+
class Solution {
68+
public int maximumValue(String[] strs) {
69+
int ans = 0;
70+
for (String s : strs) {
71+
ans = Math.max(ans, f(s));
72+
}
73+
return ans;
74+
}
75+
76+
private int f(String s) {
77+
for (int i = 0; i < s.length(); ++i) {
78+
if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
79+
return s.length();
80+
}
81+
}
82+
return Integer.parseInt(s);
83+
}
84+
}
85+
```
86+
87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
int maximumValue(vector<string>& strs) {
93+
auto f = [](string& s) {
94+
int n = s.size(), m = 0;
95+
for (char& c : s) {
96+
if (!isdigit(c)) return n;
97+
m = m * 10 + (c - '0');
98+
}
99+
return m;
100+
};
101+
int ans = 0;
102+
for (auto& s : strs) ans = max(ans, f(s));
103+
return ans;
104+
}
105+
};
106+
```
107+
108+
### **Go**
109+
110+
```go
111+
func maximumValue(strs []string) (ans int) {
112+
f := func(s string) int {
113+
n, m := len(s), 0
114+
for _, c := range s {
115+
if c >= 'a' && c <= 'z' {
116+
return n
117+
}
118+
m = m*10 + int(c-'0')
119+
}
120+
return m
121+
}
122+
for _, s := range strs {
123+
if t := f(s); ans < t {
124+
ans = t
125+
}
126+
}
127+
return
128+
}
129+
```
130+
131+
### **...**
132+
133+
```
134+
135+
```
136+
137+
<!-- tabs:end -->

0 commit comments

Comments
 (0)