Last active
August 29, 2015 14:22
-
-
Save DrewWeth/b6a5ec907465b649d3cd to your computer and use it in GitHub Desktop.
HackerRank Stock Optimization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Array; | |
import java.util.ArrayList; | |
public class Main { | |
public Main(){ | |
} | |
public static void main (String[]args){ | |
System.out.println("Starting"); | |
int[] levels = {1, 2, 100}; | |
Main main = new Main(); | |
main.optimizeStocks(levels); | |
} | |
private void optimizeStocks(int[] levels) { | |
if (levels.length < 2){ | |
System.out.println("Length error."); | |
return; | |
} | |
Node root = new Node(levels[0]); | |
ArrayList<Node> children = new ArrayList<Main.Node>(); | |
children.add(root.setNothing(levels[0])); | |
children.add(root.setBuy(levels[0])); | |
children.add(root.setSell(levels[0], levels, 0)); | |
System.out.println("level 1 done"); | |
for (int i = 1; i < levels.length; i++){ | |
System.out.println("In for loop. Children size: " + children.size()); | |
children = createChildren(children, levels, i); | |
System.out.println("Level " + (i + 1) + " done"); | |
} | |
System.out.println("Out of loop. Child size: " + children.size()); | |
System.out.println("Optimal option is: " + root.findMostMoney(children)); | |
} | |
private ArrayList<Node> createChildren(ArrayList<Node> nodes, | |
int[] levels, int i) { | |
ArrayList<Node> children = new ArrayList<Main.Node>(); | |
for (Node n: nodes){ | |
children.add(n.setNothing(levels[i])); | |
children.add(n.setBuy(levels[i])); | |
children.add(n.setSell(levels[i], levels, i)); | |
} | |
return children; | |
} | |
public class Node { | |
public Node nothing; | |
public Node buy; | |
public Node sell; | |
int levelValue, value, shareCount; | |
public Node(int _level){ | |
levelValue = _level; | |
value = 0; | |
shareCount = 0; | |
} | |
public int findMostMoney(ArrayList<Node> children) { | |
int optimal = 0; | |
for (Node c: children){ | |
if (c.getValue() > optimal) | |
optimal = c.getValue(); | |
} | |
return optimal; | |
} | |
public void printChildren() { | |
System.out.println(this.nothing.getLevelValue()); | |
System.out.println(this.buy.getLevelValue()); | |
System.out.println(this.sell.getLevelValue()); | |
} | |
public Node setSell(int levelValue, int[] levels, int i) { | |
Node node = new Node(levelValue); | |
this.setSell(node); | |
if (this.shareCount == 0) | |
return node; | |
int sharesToSell; | |
if (i+1 == levels.length){ | |
sharesToSell = this.getShareCount(); | |
} | |
else | |
sharesToSell = 1; | |
node.setShareCount(this.getShareCount() - shareCount); | |
node.setValue(this.getValue() + (node.getLevelValue() * sharesToSell)); | |
return node; | |
} | |
public Node setBuy(int levelValue) { | |
Node node = new Node(levelValue); | |
node.setShareCount(this.getShareCount() + 1); | |
int setValue = this.getValue() - node.getLevelValue(); | |
node.setValue(setValue ); | |
// System.out.println("Value: "+ setValue + ". Level value: " + node.getLevelValue()); | |
this.setBuy(node); | |
return node; | |
} | |
public Node setNothing(int levelValue) { | |
Node node = new Node(levelValue); | |
this.setNothing(node); | |
node.setShareCount(this.getShareCount()); | |
node.setValue(this.getValue()); | |
return node; | |
} | |
public int getValue(){ | |
return value; | |
} | |
public void setValue(int _value){ | |
value = _value; | |
} | |
public int getShareCount(){ | |
return shareCount; | |
} | |
public void setShareCount(int _count){ | |
shareCount = _count; | |
} | |
public void setNothing(Node _nothing){ | |
nothing = _nothing; | |
} | |
public void setBuy(Node _buy){ | |
buy = _buy; | |
} | |
public void setSell(Node _sell){ | |
sell = _sell; | |
} | |
public int getLevelValue(){ | |
return levelValue; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment