lokendra0408
BAN USERTreeSet<Integer> possibleValues = …;
void fillAllPossiblevalues(List<Integer> values, List<Integer> weights, Integer weightLimit, int valueStored) {
possibleValues.add(valueStored);
for (int i =0 ; i<weights.size(); i++) {
if (weightLimit < weights.get(i)) continue;
fillAllPossibleValues(values,weights, weightLimit -weights.get(i), valueStored + values.get(i));
}
}
Now,
int minsoFar = 0;
for (Integer value : possibleValues) { //this will be in increasing order
if (value!=++minsoFar) {
return misoFar;
}
}
Class Machine {
public int min;
public int max;
}
Map<Integer, Map<Integer, Boolean>> resultCaching = new HashMap<>();
Boolean checkForPossibility(List<Machine> machines, integer m, integer n) {
if (m<0) return false;
if (n<=0) return true;
if (resultCaching.get(m) != null && resultCaching.get(m).get(n) != null) {
return resultCaching.get(m).get(n);
}
boolean isPossible = false;
for(int i =0 ;i<machines.size();i++) {
if (machine.max > m) continue;
//if max is filled
int newm=m-machine.max;
int newn = n-machine.max;
//can we fill remaining part?
boolean resA = checkForPossibility(machines, newm, newn);
//if min is filled
newm = m-machine.min;
newn = m- machine.min;
boolean resB = checkForPossibility(machines, newm, newn, index);
isPossible = resA && resB;
}
//Store result in cachemap, then return
return p=isPossible;
}
Here is mine :
Class Node {
int value;
Node nextRight;
Node left;
Node right;
}
void fillNextRight (Node root) {
if (root == null)
return;
Node current = root;
Node nextLevelStart = root;
while (nextLevelStart != null) {
current = nextLevelStart;
nextLevelStart = null;
Node nextLevelLast = null;
while (current != null) {
fillValues( nextLevelStart, nextLevelLast, current.left);
fillValues( nextLevelStart, nextLevelLast, current.right);
current=current.nextRight;
}
}
}
void fillValues (Node nextLevelStart,Node nextLevelLast, Node nextLevelCurrent) {
if (nextLevelCurrent != null) {
if (nextLevelStart == null) nextLevelStart == nextLevelCurrent;
if (nextLevelLast != null) {
nextLevelLast.rightNode = nextLevelCurrent;
}
nextLevelLast = nextLevelCurrent;
}
}
Hi Astronaut,
- lokendra0408 February 24, 2013You are right about the weights.get(i) mistake.
Why I have used Treeset is because it will give you the elements in sorted order while you are iterating.
Whereas linkedHashSet will give you the order in which elements were inserted!
Notice the comment "//this will be in increasing order"