Skip to content

Commit aa68665

Browse files
author
Prashant Jain
committed
Added array problems
1 parent 7b9e417 commit aa68665

3 files changed

Lines changed: 180 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ hs_err_pid*
2626
*.iml
2727
build/
2828
out/
29+
.DS_Store
30+
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package in.knowledgegate.dsa.array;
2+
3+
/**
4+
* You are given an n x n grid where you have
5+
* placed some 1 x 1 x 1 cubes. Each value v = grid[i][j]
6+
* represents a tower of v cubes placed on top
7+
* of cell (i, j).
8+
*
9+
* After placing these cubes, you have decided to
10+
* glue any directly adjacent cubes to each other,
11+
* forming several irregular 3D shapes.
12+
*
13+
* Return the total surface area of the resulting
14+
* shapes.
15+
*
16+
* Note: The bottom face of each shape counts
17+
* toward its surface area.
18+
*
19+
* Example 1:
20+
* Input: grid = [[1,2],
21+
* [3,4]]
22+
* Output: 34 = 4 + 7 + 10 + 13 = 34
23+
*
24+
* Example 2:
25+
* Input: grid = [[1,1,1],
26+
* [1,0,1],
27+
* [1,1,1]]
28+
* Output: 32
29+
*
30+
* Example 3:
31+
* Input: grid = [[2,2,2],[2,1,2],[2,2,2]]
32+
* Output: 46
33+
*
34+
* Constraints:
35+
* n == grid.length == grid[i].length
36+
* 1 <= n <= 50
37+
* 0 <= grid[i][j] <= 50
38+
*/
39+
public class SurfaceAreaOf3DShapes {
40+
public static void main(String[] args) {
41+
SurfaceAreaOf3DShapes shapes =
42+
new SurfaceAreaOf3DShapes();
43+
int[][] grid = new int[][]{{1,2},{3,4}};
44+
System.out.println("Area is:" + shapes.surfaceArea(grid));
45+
}
46+
47+
public int surfaceArea(int[][] grid) {
48+
int total = 0;
49+
int size = grid.length;
50+
for (int i = 0 ; i< size; i++) {
51+
for (int j = 0; j < size; j++) {
52+
if (grid[i][j] == 0) continue;
53+
int areaGained =
54+
4 * grid[i][j] + 2;
55+
int areaLost = 0;
56+
if (i - 1 >= 0) {
57+
areaLost += Math.min(grid[i][j], grid[i-1][j]);
58+
}
59+
if (i + 1 < size) {
60+
areaLost += Math.min(grid[i][j], grid[i+1][j]);
61+
}
62+
if (j - 1 >= 0) {
63+
areaLost += Math.min(grid[i][j], grid[i][j-1]);
64+
}
65+
if (j + 1 < size) {
66+
areaLost += Math.min(grid[i][j], grid[i][j+1]);
67+
}
68+
total += areaGained - areaLost;
69+
}
70+
}
71+
72+
return total;
73+
}
74+
}
75+
76+
77+
78+
79+
80+
81+
82+
83+
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package in.knowledgegate.dsa.array;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* Determine if a 9 x 9 Sudoku board is valid. Only
8+
* the filled cells need to be validated according
9+
* to the following rules:
10+
*
11+
* Each row must contain the digits 1-9 without repetition.
12+
* Each column must contain the digits 1-9 without
13+
* repetition.
14+
* Each of the nine 3 x 3 sub-boxes of the grid must
15+
* contain the digits 1-9 without repetition.
16+
* Note:
17+
*
18+
* A Sudoku board (partially filled) could be valid
19+
* but is not necessarily solvable.
20+
* Only the filled cells need to be validated
21+
* according to the mentioned rules.
22+
*
23+
*
24+
* Example 1:
25+
* Input: board =
26+
* { {'5','3','.','.','7','.','.','.','.'}
27+
* , {'6','.','.','1','9','5','.','.','.'}
28+
* , {'.','9','8','.','.','.','.','6','.'}
29+
* , {'8','.','.','.','6','.','.','.','3'}
30+
* , {'4','.','.','8','.','3','.','.','1'}
31+
* , {'7','.','.','.','2','.','.','.','6'}
32+
* , {'.','6','.','.','.','.','2','8','.'}
33+
* , {'.','.','.','4','1','9','.','.','5'}
34+
* , {'.','.','.','.','8','.','.','7','9'}}
35+
* Output: true
36+
*
37+
* Example 2:
38+
* Input: board =
39+
* [["8","3",".",".","7",".",".",".","."]
40+
* ,["6",".",".","1","9","5",".",".","."]
41+
* ,[".","9","8",".",".",".",".","6","."]
42+
* ,["8",".",".",".","6",".",".",".","3"]
43+
* ,["4",".",".","8",".","3",".",".","1"]
44+
* ,["7",".",".",".","2",".",".",".","6"]
45+
* ,[".","6",".",".",".",".","2","8","."]
46+
* ,[".",".",".","4","1","9",".",".","5"]
47+
* ,[".",".",".",".","8",".",".","7","9"]]
48+
* Output: false
49+
*
50+
* Explanation: Same as Example 1, except with the
51+
* 5 in the top left corner being modified to 8.
52+
* Since there are two 8's in the top left 3x3
53+
* sub-box, it is invalid.
54+
*
55+
*
56+
* Constraints:
57+
* board.length == 9
58+
* board[i].length == 9
59+
* board[i][j] is a digit 1-9 or '.'.
60+
*/
61+
public class ValidSudoku {
62+
public static void main(String[] args) {
63+
ValidSudoku checker = new ValidSudoku();
64+
char[][] puzzle = new char[][]
65+
{{'5','3','.','.','7','.','.','.','.'}
66+
,{'6','.','.','1','9','5','.','.','.'}
67+
,{'.','9','8','.','.','.','.','6','.'}
68+
,{'8','.','.','.','6','.','.','.','3'}
69+
,{'4','.','.','8','.','3','.','.','1'}
70+
,{'7','.','.','.','2','.','.','.', '6'}
71+
,{'.','6','.','.','.','.','2','8','.'}
72+
,{'.','.','.','4','1','9','.','.','5'}
73+
,{'.','.','.','.','8','.','.', '7','9'}};
74+
System.out.println("IsValid:" + checker.isValidSudoku(puzzle));
75+
}
76+
77+
public boolean isValidSudoku(char[][] board) {
78+
Set<String> visited = new HashSet<>();
79+
for (int i = 0; i < 9; i++) {
80+
for (int j = 0; j < 9; j++) {
81+
char c = board[i][j];
82+
if (c == '.') continue;
83+
if (!visited.add(c +"row" + i) ||
84+
!visited.add(c + "col" + j) ||
85+
!visited.add(c + "box" + i/3 + "-" + j/3)) {
86+
return false;
87+
}
88+
}
89+
}
90+
return true;
91+
}
92+
}
93+
94+
95+

0 commit comments

Comments
 (0)