Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created July 16, 2016 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/ae6ab2d83a090b79e161cb4c870d6aa8 to your computer and use it in GitHub Desktop.
Save jianminchen/ae6ab2d83a090b79e161cb4c870d6aa8 to your computer and use it in GitHub Desktop.
bulbs - facebook code lab - stack overflow, need to work on iterative solution
public class Solution {
public int bulbs(List<Integer> a) {
if(a == null || a.size() == 0)
return 0;
Integer val = a.get(0);
a.remove(0);
if(val.intValue() ==0)
{
List<Integer> opposite = getOpposite(a);
return 1 + bulbs(opposite);
}
else
return bulbs(a);
}
public List<Integer> getOpposite(List<Integer> a)
{
List<Integer> list = new ArrayList<Integer>();
if(a== null)
return list;
for(int i=0; i< a.size(); i++)
{
int val = Integer.valueOf(a.get(i));
Integer tmp = val == 0? Integer.valueOf(1):Integer.valueOf(0);
list.add(tmp);
}
return list;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment