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.
I am getting the time limit exceeded notice for cases 9 through 13. My answers match, so the code is working as needed. I implemented ForkJoinTask and took the run time from 89 seconds to 25 seconds, per my IDE. However, it is still showing that it is taking too long. It recommends looking at the environment info, but I do not understand what I should do with the information on that page.
I do not want the answer, but would not mind a hint of what classes or topics I should research to improve my code speed or performance.
The code I am using, including the code already provided by HackerRank, is below. The solve method and TreeCreator class are my code.
import java.util.;
import java.util.concurrent.;
enum Color {
RED, GREEN
}
abstract class Tree {
private int value;
private Color color;
private int depth;
public Tree(int value, Color color, int depth) {
this.value = value;
this.color = color;
this.depth = depth;
}
public int getValue() {
return value;
}
public Color getColor() {
return color;
}
public int getDepth() {
return depth;
}
public abstract void accept(TreeVis visitor);
}
class TreeNode extends Tree {
private ArrayList<Tree> children = new ArrayList<>();
public TreeNode(int value, Color color, int depth) {
super(value, color, depth);
}
public void accept(TreeVis visitor) {
visitor.visitNode(this);
for (Tree child : children) {
child.accept(visitor);
}
}
public void addChild(Tree child) {
children.add(child);
}
}
class TreeLeaf extends Tree {
public TreeLeaf(int value, Color color, int depth) {
super(value, color, depth);
}
public void accept(TreeVis visitor) {
visitor.visitLeaf(this);
}
}
abstract class TreeVis
{
public abstract int getResult();
public abstract void visitNode(TreeNode node);
public abstract void visitLeaf(TreeLeaf leaf);
}
class SumInLeavesVisitor extends TreeVis {
private int sum = 0;
public int getResult() {
//implement this
return sum;
}
public void visitNode(TreeNode node) {
//implement this
}
public void visitLeaf(TreeLeaf leaf) {
//implement this
sum += leaf.getValue();
}
}
class ProductOfRedNodesVisitor extends TreeVis {
private Long product = 1L;
private int modulus = 1000000007;
public int getResult() {
//implement this
return product.intValue();
}
public void visitNode(TreeNode node) {
//implement this
if (node.getColor() == Color.RED){
product = (product * node.getValue()) % modulus;
if (product == 0) product = 1L;
else if (product < 0) product += modulus;
}
}
public void visitLeaf(TreeLeaf leaf) {
//implement this
if (leaf.getColor() == Color.RED){
product = (product * leaf.getValue()) % modulus;
if (product == 0) product = 1L;
else if (product < 0) product += modulus;
}
}
}
class FancyVisitor extends TreeVis {
int evenSum = 0;
int leafSum = 0;
public int getResult() {
//implement this
return Math.abs(evenSum - leafSum);
}
public void visitNode(TreeNode node) {
//implement this
if (node.getDepth() % 2 == 0) evenSum += node.getValue();
}
public void visitLeaf(TreeLeaf leaf) {
//implement this
if (leaf.getColor() == Color.GREEN) leafSum += leaf.getValue();
}
}
class TreeCreator extends RecursiveAction{
private final TreeNode parent;
private final int parentIndex;
private final ArrayList values;
private final ArrayList colors;
private final HashMap> edges;
private final Set visited;
private final ArrayList children;
Java Visitor Pattern
You are viewing a single comment's thread. Return to all comments →
I am getting the time limit exceeded notice for cases 9 through 13. My answers match, so the code is working as needed. I implemented ForkJoinTask and took the run time from 89 seconds to 25 seconds, per my IDE. However, it is still showing that it is taking too long. It recommends looking at the environment info, but I do not understand what I should do with the information on that page.
I do not want the answer, but would not mind a hint of what classes or topics I should research to improve my code speed or performance.
The code I am using, including the code already provided by HackerRank, is below. The solve method and TreeCreator class are my code.
import java.util.; import java.util.concurrent.;
enum Color { RED, GREEN }
abstract class Tree {
}
class TreeNode extends Tree {
}
class TreeLeaf extends Tree {
}
abstract class TreeVis { public abstract int getResult(); public abstract void visitNode(TreeNode node); public abstract void visitLeaf(TreeLeaf leaf);
}
class SumInLeavesVisitor extends TreeVis { private int sum = 0;
}
class ProductOfRedNodesVisitor extends TreeVis { private Long product = 1L; private int modulus = 1000000007;
}
class FancyVisitor extends TreeVis { int evenSum = 0; int leafSum = 0;
}
class TreeCreator extends RecursiveAction{ private final TreeNode parent; private final int parentIndex; private final ArrayList values; private final ArrayList colors; private final HashMap> edges; private final Set visited; private final ArrayList children;
}
public class Solution {