You are viewing a single comment's thread. Return to all comments →
public static List<Long> cubeSum(int n, List<String> operations) { // Write your code here List<Long> res = new ArrayList<>(); int x, y, z, x1, y1, z1 = 0; long val, sum = 0L; Map<Matrix, Long> map = new HashMap<>(); for (String operation : operations) { if (operation.startsWith("UPDATE")) { operation = operation.replace("UPDATE ", ""); String t[] = operation.split(" "); x = Integer.parseInt(t[0]); y = Integer.parseInt(t[1]); z = Integer.parseInt(t[2]); val = Long.parseLong(t[3]); map.put(new Matrix(x, y, z), val); } else { operation = operation.replace("QUERY ", ""); String t[] = operation.split(" "); x = Integer.parseInt(t[0]); y = Integer.parseInt(t[1]); z = Integer.parseInt(t[2]); x1 = Integer.parseInt(t[3]); y1 = Integer.parseInt(t[4]); z1 = Integer.parseInt(t[5]); Iterator<Map.Entry<Matrix, Long>> iterator = map.entrySet() .iterator(); while (iterator.hasNext()) { Map.Entry<Matrix, Long> entry = iterator.next(); Matrix m = entry.getKey(); if (m.x >= x && m.x <= x1 && m.y >= y && m.y <= y1 && m.z >= z && m.z <= z1) { sum+= entry.getValue(); } } res.add(sum); sum = 0; } } return res; }
Seems like cookies are disabled on this browser, please enable them to open this website
Cube Summation
You are viewing a single comment's thread. Return to all comments →