Skip to content

Commit 68a50ae

Browse files
authored
feat: add solutions to lc problems: No.0881~0884 (doocs#2356)
1 parent 4e34407 commit 68a50ae

File tree

10 files changed

+170
-95
lines changed

10 files changed

+170
-95
lines changed

solution/0800-0899/0881.Boats to Save People/README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
排序后,使用双指针分别指向数组首尾,每次取两个指针指向的元素之和与 `limit` 比较,如果小于等于 `limit`,则两个指针同时向中间移动一位,否则只移动右指针。累加答案即可。
5454

55-
时间复杂度 $O(n\log n)$,其中 $n$ 为数组 `people` 的长度。
55+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 `people` 的长度。
5656

5757
<!-- tabs:start -->
5858

@@ -117,6 +117,20 @@ func numRescueBoats(people []int, limit int) int {
117117
}
118118
```
119119

120+
```ts
121+
function numRescueBoats(people: number[], limit: number): number {
122+
people.sort((a, b) => a - b);
123+
let ans = 0;
124+
for (let i = 0, j = people.length - 1; i <= j; --j) {
125+
if (people[i] + people[j] <= limit) {
126+
++i;
127+
}
128+
++ans;
129+
}
130+
return ans;
131+
}
132+
```
133+
120134
<!-- tabs:end -->
121135

122136
<!-- end -->

solution/0800-0899/0881.Boats to Save People/README_EN.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@
4343

4444
## Solutions
4545

46-
### Solution 1
46+
### Solution 1: Greedy + Two Pointers
47+
48+
After sorting, use two pointers to point to the beginning and end of the array respectively. Each time, compare the sum of the elements pointed to by the two pointers with `limit`. If it is less than or equal to `limit`, then both pointers move one step towards the middle. Otherwise, only the right pointer moves. Accumulate the answer.
49+
50+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the length of the array `people`.
4751

4852
<!-- tabs:start -->
4953

@@ -108,6 +112,20 @@ func numRescueBoats(people []int, limit int) int {
108112
}
109113
```
110114

115+
```ts
116+
function numRescueBoats(people: number[], limit: number): number {
117+
people.sort((a, b) => a - b);
118+
let ans = 0;
119+
for (let i = 0, j = people.length - 1; i <= j; --j) {
120+
if (people[i] + people[j] <= limit) {
121+
++i;
122+
}
123+
++ans;
124+
}
125+
return ans;
126+
}
127+
```
128+
111129
<!-- tabs:end -->
112130

113131
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function numRescueBoats(people: number[], limit: number): number {
2+
people.sort((a, b) => a - b);
3+
let ans = 0;
4+
for (let i = 0, j = people.length - 1; i <= j; --j) {
5+
if (people[i] + people[j] <= limit) {
6+
++i;
7+
}
8+
++ans;
9+
}
10+
return ans;
11+
}

solution/0800-0899/0883.Projection Area of 3D Shapes/README.md

+41-29
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,17 @@
6666

6767
## 解法
6868

69-
### 方法一
69+
### 方法一:数学
70+
71+
我们可以分别计算三个投影的面积。
72+
73+
- xy 平面的投影面积:每个非零值都会投影到 xy 平面,所以 xy 的投影面积为非零值的个数。
74+
- yz 平面的投影面积:每一行的最大值。
75+
- zx 平面的投影面积:每一列的最大值。
76+
77+
最后将三个面积相加即可。
78+
79+
时间复杂度 $O(n^2)$,其中 $n$ 为网格 `grid` 的边长。空间复杂度 $O(1)$。
7080

7181
<!-- tabs:start -->
7282

@@ -142,40 +152,42 @@ func projectionArea(grid [][]int) int {
142152

143153
```ts
144154
function projectionArea(grid: number[][]): number {
145-
const n = grid.length;
146-
let res = grid.reduce((r, v) => r + v.reduce((r, v) => r + (v === 0 ? 0 : 1), 0), 0);
147-
for (let i = 0; i < n; i++) {
148-
let xMax = 0;
149-
let yMax = 0;
150-
for (let j = 0; j < n; j++) {
151-
xMax = Math.max(xMax, grid[i][j]);
152-
yMax = Math.max(yMax, grid[j][i]);
153-
}
154-
res += xMax + yMax;
155-
}
156-
return res;
155+
const xy: number = grid.flat().filter(v => v > 0).length;
156+
const yz: number = grid.reduce((acc, row) => acc + Math.max(...row), 0);
157+
const zx: number = grid[0]
158+
.map((_, i) => Math.max(...grid.map(row => row[i])))
159+
.reduce((acc, val) => acc + val, 0);
160+
return xy + yz + zx;
157161
}
158162
```
159163

160164
```rust
161165
impl Solution {
162166
pub fn projection_area(grid: Vec<Vec<i32>>) -> i32 {
163-
let n = grid.len();
164-
let mut res = 0;
165-
let mut x_max = vec![0; n];
166-
let mut y_max = vec![0; n];
167-
for i in 0..n {
168-
for j in 0..n {
169-
let val = grid[i][j];
170-
if val == 0 {
171-
continue;
172-
}
173-
res += 1;
174-
x_max[i] = x_max[i].max(val);
175-
y_max[j] = y_max[j].max(val);
176-
}
177-
}
178-
res + y_max.iter().sum::<i32>() + x_max.iter().sum::<i32>()
167+
let xy: i32 = grid
168+
.iter()
169+
.map(
170+
|row|
171+
row
172+
.iter()
173+
.filter(|&&v| v > 0)
174+
.count() as i32
175+
)
176+
.sum();
177+
let yz: i32 = grid
178+
.iter()
179+
.map(|row| *row.iter().max().unwrap_or(&0))
180+
.sum();
181+
let zx: i32 = (0..grid[0].len())
182+
.map(|i|
183+
grid
184+
.iter()
185+
.map(|row| row[i])
186+
.max()
187+
.unwrap_or(0)
188+
)
189+
.sum();
190+
xy + yz + zx
179191
}
180192
}
181193
```

solution/0800-0899/0883.Projection Area of 3D Shapes/README_EN.md

+41-29
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,17 @@
4848

4949
## Solutions
5050

51-
### Solution 1
51+
### Solution 1: Mathematics
52+
53+
We can calculate the area of the three projections separately.
54+
55+
- Projection area on the xy plane: Each non-zero value will be projected onto the xy plane, so the projection area on the xy plane is the count of non-zero values.
56+
- Projection area on the yz plane: The maximum value in each row.
57+
- Projection area on the zx plane: The maximum value in each column.
58+
59+
Finally, add up the three areas.
60+
61+
The time complexity is $O(n^2)$, where $n$ is the side length of the grid `grid`. The space complexity is $O(1)$.
5262

5363
<!-- tabs:start -->
5464

@@ -124,40 +134,42 @@ func projectionArea(grid [][]int) int {
124134

125135
```ts
126136
function projectionArea(grid: number[][]): number {
127-
const n = grid.length;
128-
let res = grid.reduce((r, v) => r + v.reduce((r, v) => r + (v === 0 ? 0 : 1), 0), 0);
129-
for (let i = 0; i < n; i++) {
130-
let xMax = 0;
131-
let yMax = 0;
132-
for (let j = 0; j < n; j++) {
133-
xMax = Math.max(xMax, grid[i][j]);
134-
yMax = Math.max(yMax, grid[j][i]);
135-
}
136-
res += xMax + yMax;
137-
}
138-
return res;
137+
const xy: number = grid.flat().filter(v => v > 0).length;
138+
const yz: number = grid.reduce((acc, row) => acc + Math.max(...row), 0);
139+
const zx: number = grid[0]
140+
.map((_, i) => Math.max(...grid.map(row => row[i])))
141+
.reduce((acc, val) => acc + val, 0);
142+
return xy + yz + zx;
139143
}
140144
```
141145

142146
```rust
143147
impl Solution {
144148
pub fn projection_area(grid: Vec<Vec<i32>>) -> i32 {
145-
let n = grid.len();
146-
let mut res = 0;
147-
let mut x_max = vec![0; n];
148-
let mut y_max = vec![0; n];
149-
for i in 0..n {
150-
for j in 0..n {
151-
let val = grid[i][j];
152-
if val == 0 {
153-
continue;
154-
}
155-
res += 1;
156-
x_max[i] = x_max[i].max(val);
157-
y_max[j] = y_max[j].max(val);
158-
}
159-
}
160-
res + y_max.iter().sum::<i32>() + x_max.iter().sum::<i32>()
149+
let xy: i32 = grid
150+
.iter()
151+
.map(
152+
|row|
153+
row
154+
.iter()
155+
.filter(|&&v| v > 0)
156+
.count() as i32
157+
)
158+
.sum();
159+
let yz: i32 = grid
160+
.iter()
161+
.map(|row| *row.iter().max().unwrap_or(&0))
162+
.sum();
163+
let zx: i32 = (0..grid[0].len())
164+
.map(|i|
165+
grid
166+
.iter()
167+
.map(|row| row[i])
168+
.max()
169+
.unwrap_or(0)
170+
)
171+
.sum();
172+
xy + yz + zx
161173
}
162174
}
163175
```
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
impl Solution {
22
pub fn projection_area(grid: Vec<Vec<i32>>) -> i32 {
3-
let n = grid.len();
4-
let mut res = 0;
5-
let mut x_max = vec![0; n];
6-
let mut y_max = vec![0; n];
7-
for i in 0..n {
8-
for j in 0..n {
9-
let val = grid[i][j];
10-
if val == 0 {
11-
continue;
12-
}
13-
res += 1;
14-
x_max[i] = x_max[i].max(val);
15-
y_max[j] = y_max[j].max(val);
16-
}
17-
}
18-
res + y_max.iter().sum::<i32>() + x_max.iter().sum::<i32>()
3+
let xy: i32 = grid
4+
.iter()
5+
.map(
6+
|row|
7+
row
8+
.iter()
9+
.filter(|&&v| v > 0)
10+
.count() as i32
11+
)
12+
.sum();
13+
let yz: i32 = grid
14+
.iter()
15+
.map(|row| *row.iter().max().unwrap_or(&0))
16+
.sum();
17+
let zx: i32 = (0..grid[0].len())
18+
.map(|i|
19+
grid
20+
.iter()
21+
.map(|row| row[i])
22+
.max()
23+
.unwrap_or(0)
24+
)
25+
.sum();
26+
xy + yz + zx
1927
}
2028
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
function projectionArea(grid: number[][]): number {
2-
const n = grid.length;
3-
let res = grid.reduce((r, v) => r + v.reduce((r, v) => r + (v === 0 ? 0 : 1), 0), 0);
4-
for (let i = 0; i < n; i++) {
5-
let xMax = 0;
6-
let yMax = 0;
7-
for (let j = 0; j < n; j++) {
8-
xMax = Math.max(xMax, grid[i][j]);
9-
yMax = Math.max(yMax, grid[j][i]);
10-
}
11-
res += xMax + yMax;
12-
}
13-
return res;
2+
const xy: number = grid.flat().filter(v => v > 0).length;
3+
const yz: number = grid.reduce((acc, row) => acc + Math.max(...row), 0);
4+
const zx: number = grid[0]
5+
.map((_, i) => Math.max(...grid.map(row => row[i])))
6+
.reduce((acc, val) => acc + val, 0);
7+
return xy + yz + zx;
148
}

solution/0800-0899/0884.Uncommon Words from Two Sentences/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ class Solution {
6666
public String[] uncommonFromSentences(String s1, String s2) {
6767
Map<String, Integer> cnt = new HashMap<>();
6868
for (String s : s1.split(" ")) {
69-
cnt.put(s, cnt.getOrDefault(s, 0) + 1);
69+
cnt.merge(s, 1, Integer::sum);
7070
}
7171
for (String s : s2.split(" ")) {
72-
cnt.put(s, cnt.getOrDefault(s, 0) + 1);
72+
cnt.merge(s, 1, Integer::sum);
7373
}
7474
List<String> ans = new ArrayList<>();
7575
for (var e : cnt.entrySet()) {

solution/0800-0899/0884.Uncommon Words from Two Sentences/README_EN.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@
3030

3131
## Solutions
3232

33-
### Solution 1
33+
### Solution 1: Hash Table
34+
35+
According to the problem description, as long as a word appears once, it meets the requirements of the problem. Therefore, we use a hash table `cnt` to record all words and their occurrence counts.
36+
37+
Then we traverse the hash table, and take out all strings that appear only once.
38+
39+
The time complexity is $O(m + n)$, and the space complexity is $O(m + n)$. Here, $m$ and $n$ are the lengths of strings `s1` and `s2`, respectively.
3440

3541
<!-- tabs:start -->
3642

@@ -46,10 +52,10 @@ class Solution {
4652
public String[] uncommonFromSentences(String s1, String s2) {
4753
Map<String, Integer> cnt = new HashMap<>();
4854
for (String s : s1.split(" ")) {
49-
cnt.put(s, cnt.getOrDefault(s, 0) + 1);
55+
cnt.merge(s, 1, Integer::sum);
5056
}
5157
for (String s : s2.split(" ")) {
52-
cnt.put(s, cnt.getOrDefault(s, 0) + 1);
58+
cnt.merge(s, 1, Integer::sum);
5359
}
5460
List<String> ans = new ArrayList<>();
5561
for (var e : cnt.entrySet()) {

solution/0800-0899/0884.Uncommon Words from Two Sentences/Solution.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ class Solution {
22
public String[] uncommonFromSentences(String s1, String s2) {
33
Map<String, Integer> cnt = new HashMap<>();
44
for (String s : s1.split(" ")) {
5-
cnt.put(s, cnt.getOrDefault(s, 0) + 1);
5+
cnt.merge(s, 1, Integer::sum);
66
}
77
for (String s : s2.split(" ")) {
8-
cnt.put(s, cnt.getOrDefault(s, 0) + 1);
8+
cnt.merge(s, 1, Integer::sum);
99
}
1010
List<String> ans = new ArrayList<>();
1111
for (var e : cnt.entrySet()) {

0 commit comments

Comments
 (0)