Skip to content

Commit 8daacfe

Browse files
committed
feat: add solutions to lcci problem: No.10.01
* Add solutions to lcci problem: No.10.01.Sorted Merge * Add new lc problem
1 parent 4806eac commit 8daacfe

File tree

14 files changed

+338
-22
lines changed

14 files changed

+338
-22
lines changed

Diff for: lcci/10.01.Sorted Merge/README.md

+58-2
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,38 @@ B = [2,5,6], n = 3
3434
<!-- 这里可写当前语言的特殊实现逻辑 -->
3535

3636
```python
37-
37+
class Solution:
38+
def merge(self, A: List[int], m: int, B: List[int], n: int) -> None:
39+
"""
40+
Do not return anything, modify A in-place instead.
41+
"""
42+
i, j = m - 1, n - 1
43+
for k in range(len(A) - 1, -1, -1):
44+
if j < 0 or (i >= 0 and A[i] >= B[j]):
45+
A[k] = A[i]
46+
i -= 1
47+
else:
48+
A[k] = B[j]
49+
j -= 1
3850
```
3951

4052
### **Java**
4153

4254
<!-- 这里可写当前语言的特殊实现逻辑 -->
4355

4456
```java
45-
57+
class Solution {
58+
public void merge(int[] A, int m, int[] B, int n) {
59+
int i = m - 1, j = n - 1;
60+
for (int k = A.length - 1; k >= 0; --k) {
61+
if (j < 0 || (i >= 0 && A[i] >= B[j])) {
62+
A[k] = A[i--];
63+
} else {
64+
A[k] = B[j--];
65+
}
66+
}
67+
}
68+
}
4669
```
4770

4871
### **JavaScript**
@@ -114,6 +137,39 @@ impl Solution {
114137
}
115138
```
116139

140+
### **C++**
141+
142+
```cpp
143+
class Solution {
144+
public:
145+
void merge(vector<int>& A, int m, vector<int>& B, int n) {
146+
int i = m - 1, j = n - 1;
147+
for (int k = A.size() - 1; k >= 0; --k)
148+
{
149+
if (j < 0 || (i >= 0 && A[i] >= B[j])) A[k] = A[i--];
150+
else A[k] = B[j--];
151+
}
152+
}
153+
};
154+
```
155+
156+
### **Go**
157+
158+
```go
159+
func merge(A []int, m int, B []int, n int) {
160+
i, j := m-1, n-1
161+
for k := len(A) - 1; k >= 0; k-- {
162+
if j < 0 || (i >= 0 && A[i] >= B[j]) {
163+
A[k] = A[i]
164+
i--
165+
} else {
166+
A[k] = B[j]
167+
j--
168+
}
169+
}
170+
}
171+
```
172+
117173
### **...**
118174

119175
```

Diff for: lcci/10.01.Sorted Merge/README_EN.md

+58-2
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,36 @@ B = [2,5,6], n = 3
2929
### **Python3**
3030

3131
```python
32-
32+
class Solution:
33+
def merge(self, A: List[int], m: int, B: List[int], n: int) -> None:
34+
"""
35+
Do not return anything, modify A in-place instead.
36+
"""
37+
i, j = m - 1, n - 1
38+
for k in range(len(A) - 1, -1, -1):
39+
if j < 0 or (i >= 0 and A[i] >= B[j]):
40+
A[k] = A[i]
41+
i -= 1
42+
else:
43+
A[k] = B[j]
44+
j -= 1
3345
```
3446

3547
### **Java**
3648

3749
```java
38-
50+
class Solution {
51+
public void merge(int[] A, int m, int[] B, int n) {
52+
int i = m - 1, j = n - 1;
53+
for (int k = A.length - 1; k >= 0; --k) {
54+
if (j < 0 || (i >= 0 && A[i] >= B[j])) {
55+
A[k] = A[i--];
56+
} else {
57+
A[k] = B[j--];
58+
}
59+
}
60+
}
61+
}
3962
```
4063

4164
### **JavaScript**
@@ -107,6 +130,39 @@ impl Solution {
107130
}
108131
```
109132

133+
### **C++**
134+
135+
```cpp
136+
class Solution {
137+
public:
138+
void merge(vector<int>& A, int m, vector<int>& B, int n) {
139+
int i = m - 1, j = n - 1;
140+
for (int k = A.size() - 1; k >= 0; --k)
141+
{
142+
if (j < 0 || (i >= 0 && A[i] >= B[j])) A[k] = A[i--];
143+
else A[k] = B[j--];
144+
}
145+
}
146+
};
147+
```
148+
149+
### **Go**
150+
151+
```go
152+
func merge(A []int, m int, B []int, n int) {
153+
i, j := m-1, n-1
154+
for k := len(A) - 1; k >= 0; k-- {
155+
if j < 0 || (i >= 0 && A[i] >= B[j]) {
156+
A[k] = A[i]
157+
i--
158+
} else {
159+
A[k] = B[j]
160+
j--
161+
}
162+
}
163+
}
164+
```
165+
110166
### **...**
111167

112168
```

Diff for: lcci/10.01.Sorted Merge/Solution.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
void merge(vector<int>& A, int m, vector<int>& B, int n) {
4+
int i = m - 1, j = n - 1;
5+
for (int k = A.size() - 1; k >= 0; --k)
6+
{
7+
if (j < 0 || (i >= 0 && A[i] >= B[j])) A[k] = A[i--];
8+
else A[k] = B[j--];
9+
}
10+
}
11+
};

Diff for: lcci/10.01.Sorted Merge/Solution.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func merge(A []int, m int, B []int, n int) {
2+
i, j := m-1, n-1
3+
for k := len(A) - 1; k >= 0; k-- {
4+
if j < 0 || (i >= 0 && A[i] >= B[j]) {
5+
A[k] = A[i]
6+
i--
7+
} else {
8+
A[k] = B[j]
9+
j--
10+
}
11+
}
12+
}

Diff for: lcci/10.01.Sorted Merge/Solution.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public void merge(int[] A, int m, int[] B, int n) {
3+
int i = m - 1, j = n - 1;
4+
for (int k = A.length - 1; k >= 0; --k) {
5+
if (j < 0 || (i >= 0 && A[i] >= B[j])) {
6+
A[k] = A[i--];
7+
} else {
8+
A[k] = B[j--];
9+
}
10+
}
11+
}
12+
}

Diff for: lcci/10.01.Sorted Merge/Solution.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def merge(self, A: List[int], m: int, B: List[int], n: int) -> None:
3+
"""
4+
Do not return anything, modify A in-place instead.
5+
"""
6+
i, j = m - 1, n - 1
7+
for k in range(len(A) - 1, -1, -1):
8+
if j < 0 or (i >= 0 and A[i] >= B[j]):
9+
A[k] = A[i]
10+
i -= 1
11+
else:
12+
A[k] = B[j]
13+
j -= 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# [2345. Finding the Number of Visible Mountains](https://leetcode.cn/problems/finding-the-number-of-visible-mountains)
2+
3+
[English Version](/solution/2300-2399/2345.Finding%20the%20Number%20of%20Visible%20Mountains/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>You are given a <strong>0-indexed</strong> 2D integer array <code>peaks</code> where <code>peaks[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> states that mountain <code>i</code> has a peak at coordinates <code>(x<sub>i</sub>, y<sub>i</sub>)</code>. A mountain can be described as a right-angled isosceles triangle, with its base along the <code>x</code>-axis and a right angle at its peak. More formally, the <strong>gradients</strong> of ascending and descending the mountain are <code>1</code> and <code>-1</code> respectively.</p>
10+
11+
<p>A mountain is considered <strong>visible</strong> if its peak does not lie within another mountain (including the border of other mountains).</p>
12+
13+
<p>Return <em>the number of visible mountains</em>.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong>Example 1:</strong></p>
17+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2300-2399/2345.Finding%20the%20Number%20of%20Visible%20Mountains/images/ex1.png" style="width: 402px; height: 210px;" />
18+
<pre>
19+
<strong>Input:</strong> peaks = [[2,2],[6,3],[5,4]]
20+
<strong>Output:</strong> 2
21+
<strong>Explanation:</strong> The diagram above shows the mountains.
22+
- Mountain 0 is visible since its peak does not lie within another mountain or its sides.
23+
- Mountain 1 is not visible since its peak lies within the side of mountain 2.
24+
- Mountain 2 is visible since its peak does not lie within another mountain or its sides.
25+
There are 2 mountains that are visible.</pre>
26+
27+
<p><strong>Example 2:</strong></p>
28+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2300-2399/2345.Finding%20the%20Number%20of%20Visible%20Mountains/images/ex2new1.png" style="width: 300px; height: 180px;" />
29+
<pre>
30+
<strong>Input:</strong> peaks = [[1,3],[1,3]]
31+
<strong>Output:</strong> 0
32+
<strong>Explanation:</strong> The diagram above shows the mountains (they completely overlap).
33+
Both mountains are not visible since their peaks lie within each other.
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>1 &lt;= peaks.length &lt;= 10<sup>5</sup></code></li>
41+
<li><code>peaks[i].length == 2</code></li>
42+
<li><code>1 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
43+
</ul>
44+
45+
46+
## 解法
47+
48+
<!-- 这里可写通用的实现逻辑 -->
49+
50+
<!-- tabs:start -->
51+
52+
### **Python3**
53+
54+
<!-- 这里可写当前语言的特殊实现逻辑 -->
55+
56+
```python
57+
58+
```
59+
60+
### **Java**
61+
62+
<!-- 这里可写当前语言的特殊实现逻辑 -->
63+
64+
```java
65+
66+
```
67+
68+
### **TypeScript**
69+
70+
```ts
71+
72+
```
73+
74+
### **...**
75+
76+
```
77+
78+
```
79+
80+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# [2345. Finding the Number of Visible Mountains](https://leetcode.com/problems/finding-the-number-of-visible-mountains)
2+
3+
[中文文档](/solution/2300-2399/2345.Finding%20the%20Number%20of%20Visible%20Mountains/README.md)
4+
5+
## Description
6+
7+
<p>You are given a <strong>0-indexed</strong> 2D integer array <code>peaks</code> where <code>peaks[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> states that mountain <code>i</code> has a peak at coordinates <code>(x<sub>i</sub>, y<sub>i</sub>)</code>. A mountain can be described as a right-angled isosceles triangle, with its base along the <code>x</code>-axis and a right angle at its peak. More formally, the <strong>gradients</strong> of ascending and descending the mountain are <code>1</code> and <code>-1</code> respectively.</p>
8+
9+
<p>A mountain is considered <strong>visible</strong> if its peak does not lie within another mountain (including the border of other mountains).</p>
10+
11+
<p>Return <em>the number of visible mountains</em>.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong>Example 1:</strong></p>
15+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2300-2399/2345.Finding%20the%20Number%20of%20Visible%20Mountains/images/ex1.png" style="width: 402px; height: 210px;" />
16+
<pre>
17+
<strong>Input:</strong> peaks = [[2,2],[6,3],[5,4]]
18+
<strong>Output:</strong> 2
19+
<strong>Explanation:</strong> The diagram above shows the mountains.
20+
- Mountain 0 is visible since its peak does not lie within another mountain or its sides.
21+
- Mountain 1 is not visible since its peak lies within the side of mountain 2.
22+
- Mountain 2 is visible since its peak does not lie within another mountain or its sides.
23+
There are 2 mountains that are visible.</pre>
24+
25+
<p><strong>Example 2:</strong></p>
26+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/2300-2399/2345.Finding%20the%20Number%20of%20Visible%20Mountains/images/ex2new1.png" style="width: 300px; height: 180px;" />
27+
<pre>
28+
<strong>Input:</strong> peaks = [[1,3],[1,3]]
29+
<strong>Output:</strong> 0
30+
<strong>Explanation:</strong> The diagram above shows the mountains (they completely overlap).
31+
Both mountains are not visible since their peaks lie within each other.
32+
</pre>
33+
34+
<p>&nbsp;</p>
35+
<p><strong>Constraints:</strong></p>
36+
37+
<ul>
38+
<li><code>1 &lt;= peaks.length &lt;= 10<sup>5</sup></code></li>
39+
<li><code>peaks[i].length == 2</code></li>
40+
<li><code>1 &lt;= x<sub>i</sub>, y<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
41+
</ul>
42+
43+
44+
## Solutions
45+
46+
<!-- tabs:start -->
47+
48+
### **Python3**
49+
50+
```python
51+
52+
```
53+
54+
### **Java**
55+
56+
```java
57+
58+
```
59+
60+
### **TypeScript**
61+
62+
```ts
63+
64+
```
65+
66+
### **...**
67+
68+
```
69+
70+
```
71+
72+
<!-- tabs:end -->
Loading
Loading

0 commit comments

Comments
 (0)