Skip to content

Commit fa90acc

Browse files
committed
feat: add solutions to lc problems: No.2540~2547
* No.2540.Minimum Common Value * No.2541.Minimum Operations to Make Array Equal II * No.2542.Maximum Subsequence Score * No.2543.Check if Point Is Reachable * No.2544.Alternating Digit Sum * No.2545.Sort the Students by Their Kth Score * No.2546.Apply Bitwise Operations to Make Strings Equal * No.2547.Minimum Cost to Split an Array
1 parent ad956dd commit fa90acc

File tree

61 files changed

+2875
-25
lines changed

Some content is hidden

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

61 files changed

+2875
-25
lines changed

solution/0100-0199/0117.Populating Next Right Pointers in Each Node II/README_EN.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class Solution:
109109
if prev:
110110
prev.next = curr
111111
prev = curr
112-
112+
113113
node = root
114114
while node:
115115
prev = next = None
@@ -133,7 +133,7 @@ class Node {
133133
public Node next;
134134
135135
public Node() {}
136-
136+
137137
public Node(int _val) {
138138
val = _val;
139139
}
@@ -185,7 +185,7 @@ class Node {
185185
public Node next;
186186
187187
public Node() {}
188-
188+
189189
public Node(int _val) {
190190
val = _val;
191191
}

solution/0200-0299/0222.Count Complete Tree Nodes/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class Solution:
107107
d += 1
108108
root = root.left
109109
return d
110-
110+
111111
if root is None:
112112
return 0
113113
left, right = depth(root.left), depth(root.right)

solution/0200-0299/0222.Count Complete Tree Nodes/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class Solution:
7676
d += 1
7777
root = root.left
7878
return d
79-
79+
8080
if root is None:
8181
return 0
8282
left, right = depth(root.left), depth(root.right)

solution/0800-0899/0812.Largest Triangle Area/README.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,33 @@
66

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

9-
<p>给定包含多个点的集合,从其中取三个点组成三角形,返回能组成的最大三角形的面积。</p>
9+
<p>给你一个由 <strong>X-Y</strong> 平面上的点组成的数组 <code>points</code> ,其中 <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> 。从其中取任意三个不同的点组成三角形,返回能组成的最大三角形的面积。与真实值误差在 <code>10<sup>-5</sup></code> 内的答案将会视为正确答案<strong>。</strong></p>
10+
11+
<p>&nbsp;</p>
12+
13+
<p><strong class="example">示例 1:</strong></p>
14+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0800-0899/0812.Largest%20Triangle%20Area/images/1027.png" style="height: 369px; width: 450px;" />
15+
<pre>
16+
<strong>输入:</strong>points = [[0,0],[0,1],[1,0],[0,2],[2,0]]
17+
<strong>输出:</strong>2.00000
18+
<strong>解释:</strong>输入中的 5 个点如上图所示,红色的三角形面积最大。
19+
</pre>
20+
21+
<p><strong class="example">示例 2:</strong></p>
1022

1123
<pre>
12-
<strong>示例:</strong>
13-
<strong>输入:</strong> points = [[0,0],[0,1],[1,0],[0,2],[2,0]]
14-
<strong>输出:</strong> 2
15-
<strong>解释:</strong>
16-
这五个点如下图所示。组成的橙色三角形是最大的,面积为2。
24+
<strong>输入:</strong>points = [[1,0],[0,0],[0,1]]
25+
<strong>输出:</strong>0.50000
1726
</pre>
1827

19-
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0800-0899/0812.Largest%20Triangle%20Area/images/1027.png" style="height:328px; width:400px" /></p>
28+
<p>&nbsp;</p>
2029

21-
<p><strong>注意: </strong></p>
30+
<p><strong>提示:</strong></p>
2231

2332
<ul>
24-
<li><code>3 &lt;= points.length &lt;= 50</code>.</li>
25-
<li>不存在重复的点。</li>
26-
<li>&nbsp;<code>-50 &lt;= points[i][j] &lt;= 50</code>.</li>
27-
<li>结果误差值在&nbsp;<code>10^-6</code>&nbsp;以内都认为是正确答案。</li>
33+
<li><code>3 &lt;= points.length &lt;= 50</code></li>
34+
<li><code>-50 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 50</code></li>
35+
<li>给出的所有点 <strong>互不相同</strong></li>
2836
</ul>
2937

3038
## 解法

solution/1800-1899/1833.Maximum Ice Cream Bars/README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,37 @@
1010

1111
<p>商店中新到 <code>n</code> 支雪糕,用长度为 <code>n</code> 的数组 <code>costs</code> 表示雪糕的定价,其中 <code>costs[i]</code> 表示第 <code>i</code> 支雪糕的现金价格。Tony 一共有 <code>coins</code> 现金可以用于消费,他想要买尽可能多的雪糕。</p>
1212

13-
<p>给你价格数组 <code>costs</code> 和现金量 <code>coins</code> ,请你计算并返回 Tony 用 <code>coins</code> 现金能够买到的雪糕的 <strong>最大数量</strong> 。</p>
14-
1513
<p><strong>注意:</strong>Tony 可以按任意顺序购买雪糕。</p>
1614

17-
<p> </p>
15+
<p>给你价格数组 <code>costs</code> 和现金量 <code>coins</code> ,请你计算并返回 Tony 用 <code>coins</code> 现金能够买到的雪糕的 <strong>最大数量</strong> 。</p>
16+
17+
<p>&nbsp;</p>
1818

1919
<p><strong>示例 1:</strong></p>
2020

21-
<pre><strong>输入:</strong>costs = [1,3,2,4,1], coins = 7
21+
<pre>
22+
<strong>输入:</strong>costs = [1,3,2,4,1], coins = 7
2223
<strong>输出:</strong>4
2324
<strong>解释:</strong>Tony 可以买下标为 0、1、2、4 的雪糕,总价为 1 + 3 + 2 + 1 = 7
2425
</pre>
2526

2627
<p><strong>示例 2:</strong></p>
2728

28-
<pre><strong>输入:</strong>costs = [10,6,8,7,7,8], coins = 5
29+
<pre>
30+
<strong>输入:</strong>costs = [10,6,8,7,7,8], coins = 5
2931
<strong>输出:</strong>0
3032
<strong>解释:</strong>Tony 没有足够的钱买任何一支雪糕。
3133
</pre>
3234

3335
<p><strong>示例 3:</strong></p>
3436

35-
<pre><strong>输入:</strong>costs = [1,6,3,1,2,5], coins = 20
37+
<pre>
38+
<strong>输入:</strong>costs = [1,6,3,1,2,5], coins = 20
3639
<strong>输出:</strong>6
3740
<strong>解释:</strong>Tony 可以买下所有的雪糕,总价为 1 + 6 + 3 + 1 + 2 + 5 = 18 。
3841
</pre>
3942

40-
<p> </p>
43+
<p>&nbsp;</p>
4144

4245
<p><strong>提示:</strong></p>
4346

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# [2540. 最小公共值](https://leetcode.cn/problems/minimum-common-value)
2+
3+
[English Version](/solution/2500-2599/2540.Minimum%20Common%20Value/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你两个整数数组&nbsp;<code>nums1</code> 和&nbsp;<code>nums2</code>&nbsp;,它们已经按非降序排序,请你返回两个数组的 <strong>最小公共整数</strong>&nbsp;。如果两个数组&nbsp;<code>nums1</code> 和&nbsp;<code>nums2</code>&nbsp;没有公共整数,请你返回&nbsp;<code>-1</code>&nbsp;。</p>
10+
11+
<p>如果一个整数在两个数组中都 <strong>至少出现一次</strong>&nbsp;,那么这个整数是数组&nbsp;<code>nums1</code> 和&nbsp;<code>nums2</code>&nbsp;<strong>公共</strong>&nbsp;的。</p>
12+
13+
<p>&nbsp;</p>
14+
15+
<p><strong>示例 1:</strong></p>
16+
17+
<pre><b>输入:</b>nums1 = [1,2,3], nums2 = [2,4]
18+
<b>输出:</b>2
19+
<b>解释:</b>两个数组的最小公共元素是 2 ,所以我们返回 2 。
20+
</pre>
21+
22+
<p><strong>示例 2:</strong></p>
23+
24+
<pre><b>输入:</b>nums1 = [1,2,3,6], nums2 = [2,3,4,5]
25+
<b>输出:</b>2
26+
<b>解释:</b>两个数组中的公共元素是 2 和 3 ,2 是较小值,所以返回 2 。
27+
</pre>
28+
29+
<p>&nbsp;</p>
30+
31+
<p><strong>提示:</strong></p>
32+
33+
<ul>
34+
<li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5</sup></code></li>
35+
<li><code>1 &lt;= nums1[i], nums2[j] &lt;= 10<sup>9</sup></code></li>
36+
<li><code>nums1</code> 和&nbsp;<code>nums2</code>&nbsp;都是 <strong>非降序</strong>&nbsp;的。</li>
37+
</ul>
38+
39+
## 解法
40+
41+
<!-- 这里可写通用的实现逻辑 -->
42+
43+
<!-- tabs:start -->
44+
45+
### **Python3**
46+
47+
<!-- 这里可写当前语言的特殊实现逻辑 -->
48+
49+
```python
50+
class Solution:
51+
def getCommon(self, nums1: List[int], nums2: List[int]) -> int:
52+
i = j = 0
53+
m, n = len(nums1), len(nums2)
54+
while i < m and j < n:
55+
if nums1[i] == nums2[j]:
56+
return nums1[i]
57+
if nums1[i] < nums2[j]:
58+
i += 1
59+
else:
60+
j += 1
61+
return -1
62+
```
63+
64+
### **Java**
65+
66+
<!-- 这里可写当前语言的特殊实现逻辑 -->
67+
68+
```java
69+
class Solution {
70+
public int getCommon(int[] nums1, int[] nums2) {
71+
int m = nums1.length, n = nums2.length;
72+
for (int i = 0, j = 0; i < m && j < n;) {
73+
if (nums1[i] == nums2[j]) {
74+
return nums1[i];
75+
}
76+
if (nums1[i] < nums2[j]) {
77+
++i;
78+
} else {
79+
++j;
80+
}
81+
}
82+
return -1;
83+
}
84+
}
85+
```
86+
87+
### **C++**
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
int getCommon(vector<int>& nums1, vector<int>& nums2) {
93+
int m = nums1.size(), n = nums2.size();
94+
for (int i = 0, j = 0; i < m && j < n;) {
95+
if (nums1[i] == nums2[j]) {
96+
return nums1[i];
97+
}
98+
if (nums1[i] < nums2[j]) {
99+
++i;
100+
} else {
101+
++j;
102+
}
103+
}
104+
return -1;
105+
}
106+
};
107+
```
108+
109+
### **Go**
110+
111+
```go
112+
func getCommon(nums1 []int, nums2 []int) int {
113+
m, n := len(nums1), len(nums2)
114+
for i, j := 0, 0; i < m && j < n; {
115+
if nums1[i] == nums2[j] {
116+
return nums1[i]
117+
}
118+
if nums1[i] < nums2[j] {
119+
i++
120+
} else {
121+
j++
122+
}
123+
}
124+
return -1
125+
}
126+
```
127+
128+
### **...**
129+
130+
```
131+
132+
```
133+
134+
<!-- tabs:end -->
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# [2540. Minimum Common Value](https://leetcode.com/problems/minimum-common-value)
2+
3+
[中文文档](/solution/2500-2599/2540.Minimum%20Common%20Value/README.md)
4+
5+
## Description
6+
7+
<p>Given two integer arrays <code>nums1</code> and <code>nums2</code>, sorted in non-decreasing order, return <em>the <strong>minimum integer common</strong> to both arrays</em>. If there is no common integer amongst <code>nums1</code> and <code>nums2</code>, return <code>-1</code>.</p>
8+
9+
<p>Note that an integer is said to be <strong>common</strong> to <code>nums1</code> and <code>nums2</code> if both arrays have <strong>at least one</strong> occurrence of that integer.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong class="example">Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> nums1 = [1,2,3], nums2 = [2,4]
16+
<strong>Output:</strong> 2
17+
<strong>Explanation:</strong> The smallest element common to both arrays is 2, so we return 2.
18+
</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> nums1 = [1,2,3,6], nums2 = [2,3,4,5]
24+
<strong>Output:</strong> 2
25+
<strong>Explanation:</strong> There are two common elements in the array 2 and 3 out of which 2 is the smallest, so 2 is returned.
26+
</pre>
27+
28+
<p>&nbsp;</p>
29+
<p><strong>Constraints:</strong></p>
30+
31+
<ul>
32+
<li><code>1 &lt;= nums1.length, nums2.length &lt;= 10<sup>5</sup></code></li>
33+
<li><code>1 &lt;= nums1[i], nums2[j] &lt;= 10<sup>9</sup></code></li>
34+
<li>Both <code>nums1</code> and <code>nums2</code> are sorted in <strong>non-decreasing</strong> order.</li>
35+
</ul>
36+
37+
## Solutions
38+
39+
<!-- tabs:start -->
40+
41+
### **Python3**
42+
43+
```python
44+
class Solution:
45+
def getCommon(self, nums1: List[int], nums2: List[int]) -> int:
46+
i = j = 0
47+
m, n = len(nums1), len(nums2)
48+
while i < m and j < n:
49+
if nums1[i] == nums2[j]:
50+
return nums1[i]
51+
if nums1[i] < nums2[j]:
52+
i += 1
53+
else:
54+
j += 1
55+
return -1
56+
```
57+
58+
### **Java**
59+
60+
```java
61+
class Solution {
62+
public int getCommon(int[] nums1, int[] nums2) {
63+
int m = nums1.length, n = nums2.length;
64+
for (int i = 0, j = 0; i < m && j < n;) {
65+
if (nums1[i] == nums2[j]) {
66+
return nums1[i];
67+
}
68+
if (nums1[i] < nums2[j]) {
69+
++i;
70+
} else {
71+
++j;
72+
}
73+
}
74+
return -1;
75+
}
76+
}
77+
```
78+
79+
### **C++**
80+
81+
```cpp
82+
class Solution {
83+
public:
84+
int getCommon(vector<int>& nums1, vector<int>& nums2) {
85+
int m = nums1.size(), n = nums2.size();
86+
for (int i = 0, j = 0; i < m && j < n;) {
87+
if (nums1[i] == nums2[j]) {
88+
return nums1[i];
89+
}
90+
if (nums1[i] < nums2[j]) {
91+
++i;
92+
} else {
93+
++j;
94+
}
95+
}
96+
return -1;
97+
}
98+
};
99+
```
100+
101+
### **Go**
102+
103+
```go
104+
func getCommon(nums1 []int, nums2 []int) int {
105+
m, n := len(nums1), len(nums2)
106+
for i, j := 0, 0; i < m && j < n; {
107+
if nums1[i] == nums2[j] {
108+
return nums1[i]
109+
}
110+
if nums1[i] < nums2[j] {
111+
i++
112+
} else {
113+
j++
114+
}
115+
}
116+
return -1
117+
}
118+
```
119+
120+
### **...**
121+
122+
```
123+
124+
```
125+
126+
<!-- tabs:end -->

0 commit comments

Comments
 (0)