Cube Summation

  • + 0 comments

    Java 8:

        public static List<Long> cubeSum(int n, List<String> operations) {
            long[][][] a = new long[n][n][n];
            
            List<Long> res = new ArrayList<>();
            for(String s: operations) {
                String[] v = s.split(" ");
                if(s.contains("UPDATE")) {
                    a[Integer.valueOf(v[1]) - 1][Integer.valueOf(v[2]) - 1][Integer.valueOf(v[3]) - 1] = Long.valueOf(v[4]);
                } else {
                    int x1 = Integer.valueOf(v[1]) - 1;
                    int y1 = Integer.valueOf(v[2]) - 1;
                    int z1 = Integer.valueOf(v[3]) - 1;
                    int x2 = Integer.valueOf(v[4]) - 1;
                    int y2 = Integer.valueOf(v[5]) - 1;
                    int z2 = Integer.valueOf(v[6]) - 1;
                    long sum = 0;
                    for (int i = x1; i <= x2; i++) {
                        for (int j = y1; j <= y2; j++) {
                            for (int k = z1; k <= z2; k++) {
                                sum += a[i][j][k];
                            }
                        }
                    }
                    res.add(sum);
                }
            }
            return res;
    
        }