Movatterモバイル変換


[0]ホーム

URL:


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

Commitf9feb70

Browse files
add 892
1 parentef904be commitf9feb70

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,9 @@ _If you like this project, please leave me a star._ ★
453453
|897|[Increasing Order Search Tree](https://leetcode.com/problems/increasing-order-search-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_897.java) | |Easy| DFS, recursion
454454
|896|[Monotonic Array](https://leetcode.com/problems/monotonic-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_896.java)||Easy|
455455
|895|[Maximum Frequency Stack](https://leetcode.com/problems/maximum-frequency-stack/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_895.java)| HashTable, Stack|Hard|
456-
|890|[Find and Replace Pattern](https://leetcode.com/problems/find-and-replace-pattern/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_890.java)||Medium|
457456
|893|[Groups of Special-Equivalent Strings](https://leetcode.com/problems/groups-of-special-equivalent-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_893.java)|[:tv:](https://youtu.be/tbtXPKkA2Zw)|Easy|
457+
|892|[Surface Area of 3D Shapes](https://leetcode.com/problems/surface-area-of-3d-shapes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_892.java)| Array, Math, Geometry, Matrix|Easy|
458+
|890|[Find and Replace Pattern](https://leetcode.com/problems/find-and-replace-pattern/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_890.java)||Medium|
458459
|888|[Fair Candy Swap](https://leetcode.com/problems/fair-candy-swap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_888.java)||Easy|
459460
|885|[Spiral Matrix III](https://leetcode.com/problems/spiral-matrix-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_885.java)|[:tv:](https://www.youtube.com/watch?v=0qep3f9cqVs)|Medium|
460461
|884|[Uncommon Words from Two Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_884.java)||Easy|
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
packagecom.fishercoder.solutions;
2+
3+
publicclass_892 {
4+
publicstaticclassSolution1 {
5+
/**
6+
* It's the way that you approach a problem like this matters. This is why we practice LeetCode - train your thought process, i.e. how do you approach a seemingly complex problem.
7+
* <p>
8+
* Inspired by: https://leetcode.com/problems/surface-area-of-3d-shapes/discuss/163414/C%2B%2BJava1-line-Python-Minus-Hidden-Area
9+
* <p>
10+
* Idea is:
11+
* 1. Each tower's surface is 4*height + 2, because each tower has 6 surfaces: 4 standing ones and 1 on the top and 1 on the bottom;
12+
* 2. For the adjacent areas between two towers, we can deduct the overlapped area by using the smaller area * 2;
13+
* 3. How to achieve #2, for each tower, we can its against rightside neighbor, and then check against its bottom neighbor, this will cover all.
14+
* Of course, the three for loops could be combined into one, I put it like this for easier understanding.
15+
*/
16+
publicintsurfaceArea(int[][]grid) {
17+
intarea =0;
18+
intm =grid.length;
19+
intn =grid[0].length;
20+
for (inti =0;i <m;i++) {
21+
for (intj =0;j <n;j++) {
22+
if (grid[i][j] !=0) {
23+
area += (grid[i][j] *4 +2);
24+
}
25+
}
26+
}
27+
//check its right side neighbors
28+
for (inti =0;i <m;i++) {
29+
for (intj =0;j <n -1;j++) {
30+
if (grid[i][j] !=0 &&grid[i][j +1] !=0) {
31+
area -=2 *Math.min(grid[i][j],grid[i][j +1]);
32+
}
33+
}
34+
}
35+
//check its downside neighbors
36+
for (inti =0;i <m -1;i++) {
37+
for (intj =0;j <n;j++) {
38+
if (grid[i][j] !=0 &&grid[i +1][j] !=0) {
39+
area -=2 *Math.min(grid[i][j],grid[i +1][j]);
40+
}
41+
}
42+
}
43+
returnarea;
44+
}
45+
}
46+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.utils.CommonUtils;
4+
importcom.fishercoder.solutions._892;
5+
importorg.junit.BeforeClass;
6+
importorg.junit.Test;
7+
8+
importstaticjunit.framework.TestCase.assertEquals;
9+
10+
publicclass_892Test {
11+
privatestatic_892.Solution1solution1;
12+
13+
@BeforeClass
14+
publicstaticvoidsetup() {
15+
solution1 =new_892.Solution1();
16+
}
17+
18+
@Test
19+
publicvoidtest1() {
20+
assertEquals(10,solution1.surfaceArea(newint[][]{{2}}));
21+
}
22+
23+
@Test
24+
publicvoidtest2() {
25+
assertEquals(34,solution1.surfaceArea(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,2],[3,4]")));
26+
}
27+
28+
@Test
29+
publicvoidtest3() {
30+
assertEquals(16,solution1.surfaceArea(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,0],[0,2]")));
31+
}
32+
33+
@Test
34+
publicvoidtest4() {
35+
assertEquals(32,solution1.surfaceArea(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,1,1],[1,0,1],[1,1,1]")));
36+
}
37+
38+
@Test
39+
publicvoidtest5() {
40+
assertEquals(46,solution1.surfaceArea(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[2,2,2],[2,1,2],[2,2,2]")));
41+
}
42+
43+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp