LeetCode Solutions

672. Bulb Switcher II

Time: $O(1)$

Space: $O(1)$

			

class Solution {
 public:
  int flipLights(int n, int m) {
    n = min(n, 3);

    if (m == 0)
      return 1;
    if (m == 1)
      return vector{2, 3, 4}[n - 1];
    if (m == 2)
      return vector{2, 4, 7}[n - 1];

    return pow(2, n);
  }
};
			

class Solution {
  public int flipLights(int n, int m) {
    n = Math.min(n, 3);

    if (m == 0)
      return 1;
    if (m == 1)
      return new int[] {2, 3, 4}[n - 1];
    if (m == 2)
      return new int[] {2, 4, 7}[n - 1];

    return (int) Math.pow(2, n);
  }
}
			

class Solution:
  def flipLights(self, n: int, m: int) -> int:
    n = min(n, 3)

    if m == 0:
      return 1
    if m == 1:
      return [2, 3, 4][n - 1]
    if m == 2:
      return [2, 4, 7][n - 1]

    return [2, 4, 8][n - 1]