8000 add 892 · Algorithm-box/Leetcode@f9feb70 · GitHub
[go: up one dir, main page]

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

Appearance settings

Commit f9feb70

Browse files
add 892
1 parent ef904be commit f9feb70

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+
package com.fishercoder.solutions;
2+
3+
public class _892 {
4+
public static class Solution1 {
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+
public int surfaceArea(int[][] grid) {
17+
int area = 0;
18+
int m = grid.length;
19+
int n = grid[0].length;
20+
for (int i = 0; i < m; i++) {
21+
for (int j = 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 (int i = 0; i < m; i++) {
29+
for (int j = 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 (int i = 0; i < m - 1; i++) {
37+
for (int j = 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+
return area;
44+
}
45+
}
46+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._892;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static junit.framework.TestCase.assertEquals;
9+
10+
public class _892Test {
11+
private static _892.Solution1 solution1;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _892.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
assertEquals(10, solution1.surfaceArea(new int[][]{{2}}));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
assertEquals(34, solution1.surfaceArea(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,2],[3,4]")));
26+
}
27+
28+
@Test
29+
public void test3() {
30+
assertEquals(16, solution1.surfaceArea(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,0],[0,2]")));
31+
}
32+
33+
@Test
34+
public void test4() {
35+
assertEquals(32, solution1.surfaceArea(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,1,1],[1,0,1],[1,1,1]")));
36+
}
37+
38+
@Test
39+
public void test5() {
40+
assertEquals(46, solution1.surfaceArea(CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[2,2,2],[2,1,2],[2,2,2]")));
41+
}
42+
43+
}

0 commit comments

Comments
 (0)
0