LeetCode-in-Java

3842. Toggle Light Bulbs

Easy

You are given an array bulbs of integers between 1 and 100.

There are 100 light bulbs numbered from 1 to 100. All of them are switched off initially.

For each element bulbs[i] in the array bulbs:

Return the list of integers denoting the light bulbs that are on in the end, sorted in ascending order. If no bulb is on, return an empty list.

Example 1:

Input: bulbs = [10,30,20,10]

Output: [20,30]

Explanation:

Example 2:

Input: bulbs = [100,100]

Output: []

Explanation:

Constraints:

Solution

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public List<Integer> toggleLightBulbs(List<Integer> bulbs) {
        boolean[] on = new boolean[101];
        for (int b : bulbs) {
            on[b] = !on[b];
        }
        List<Integer> ans = new ArrayList<>();
        for (int i = 1; i < 101; ++i) {
            if (on[i]) {
                ans.add(i);
            }
        }
        return ans;
    }
}