LeetCode: 672. Bulb Switcher II

There is a room with n lights which are turned on initially and 4 buttons on the wall. After performing exactly m unknown operations towards buttons, you need to return how many different kinds of status of the n lights could be.

Suppose n lights are labeled as number [1, 2, 3 …, n], function of these 4 buttons are given below:

  1. Flip(按動開關) all the lights.
  2. Flip(按動開關) lights with even numbers.
  3. Flip(按動開關) lights with odd numbers.
  4. Flip(按動開關) lights with (3k + 1) numbers, k = 0, 1, 2, …

Example 1:

Input: n = 1, m = 1.
Output: 2
Explanation: Status can be: [on], [off]

Example 2:

Input: n = 2, m = 1.
Output: 3
Explanation: Status can be: [on, off], [off, on], [off, off]

Example 3:

Input: n = 3, m = 1.
Output: 4
Explanation: Status can be: [off, on, off], [on, off, on], [off, off, off], [off, on, on].

Note: n and m both fit in range [0, 1000].

這道題又是一個**的數學題發火。找規律呀找規律。

我們只需要考慮當 n<=2 and m < 3 的特殊情形。因爲當 n >2 and m >=3, 結果肯定是 8.
The four buttons:

  1. Flip all the lights.
  2. Flip lights with even numbers.
  3. Flip lights with odd numbers.
  4. Flip lights with (3k + 1) numbers, k = 0, 1, 2, …

如果我們使用了 button 1 和 2, 其效果等同於使用 button 3 。
類似的..

1 + 2 --> 3, 1 + 3 --> 2, 2 + 3 --> 1
所以,只有 8 種情形。

All_on12341+42+43+4

並且當 n>2 and m>=3 時,我們就能夠獲得所有的情形。

以下是另外一種解釋的方法:

這 4 個按鈕代表了 4 種數字。
x % 2 == 1 && x % 3 == 1, such as x == 1;
x % 2 == 1 && x % 3 != 1, such as x == 3;
x % 2 == 0 && x % 3 == 1, such as x == 4;
x % 2 == 0 && x % 3 != 1, such as x == 2.

因此有 8 種獨立的按鈕操作(假設原來的狀態是 on, on, on, on) :
1                                               (off, off, off, off)
2                                               (on, off, on, off)
3                                               (off, on, off, on)
4                                               (off, on, on, off)
1 + 4                                         (on, off, off, on)
2 + 4                                         (off, off, on, on)
3 + 4                                         (on, on, off, off)
1 + 2 + 3 == 3 + 3                    (on, on, on, on)
因爲 1 + 2 == 3, 2 + 3 == 1, 1 + 3 == 2, 1 + 2 + 4 == 3 + 4, 2 + 3 + 4 == 1 + 4,1 + 3 + 4 == 2 + 4, 1 + 2 + 3 + 4 == 3 + 3 + 4 == 4。

當 m == 0 時, 無事發生,只有 1 種狀態。

當 n == 1 時, bulb 可以爲 “on” 狀態(使用按鈕 2 來按動偶數開關,不會造成任何變化 )。也可以爲 “off” 狀態(使用了按鈕 1 or 3 or 4 )。
操作 2 = 操作 1+操作 3 。操作 3 = 操作 1 + 操作 2 。操作 1 = 操作 2 + 操作 3 。因此,對於奇數次操作還是偶數次操作,我們都可以得到相同的狀態。 當 n == 1 時,結果始終爲 2 ,始終能得到 2 個狀態。

當 n == 2 時, 有 4 種可能的狀態。”on, off” == 2 ==1 + 3 。   “off, on” == 3 == 1 + 2。    “off, off” == 1 == 2 + 3。    “on, on” == 1 + 1 == 2 + 3 + 1 == 1 + 3 + 3 + 1 ==  2 + 2 == 3 + 3 == 4 + 4 。除了狀態 “on, on” 需要 2 次或者 2 次以上的操作,其他狀態都可以通過任意奇數/偶數操作來得到。

當 n == 3 時, 8 種 不同的狀態都有可能發生。使用跟上面同樣的分析方法,我們可以發現除了 m == 1 and m == 2 的情形, 其他的情形都能夠得到 8 種狀態。

class Solution {
    public int flipLights(int n, int m) {
        if(m==0) return 1;
        if(n==1) return 2;
        if(n==2&&m==1) return 3;
        if(n==2) return 4;
        if(m==1) return 4;
        if(m==2) return 7;
        if(m>=3) return 8;
        return 8;
    }
}
還有大神表示,這道題或許可以考慮用 bit manipulation 來做。因爲這 4 個操作可以看成是:對  111, 010, 101, 100 來進行 xor 操作。

還有大神表示,這道題可以有 DP 的思路。

 dp with m rows and n columns。dp[ i ][ j ] 表示 i 個操作 j 個燈泡 能得到的最多狀態數( i 與 j 從 1 開始計數)。假設 m == 5 , n == 7, 那麼 dp 如下所示:

2, 3, 4, 4, 4, 4, 4, 
2, 4, 7, 7, 7, 7, 7, 
2, 4, 8, 8, 8, 8, 8, 
2, 4, 8, 8, 8, 8, 8, 
2, 4, 8, 8, 8, 8, 8,

因爲題目比較新,所以還沒有更多的人給出更棒的解答,讓我們持續關注這道題的 discussion 區吧。https://leetcode.com/problems/bulb-switcher-ii/description/


轉載自:http://blog.csdn.net/huanghanqian/article/details/77857912

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章