Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 29522a2

Browse files
✨feat: Add 883
1 parent c4b947e commit 29522a2

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
| [859. 亲密字符串](https://leetcode-cn.com/problems/buddy-strings/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/buddy-strings/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-q056/) | 简单 | 🤩🤩🤩🤩🤩 |
9898
| [867. 转置矩阵](https://leetcode-cn.com/problems/transpose-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/transpose-matrix/solution/yi-you-wei-jin-huo-xu-ni-neng-kan-kan-zh-m53m/) | 简单 | 🤩🤩🤩🤩 |
9999
| [868. 二进制间距](https://leetcode-cn.com/problems/binary-gap/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-gap/solution/by-ac_oier-2sui/) | 简单 | 🤩🤩🤩🤩 |
100+
| [883. 三维形体投影面积](https://leetcode-cn.com/problems/projection-area-of-3d-shapes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/projection-area-of-3d-shapes/solution/by-ac_oier-r6hj/) | 简单 | 🤩🤩🤩🤩 |
100101
| [884. 两句话中的不常见单词](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/solution/gong-shui-san-xie-shu-ju-jie-gou-mo-ni-t-wwam/) | 简单 | 🤩🤩🤩🤩 |
101102
| [896. 单调数列](https://leetcode-cn.com/problems/monotonic-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/monotonic-array/solution/wei-shi-yao-yi-ci-bian-li-yao-bi-liang-c-uglp/) | 简单 | 🤩🤩🤩🤩 |
102103
| [997. 找到小镇的法官](https://leetcode-cn.com/problems/find-the-town-judge/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-the-town-judge/solution/gong-shui-san-xie-jian-dan-chu-du-ru-du-5ms57/) | 简单 | 🤩🤩🤩🤩 |
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[883. 三维形体投影面积](https://leetcode-cn.com/problems/projection-area-of-3d-shapes/solution/by-ac_oier-r6hj/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
在 $n * n$ 的网格 `grid` 中,我们放置了一些与 `x`,`y`,`z` 三轴对齐的 1ドル * 1 * 1$ 立方体。
10+
11+
每个值 $v = grid[i][j]$ 表示 $v$ 个正方体叠放在单元格 $(i, j)$ 上。
12+
13+
现在,我们查看这些立方体在 `xy``yz``zx` 平面上的投影。
14+
15+
投影 就像影子,将 三维 形体映射到一个 二维 平面上。从顶部、前面和侧面看立方体时,我们会看到"影子"。
16+
17+
返回 所有三个投影的总面积 。
18+
19+
示例 1:
20+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/08/02/shadow.png)
21+
```
22+
输入:[[1,2],[3,4]]
23+
24+
输出:17
25+
26+
解释:这里有该形体在三个轴对齐平面上的三个投影("阴影部分")。
27+
```
28+
示例 2:
29+
```
30+
输入:grid = [[2]]
31+
32+
输出:5
33+
```
34+
示例 3:
35+
```
36+
输入:[[1,0],[0,2]]
37+
38+
输出:8
39+
```
40+
41+
提示:
42+
* $n == grid.length == grid[i].length$
43+
* 1ドル <= n <= 50$
44+
* 0ドル <= grid[i][j] <= 50$
45+
46+
---
47+
48+
### 模拟
49+
50+
根据题意进行模拟即可,使用三个变量分别统计三视图的阴影面积:
51+
52+
* `ans1`:统计俯视图的面积,共有 $n * n$ 个位置需要被统计,当任意格子 $g[i][j] > 0,ドル阴影面积加一;
53+
* `ans2`:统计左视图的面积,共有 $n$ 行需要被统计,每一行对 `ans2` 的贡献为该行的最大高度;
54+
* `ans3`:统计主视图的面积,共有 $n$ 列需要被统计,每一列对 `ans3` 的贡献为该列的最大高度。
55+
56+
代码:
57+
```Java
58+
class Solution {
59+
public int projectionArea(int[][] g) {
60+
int ans1 = 0, ans2 = 0, ans3 = 0;
61+
int n = g.length;
62+
for (int i = 0; i < n; i++) {
63+
int a = 0, b = 0;
64+
for (int j = 0; j < n; j++) {
65+
if (g[i][j] > 0) ans1++;
66+
a = Math.max(a, g[i][j]);
67+
b = Math.max(b, g[j][i]);
68+
}
69+
ans2 += a; ans3 += b;
70+
}
71+
return ans1 + ans2 + ans3;
72+
}
73+
}
74+
```
75+
* 时间复杂度:$O(n^2)$
76+
* 空间复杂度:$O(1)$
77+
78+
---
79+
80+
### 最后
81+
82+
这是我们「刷穿 LeetCode」系列文章的第 `No.883` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
83+
84+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
85+
86+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
87+
88+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
89+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /