Created
July 16, 2016 19:08
-
-
Save jianminchen/fb463d7bfe70022f26fc6e0854f94228 to your computer and use it in GitHub Desktop.
bulbs - facebook code lab - partial correct answer
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
public class Solution { | |
public int bulbs(List<Integer> a) { | |
if(a == null || a.size() == 0) | |
return 0; | |
int count = 0; | |
while(a !=null && a.size()> 0) | |
{ | |
Integer previous = a.get(0); | |
a.remove(0); | |
if(previous.intValue() ==0) | |
{ | |
count ++; | |
List<Integer> opposite = getOpposite(a); | |
a = opposite; | |
} | |
} | |
return count; | |
} | |
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
Here is the error message to work on - from facebook code lab:
Time Complexity
Almost there. Your solution is not well optimized for runtime. Focus your effort in solving the problem in shorter time frame.
Partially Correct Answer. Make your solution more efficient