JAVA CODE 1. On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligne
ID: 3749046 • Letter: J
Question
JAVA CODE
1. On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes.
Each value v = grid[i][j] represents a tower of v cubes placed on top of grid cell (i, j).
Now we view the projection of these cubes onto the xy, yz, and zx planes.
A projection is like a shadow, that maps our 3 dimensional figure to a 2 dimensional plane.
Here, we are viewing the "shadow" when looking at the cubes from the top, the front, and the side.
Return the total area of all three projections.
Example 1:
Input: [[2]]
Output: 5
Example 2:
Input: [[1,2],[3,4]]
Output: 17
Example 3:
Input: [[1,0],[0,2]]
Output: 8
Example 4:
Input: [[1,1,1],[1,0,1],[1,1,1]]
Output: 14
Example 5:
Input: [[2,2,2],[2,1,2],[2,2,2]]
Output: 21
Note:
1 <= grid.length = grid[0].length <= 50
0 <= grid[i][j] <= 50
for (int i = 0; i < N; ++i) {
int bestRow = 0; // largest of grid[i][j]
int bestCol = 0; // largest of grid[j][i]
for (int j = 0; j < N; ++j) {
if (grid[i][j] > 0) ans++; // top shadow
bestRow = Math.max(bestRow, grid[i][j]);
bestCol = Math.max(bestCol, grid[j][i]);
I have this logic above. help me out in building a main method
Explanation / Answer
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
public static void main(String[] args) {
//input
Scanner in = new Scanner(System.in);
System.out.println("Input:");
String input =in.nextLine();
//processes the input and make 2d matrix
input = input.substring(1,input.length()-1); //removes first and last bracket
//extract rows in format like [1,2,3] using regex
Pattern p = Pattern.compile("\[{1}[0-9,.]{1,}\]{1}");
Matcher m = p.matcher(input);
int rc=0; //counts number of match
while(m.find()){
rc++;
}
String rows_string[] = new String[rc]; // array to stores all rows as string like [1,2,3]
Matcher m2 = p.matcher(input);
int counter=0;
while(m2.find()){
rows_string[counter]=m2.group(); //stores row in array
counter++;
}
int num_rows=counter;
if(num_rows>0){
//extracts columns
int num_cols = rows_string[0].substring(1,rows_string[0].length()-1).split(",").length;//counts number of columns
//2d grid
int[][] grid = new int[num_rows][num_cols];
for(int i=0;i<num_rows;i++){
rows_string[i] = rows_string[i].substring(1,rows_string[i].length()-1);
String vals_string[] = rows_string[i].split(",");
for(int j=0;j<num_cols;j++){
grid[i][j] = Integer.parseInt(vals_string[j]); //put value in grid
}
}
int ansXY=0,ansYZ=0,ansXZ=0; //vaariables to sum up shadow area
//shadow on XY plane
for(int i=0;i<num_rows;i++){
for(int j=0;j<num_cols;j++){
if(grid[i][j]>0){
ansXY++;
}
}
}
//shadow on YZ plane
for(int i=0;i<num_rows;i++){
int bestCol = 0;
for(int j=0;j<num_cols;j++){
bestCol = Math.max(bestCol,grid[i][j]);
}
ansYZ=ansYZ+bestCol;
}
//shadow on XZ plane
for(int j=0;j<num_cols;j++){
int bestRow = 0;
for(int i=0;i<num_rows;i++){
bestRow = Math.max(bestRow,grid[i][j]);
}
ansXZ=ansXZ+bestRow;
}
//total shadow
int ans = ansXY+ansYZ+ansXZ;
System.out.println("Output: "+String.valueOf(ans));
}
}
}
//note: if you have any doubt or difficulty in understading, please ask about it.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.