From 0ae330def50bb5b427d3b911834f76e34b1fa234 Mon Sep 17 00:00:00 2001 From: yanglbme <szuyanglb@outlook.com> Date: Mon, 19 Feb 2024 17:32:56 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.0881~0884 --- .../0881.Boats to Save People/README.md | 16 ++++- .../0881.Boats to Save People/README_EN.md | 20 +++++- .../0881.Boats to Save People/Solution.ts | 11 +++ .../README.md | 70 +++++++++++-------- .../README_EN.md | 70 +++++++++++-------- .../Solution.rs | 40 ++++++----- .../Solution.ts | 18 ++--- .../README.md | 4 +- .../README_EN.md | 12 +++- .../Solution.java | 4 +- 10 files changed, 170 insertions(+), 95 deletions(-) create mode 100644 solution/0800-0899/0881.Boats to Save People/Solution.ts diff --git a/solution/0800-0899/0881.Boats to Save People/README.md b/solution/0800-0899/0881.Boats to Save People/README.md index 0a4ba7da430da..63faf0675a07c 100644 --- a/solution/0800-0899/0881.Boats to Save People/README.md +++ b/solution/0800-0899/0881.Boats to Save People/README.md @@ -52,7 +52,7 @@ 排序后,使用双指针分别指向数组首尾,每次取两个指针指向的元素之和与 `limit` 比较,如果小于等于 `limit`,则两个指针同时向中间移动一位,否则只移动右指针。累加答案即可。 -时间复杂度 $O(n\log n)$,其中 $n$ 为数组 `people` 的长度。 +时间复杂度 $O(n \times \log n)$,空间复杂度 $O(\log n)$。其中 $n$ 为数组 `people` 的长度。 <!-- tabs:start --> @@ -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 --> diff --git a/solution/0800-0899/0881.Boats to Save People/README_EN.md b/solution/0800-0899/0881.Boats to Save People/README_EN.md index 8f6f8ace884bf..afa97e49f0c9d 100644 --- a/solution/0800-0899/0881.Boats to Save People/README_EN.md +++ b/solution/0800-0899/0881.Boats to Save People/README_EN.md @@ -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 --> @@ -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 --> diff --git a/solution/0800-0899/0881.Boats to Save People/Solution.ts b/solution/0800-0899/0881.Boats to Save People/Solution.ts new file mode 100644 index 0000000000000..73edaa873bf5e --- /dev/null +++ b/solution/0800-0899/0881.Boats to Save People/Solution.ts @@ -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; +} diff --git a/solution/0800-0899/0883.Projection Area of 3D Shapes/README.md b/solution/0800-0899/0883.Projection Area of 3D Shapes/README.md index 710b7b5e10a8a..ecd36c561ee1f 100644 --- a/solution/0800-0899/0883.Projection Area of 3D Shapes/README.md +++ b/solution/0800-0899/0883.Projection Area of 3D Shapes/README.md @@ -66,7 +66,17 @@ ## 解法 -### 方法一 +### 方法一:数学 + +我们可以分别计算三个投影的面积。 + +- xy 平面的投影面积:每个非零值都会投影到 xy 平面,所以 xy 的投影面积为非零值的个数。 +- yz 平面的投影面积:每一行的最大值。 +- zx 平面的投影面积:每一列的最大值。 + +最后将三个面积相加即可。 + +时间复杂度 $O(n^2)$,其中 $n$ 为网格 `grid` 的边长。空间复杂度 $O(1)$。 <!-- tabs:start --> @@ -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 } } ``` diff --git a/solution/0800-0899/0883.Projection Area of 3D Shapes/README_EN.md b/solution/0800-0899/0883.Projection Area of 3D Shapes/README_EN.md index 4483e8958e635..f8f16ae6cf931 100644 --- a/solution/0800-0899/0883.Projection Area of 3D Shapes/README_EN.md +++ b/solution/0800-0899/0883.Projection Area of 3D Shapes/README_EN.md @@ -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 --> @@ -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 } } ``` diff --git a/solution/0800-0899/0883.Projection Area of 3D Shapes/Solution.rs b/solution/0800-0899/0883.Projection Area of 3D Shapes/Solution.rs index c70bf7d1f3daa..33848f581c021 100644 --- a/solution/0800-0899/0883.Projection Area of 3D Shapes/Solution.rs +++ b/solution/0800-0899/0883.Projection Area of 3D Shapes/Solution.rs @@ -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 } } diff --git a/solution/0800-0899/0883.Projection Area of 3D Shapes/Solution.ts b/solution/0800-0899/0883.Projection Area of 3D Shapes/Solution.ts index 46d64d20472b9..3fe97a2ca6cff 100644 --- a/solution/0800-0899/0883.Projection Area of 3D Shapes/Solution.ts +++ b/solution/0800-0899/0883.Projection Area of 3D Shapes/Solution.ts @@ -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; } diff --git a/solution/0800-0899/0884.Uncommon Words from Two Sentences/README.md b/solution/0800-0899/0884.Uncommon Words from Two Sentences/README.md index 5ff8e7b03fa81..62ccaefba464b 100644 --- a/solution/0800-0899/0884.Uncommon Words from Two Sentences/README.md +++ b/solution/0800-0899/0884.Uncommon Words from Two Sentences/README.md @@ -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()) { diff --git a/solution/0800-0899/0884.Uncommon Words from Two Sentences/README_EN.md b/solution/0800-0899/0884.Uncommon Words from Two Sentences/README_EN.md index 8b098a0bb6408..4aa2e2b54baf5 100644 --- a/solution/0800-0899/0884.Uncommon Words from Two Sentences/README_EN.md +++ b/solution/0800-0899/0884.Uncommon Words from Two Sentences/README_EN.md @@ -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 --> @@ -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()) { diff --git a/solution/0800-0899/0884.Uncommon Words from Two Sentences/Solution.java b/solution/0800-0899/0884.Uncommon Words from Two Sentences/Solution.java index 61f42ee5558d1..9d8efba2dfe36 100644 --- a/solution/0800-0899/0884.Uncommon Words from Two Sentences/Solution.java +++ b/solution/0800-0899/0884.Uncommon Words from Two Sentences/Solution.java @@ -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()) {