Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problems: No.0881~0884 #2356

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion solution/0800-0899/0881.Boats to Save People/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

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

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

<!-- tabs:start -->

Expand Down Expand Up @@ -117,6 +117,20 @@ func numRescueBoats(people []int, limit int) int {
}
```

```ts
function numRescueBoats(people: number[], limit: number): number {
people.sort((a, b) => a - b);
let ans = 0;
for (let i = 0, j = people.length - 1; i <= j; --j) {
if (people[i] + people[j] <= limit) {
++i;
}
++ans;
}
return ans;
}
```

<!-- tabs:end -->

<!-- end -->
20 changes: 19 additions & 1 deletion solution/0800-0899/0881.Boats to Save People/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@

## Solutions

### Solution 1
### Solution 1: Greedy + Two Pointers

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.

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`.

<!-- tabs:start -->

Expand Down Expand Up @@ -108,6 +112,20 @@ func numRescueBoats(people []int, limit int) int {
}
```

```ts
function numRescueBoats(people: number[], limit: number): number {
people.sort((a, b) => a - b);
let ans = 0;
for (let i = 0, j = people.length - 1; i <= j; --j) {
if (people[i] + people[j] <= limit) {
++i;
}
++ans;
}
return ans;
}
```

<!-- tabs:end -->

<!-- end -->
11 changes: 11 additions & 0 deletions solution/0800-0899/0881.Boats to Save People/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function numRescueBoats(people: number[], limit: number): number {
people.sort((a, b) => a - b);
let ans = 0;
for (let i = 0, j = people.length - 1; i <= j; --j) {
if (people[i] + people[j] <= limit) {
++i;
}
++ans;
}
return ans;
}
70 changes: 41 additions & 29 deletions solution/0800-0899/0883.Projection Area of 3D Shapes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,17 @@

## 解法

### 方法一
### 方法一:数学

我们可以分别计算三个投影的面积。

- xy 平面的投影面积:每个非零值都会投影到 xy 平面,所以 xy 的投影面积为非零值的个数。
- yz 平面的投影面积:每一行的最大值。
- zx 平面的投影面积:每一列的最大值。

最后将三个面积相加即可。

时间复杂度 $O(n^2)$,其中 $n$ 为网格 `grid` 的边长。空间复杂度 $O(1)$。

<!-- tabs:start -->

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

```ts
function projectionArea(grid: number[][]): number {
const n = grid.length;
let res = grid.reduce((r, v) => r + v.reduce((r, v) => r + (v === 0 ? 0 : 1), 0), 0);
for (let i = 0; i < n; i++) {
let xMax = 0;
let yMax = 0;
for (let j = 0; j < n; j++) {
xMax = Math.max(xMax, grid[i][j]);
yMax = Math.max(yMax, grid[j][i]);
}
res += xMax + yMax;
}
return res;
const xy: number = grid.flat().filter(v => v > 0).length;
const yz: number = grid.reduce((acc, row) => acc + Math.max(...row), 0);
const zx: number = grid[0]
.map((_, i) => Math.max(...grid.map(row => row[i])))
.reduce((acc, val) => acc + val, 0);
return xy + yz + zx;
}
```

```rust
impl Solution {
pub fn projection_area(grid: Vec<Vec<i32>>) -> i32 {
let n = grid.len();
let mut res = 0;
let mut x_max = vec![0; n];
let mut y_max = vec![0; n];
for i in 0..n {
for j in 0..n {
let val = grid[i][j];
if val == 0 {
continue;
}
res += 1;
x_max[i] = x_max[i].max(val);
y_max[j] = y_max[j].max(val);
}
}
res + y_max.iter().sum::<i32>() + x_max.iter().sum::<i32>()
let xy: i32 = grid
.iter()
.map(
|row|
row
.iter()
.filter(|&&v| v > 0)
.count() as i32
)
.sum();
let yz: i32 = grid
.iter()
.map(|row| *row.iter().max().unwrap_or(&0))
.sum();
let zx: i32 = (0..grid[0].len())
.map(|i|
grid
.iter()
.map(|row| row[i])
.max()
.unwrap_or(0)
)
.sum();
xy + yz + zx
}
}
```
Expand Down
70 changes: 41 additions & 29 deletions solution/0800-0899/0883.Projection Area of 3D Shapes/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,17 @@

## Solutions

### Solution 1
### Solution 1: Mathematics

We can calculate the area of the three projections separately.

- 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.
- Projection area on the yz plane: The maximum value in each row.
- Projection area on the zx plane: The maximum value in each column.

Finally, add up the three areas.

The time complexity is $O(n^2)$, where $n$ is the side length of the grid `grid`. The space complexity is $O(1)$.

<!-- tabs:start -->

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

```ts
function projectionArea(grid: number[][]): number {
const n = grid.length;
let res = grid.reduce((r, v) => r + v.reduce((r, v) => r + (v === 0 ? 0 : 1), 0), 0);
for (let i = 0; i < n; i++) {
let xMax = 0;
let yMax = 0;
for (let j = 0; j < n; j++) {
xMax = Math.max(xMax, grid[i][j]);
yMax = Math.max(yMax, grid[j][i]);
}
res += xMax + yMax;
}
return res;
const xy: number = grid.flat().filter(v => v > 0).length;
const yz: number = grid.reduce((acc, row) => acc + Math.max(...row), 0);
const zx: number = grid[0]
.map((_, i) => Math.max(...grid.map(row => row[i])))
.reduce((acc, val) => acc + val, 0);
return xy + yz + zx;
}
```

```rust
impl Solution {
pub fn projection_area(grid: Vec<Vec<i32>>) -> i32 {
let n = grid.len();
let mut res = 0;
let mut x_max = vec![0; n];
let mut y_max = vec![0; n];
for i in 0..n {
for j in 0..n {
let val = grid[i][j];
if val == 0 {
continue;
}
res += 1;
x_max[i] = x_max[i].max(val);
y_max[j] = y_max[j].max(val);
}
}
res + y_max.iter().sum::<i32>() + x_max.iter().sum::<i32>()
let xy: i32 = grid
.iter()
.map(
|row|
row
.iter()
.filter(|&&v| v > 0)
.count() as i32
)
.sum();
let yz: i32 = grid
.iter()
.map(|row| *row.iter().max().unwrap_or(&0))
.sum();
let zx: i32 = (0..grid[0].len())
.map(|i|
grid
.iter()
.map(|row| row[i])
.max()
.unwrap_or(0)
)
.sum();
xy + yz + zx
}
}
```
Expand Down
40 changes: 24 additions & 16 deletions solution/0800-0899/0883.Projection Area of 3D Shapes/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
impl Solution {
pub fn projection_area(grid: Vec<Vec<i32>>) -> i32 {
let n = grid.len();
let mut res = 0;
let mut x_max = vec![0; n];
let mut y_max = vec![0; n];
for i in 0..n {
for j in 0..n {
let val = grid[i][j];
if val == 0 {
continue;
}
res += 1;
x_max[i] = x_max[i].max(val);
y_max[j] = y_max[j].max(val);
}
}
res + y_max.iter().sum::<i32>() + x_max.iter().sum::<i32>()
let xy: i32 = grid
.iter()
.map(
|row|
row
.iter()
.filter(|&&v| v > 0)
.count() as i32
)
.sum();
let yz: i32 = grid
.iter()
.map(|row| *row.iter().max().unwrap_or(&0))
.sum();
let zx: i32 = (0..grid[0].len())
.map(|i|
grid
.iter()
.map(|row| row[i])
.max()
.unwrap_or(0)
)
.sum();
xy + yz + zx
}
}
18 changes: 6 additions & 12 deletions solution/0800-0899/0883.Projection Area of 3D Shapes/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
function projectionArea(grid: number[][]): number {
const n = grid.length;
let res = grid.reduce((r, v) => r + v.reduce((r, v) => r + (v === 0 ? 0 : 1), 0), 0);
for (let i = 0; i < n; i++) {
let xMax = 0;
let yMax = 0;
for (let j = 0; j < n; j++) {
xMax = Math.max(xMax, grid[i][j]);
yMax = Math.max(yMax, grid[j][i]);
}
res += xMax + yMax;
}
return res;
const xy: number = grid.flat().filter(v => v > 0).length;
const yz: number = grid.reduce((acc, row) => acc + Math.max(...row), 0);
const zx: number = grid[0]
.map((_, i) => Math.max(...grid.map(row => row[i])))
.reduce((acc, val) => acc + val, 0);
return xy + yz + zx;
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ class Solution {
public String[] uncommonFromSentences(String s1, String s2) {
Map<String, Integer> cnt = new HashMap<>();
for (String s : s1.split(" ")) {
cnt.put(s, cnt.getOrDefault(s, 0) + 1);
cnt.merge(s, 1, Integer::sum);
}
for (String s : s2.split(" ")) {
cnt.put(s, cnt.getOrDefault(s, 0) + 1);
cnt.merge(s, 1, Integer::sum);
}
List<String> ans = new ArrayList<>();
for (var e : cnt.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@

## Solutions

### Solution 1
### Solution 1: Hash Table

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.

Then we traverse the hash table, and take out all strings that appear only once.

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.

<!-- tabs:start -->

Expand All @@ -46,10 +52,10 @@ class Solution {
public String[] uncommonFromSentences(String s1, String s2) {
Map<String, Integer> cnt = new HashMap<>();
for (String s : s1.split(" ")) {
cnt.put(s, cnt.getOrDefault(s, 0) + 1);
cnt.merge(s, 1, Integer::sum);
}
for (String s : s2.split(" ")) {
cnt.put(s, cnt.getOrDefault(s, 0) + 1);
cnt.merge(s, 1, Integer::sum);
}
List<String> ans = new ArrayList<>();
for (var e : cnt.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ class Solution {
public String[] uncommonFromSentences(String s1, String s2) {
Map<String, Integer> cnt = new HashMap<>();
for (String s : s1.split(" ")) {
cnt.put(s, cnt.getOrDefault(s, 0) + 1);
cnt.merge(s, 1, Integer::sum);
}
for (String s : s2.split(" ")) {
cnt.put(s, cnt.getOrDefault(s, 0) + 1);
cnt.merge(s, 1, Integer::sum);
}
List<String> ans = new ArrayList<>();
for (var e : cnt.entrySet()) {
Expand Down
Loading