We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Absolutely OO overkilled this in Java, but I had a fun time doing it.
Note: The sums generated exceed what a 32-bit int can handle. You will need to alter the code provided to use Longs instead of Integers if you notice your results rolling around due to overrun.
All tests passed. Not sure about efficiency; Rolled my own data-nodes. Tried to use TreeSets to do "just in time" node creation and to skip over empty slots during summation.
classResult{publicstaticList<Long>cubeSum(intn,List<String>operations){Cubecube=newCube();List<Long>outputs=newLinkedList<>();for(Stringoperation:operations){OperationType.doOperation(operation.split(" "),cube,outputs);}returnoutputs;}}publicclassSolution{publicstaticvoidmain(String[]args)throwsIOException{BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(System.in));BufferedWriterbufferedWriter=newBufferedWriter(newFileWriter(System.getenv("OUTPUT_PATH")));intT=Integer.parseInt(bufferedReader.readLine().trim());IntStream.range(0,T).forEach(TItr->{try{String[]firstMultipleInput=bufferedReader.readLine().replaceAll("\\s+$","").split(" ");intmatSize=Integer.parseInt(firstMultipleInput[0]);intm=Integer.parseInt(firstMultipleInput[1]);List<String>ops=IntStream.range(0,m).mapToObj(i->{try{returnbufferedReader.readLine();}catch(IOExceptionex){thrownewRuntimeException(ex);}}).collect(toList());List<Long>res=Result.cubeSum(matSize,ops);bufferedWriter.write(res.stream().map(Object::toString).collect(joining("\n"))+"\n");}catch(IOExceptionex){thrownewRuntimeException(ex);}});bufferedReader.close();bufferedWriter.close();}}classCubeextendsNodeSet<Plane>{publicCube(){super(newTreeSet<>());}publicvoidsetValue(intx,inty,intz,intvalue){IndexedNode<Plane>planeNode;planeNode=super.getNode(x);planeNode.getData().setValue(y,z,value);}publiclongsumValues(intx1,inty1,intz1,intx2,inty2,intz2){longsum=0L;for(IndexedNode<Plane>planeNode:getSubset(x1,x2)){sum+=planeNode.getData().sumValues(y1,z1,y2,z2);}returnsum;}@OverridepublicIndexedNode<Plane>setNewNode(intposition){PlanenewPlane=newPlane();IndexedNode<Plane>newNode=newIndexedNode<>(position,newPlane);getNodeSet().add(newNode);returnnewNode;}}interfaceHasIndex{publicintgetIndex();}classIndexedimplementsHasIndex,Comparable<HasIndex>{privatefinalintindex;publicIndexed(intindex){this.index=index;}@OverridepublicintgetIndex(){returnthis.index;}@OverridepublicintcompareTo(HasIndexo){returnInteger.compare(getIndex(),o.getIndex());}}classIndexedNode<T>extendsIndexed{privateTdata;publicIndexedNode(intindex,Tdata){super(index);this.data=data;}publicTgetData(){returndata;}publicvoidsetData(Tdata){this.data=data;}}classLineextendsNodeSet<Integer>{Line(){super(newTreeSet<>());}publiclongsumValues(intz1,intz2){longsum=0L;for(IndexedNode<Integer>valueNode:getSubset(z1,z2)){sum+=valueNode.getData();}returnsum;}@OverridepublicIndexedNode<Integer>setNewNode(intposition){IndexedNode<Integer>newNode=newIndexedNode<>(position,0);getNodeSet().add(newNode);returnnewNode;}publicvoidsetValue(intz,intvalue){IndexedNode<Integer>node=super.getNode(z);node.setData(value);}}abstractclassNodeSet<T>{privatefinalTreeSet<IndexedNode<T>>nodes;NodeSet(TreeSet<IndexedNode<T>>nodes){assertnodes!=null;this.nodes=nodes;}publicTreeSet<IndexedNode<T>>getNodeSet(){returnthis.nodes;}publicSortedSet<IndexedNode<T>>getSubset(intstart,intend){IndexedNode<T>startNode=newIndexedNode<>(start,null);IndexedNode<T>endNode=newIndexedNode<>(end+1,null);returngetNodeSet().subSet(startNode,endNode);}publicIndexedNode<T>getNode(intposition){IndexedNode<T>node=newIndexedNode<>(position,null);if(!getNodeSet().contains(node)){node=setNewNode(position);}else{node=getSubset(position,position+1).first();}returnnode;}publicabstractIndexedNode<T>setNewNode(intposition);}enumOperationType{QUERY("QUERY"){@Overrideprotectedvoidevaluate(String[]inputs,Cubecube,List<Long>outputs){intx1=Integer.parseInt(inputs[1]);inty1=Integer.parseInt(inputs[2]);intz1=Integer.parseInt(inputs[3]);intx2=Integer.parseInt(inputs[4]);inty2=Integer.parseInt(inputs[5]);intz2=Integer.parseInt(inputs[6]);outputs.add(cube.sumValues(x1,y1,z1,x2,y2,z2));}},UPDATE("UPDATE"){@Overrideprotectedvoidevaluate(String[]inputs,Cubecube,List<Long>outputs){intx=Integer.parseInt(inputs[1]);inty=Integer.parseInt(inputs[2]);intz=Integer.parseInt(inputs[3]);intvalue=Integer.parseInt(inputs[4]);cube.setValue(x,y,z,value);}};privatefinalStringtext;OperationType(Stringtext){this.text=text;}publicstaticvoiddoOperation(String[]inputs,Cubecube,List<Long>outputs){Stringtype=inputs[0];for(OperationTypet:OperationType.values()){if(type.toUpperCase().equals(t.text)){t.evaluate(inputs,cube,outputs);return;}}thrownewRuntimeException("Operation \""+type+"\" not found");}protectedabstractvoidevaluate(String[]inputs,Cubecube,List<Long>outputs);}classPlaneextendsNodeSet<Line>{publicPlane(){super(newTreeSet<>());}publiclongsumValues(inty1,intz1,inty2,intz2){longsum=0L;for(IndexedNode<Line>planeNode:getSubset(y1,y2)){sum+=planeNode.getData().sumValues(z1,z2);}returnsum;}publicvoidsetValue(inty,intz,intvalue){Lineline=super.getNode(y).getData();line.setValue(z,value);}@OverridepublicIndexedNode<Line>setNewNode(intposition){Lineline=newLine();IndexedNode<Line>newNode=newIndexedNode<>(position,line);this.getNodeSet().add(newNode);returnnewNode;}}
Cookie support is required to access HackerRank
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 →
Absolutely OO overkilled this in Java, but I had a fun time doing it. Note: The sums generated exceed what a 32-bit int can handle. You will need to alter the code provided to use Longs instead of Integers if you notice your results rolling around due to overrun.
All tests passed. Not sure about efficiency; Rolled my own data-nodes. Tried to use TreeSets to do "just in time" node creation and to skip over empty slots during summation.