|
| 1 | +package hackerRank_JavaProblemSolving; |
| 2 | + |
| 3 | +import java.io.BufferedWriter; |
| 4 | +import java.io.FileWriter; |
| 5 | +import java.io.IOException; |
| 6 | +import java.util.Scanner; |
| 7 | + |
| 8 | +public class Problem081_3DSurfaceArea__ComplexCubicalForm { |
| 9 | + |
| 10 | + // CREDITS : https://www.hackerrank.com/subash_kotha2?hr_r=1 |
| 11 | + static private int surfaceArea(int[][] a, int h, int w) { |
| 12 | + int sum=0; |
| 13 | + for(int i = 0 ; i < h; ++i) { |
| 14 | + for(int j = 0; j < w; ++j) { |
| 15 | + if (i == 0) { |
| 16 | + sum += a[i][j]; |
| 17 | + } |
| 18 | + if (i == h-1) { |
| 19 | + sum += a[i][j]; |
| 20 | + } |
| 21 | + if (j == 0) { |
| 22 | + sum += a[i][j]; |
| 23 | + } |
| 24 | + if (j == w-1) { |
| 25 | + sum += a[i][j]; |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + for(int i=0; i<h; ++i) { |
| 31 | + for(int j=0;j<w-1;++j) { |
| 32 | + if (a[i][j] < a[i][j+1]) { |
| 33 | + sum+=(a[i][j+1]-a[i][j]); |
| 34 | + } |
| 35 | + if (a[i][w-j-1] < a[i][w-j-2]) { |
| 36 | + sum+=(a[i][w-j-2]-a[i][w-j-1]); |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + for(int i=0; i<h-1; ++i) { |
| 42 | + for(int j=0; j<w; ++j) { |
| 43 | + if (a[i][j]<a[i+1][j]) { |
| 44 | + sum+=(a[i+1][j]-a[i][j]); |
| 45 | + } |
| 46 | + if(a[h-i-1][j]<a[h-i-2][j]) { |
| 47 | + sum+=(a[h-i-2][j]-a[h-i-1][j]); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + return (sum+(2*h*w)); |
| 52 | + } // surfaceArea |
| 53 | + |
| 54 | + private static final Scanner scanner = new Scanner(System.in); |
| 55 | + |
| 56 | + public static void main(String[] args) throws IOException { |
| 57 | + BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); |
| 58 | + |
| 59 | + String[] HW = scanner.nextLine().split(" "); |
| 60 | + |
| 61 | + int H = Integer.parseInt(HW[0]); |
| 62 | + |
| 63 | + int W = Integer.parseInt(HW[1]); |
| 64 | + |
| 65 | + int[][] A = new int[H][W]; |
| 66 | + |
| 67 | + for (int i = 0; i < H; i++) { |
| 68 | + String[] ARowItems = scanner.nextLine().split(" "); |
| 69 | + scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); |
| 70 | + |
| 71 | + for (int j = 0; j < W; j++) { |
| 72 | + int AItem = Integer.parseInt(ARowItems[j]); |
| 73 | + A[i][j] = AItem; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + int result = surfaceArea(A,H,W); |
| 78 | + |
| 79 | + bufferedWriter.write(String.valueOf(result)); |
| 80 | + bufferedWriter.newLine(); |
| 81 | + |
| 82 | + bufferedWriter.close(); |
| 83 | + |
| 84 | + scanner.close(); |
| 85 | + } |
| 86 | +} |
0 commit comments