Leetcode 672.灯泡开关II

灯泡开关II

现有一个房间,墙上挂有 n 只已经打开的灯泡和 4 个按钮。在进行了 m 次未知操作后,你需要返回这 n 只灯泡可能有多少种不同的状态。

假设这 n 只灯泡被编号为 [1, 2, 3 ..., n],这 4 个按钮的功能如下:

  1. 将所有灯泡的状态反转(即开变为关,关变为开)
  2. 将编号为偶数的灯泡的状态反转
  3. 将编号为奇数的灯泡的状态反转
  4. 将编号为 3k+1 的灯泡的状态反转(k = 0, 1, 2, ...)

示例 1:

输入: n = 1, m = 1.

输出: 2

说明: 状态为: [开], [关]

示例 2:

输入: n = 2, m = 1.

输出: 3

说明: 状态为: [开, 关], [关, 开], [关, 关]

示例 3:

输入: n = 3, m = 1.

输出: 4

说明: 状态为: [关, 开, 关], [开, 关, 开], [关, 关, 关], [关, 开, 开].

注意: n 和 m 都属于 [0, 1000].

我们将3k+1的值定义为内部值,非3k+1的值定义为外部值,

容易发现,当n>=4的时候,内部值和外部值都有计数和偶数,这里还有一个规律,就是对于一组n,m能够得到的灯泡状态,进行奇数次变换或者是偶数次变换之后能够变回开始的状态,因此对于n,m2,m2>=m+2包含n,m的所有状态

当n=1的时候,当m=1的时候包含全部两种状态,m=2的时候包含全部两种状态,m>2的时候包含m=1的时候的全部状态,所以返回2

当n=2的时候,当m=1的时候包含3种状态,m=2的时候包含4种状态,m=3包含全部状态,m>3的时候包含m=2的全部状态也就是全部2种状态

当n>2的时候,当m=1的时候包含7种状态,当m=2的时候包含8种状态,m=3的时候包含全部8种状态,m>3的时候包含m=2的全部状态也就是8种状态

 1 class Solution {
 2     public int flipLights(int n, int m) {
 3         if(m==0) return 1;
 4         if(n==1) return 2;
 5         if(n==2){
 6             if(m==1) return 3;
 7             else{
 8                 return 4;
 9             }
10         }
11         if(m==1) return 4;
12         if(m==2) return 7;
13         return 8;
14     }
15 }

原文地址:https://www.cnblogs.com/kexinxin/p/10400305.html

时间: 2024-08-03 01:24:29

Leetcode 672.灯泡开关II的相关文章

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

LeetCode——Pascal's Triangle II

Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. public class Solution { public List<Integer> getRow(int rowIndex) { List<List<Integer>> list = new ArrayList<List&

LeetCode --- 63. Unique Paths II

题目链接:Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one

LeetCode Linked List Cycle II

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode* fast = head; ListNode* slow = head;

【Leetcode】Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 思路:与[Leetcode]Path Sum 不同

Leetcode:Reverse Linked List II 反转链表区间

Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given   1->2->3->4->5->NULL,  m = 2 and n = 4, return  1->4->3->2->5->NULL. Note:Given m, n satisfy the following

【LeetCode】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]此题与Spiral Matrix类似,可以用相同的方法解决,相比之下,此题比前一题简单 publ

leetcode第一刷_Subsets II

要求子集,有非常现成的方法.N个数,子集的个数是2^N,每个元素都有在集合中和不在集合中两种状态,这些状态用[0,pow(2,N)]中每个数来穷举,如果这个数中的第i位为1,说明当前集合中包含源数组中的第i个数. 至于有没有重复的元素,大部分有重复元素的问题,都可以借助一个vis集合,里面存放所有已经求得的集合或者其他形式的解,只有少数题目会超时,哪些问题具体的说. class Solution { public: vector<vector<int> > subsetsWithD