为什么80%的码农都做不了架构师?>>>   

问题:

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.

四个按钮的功能:

  • 翻转所有的灯。
  • 翻转偶数的灯。
  • 翻转奇数的灯。
  • 翻转(3k + 1)数字,k = 0,1,2,...

如果我们使用按钮1和2,则等同于使用按钮3。

同样的:

1 + 2 → 3,1 + 3 → 2,2 + 3 → 1
所以,只有8种结果:1,2,3,4,1 + 4,2 + 4,3 + 4,当n> 2和m> = 3时,我们可以得到所有的情况。

class Solution { //7ms
    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;
    }
}

//O(1)数学问题,总共有8个state  1111,1010,0101,0111,0000,0011, 1100 and 1001.
//需要枚举 n>3以后就只能是这8个state了
//n == 1 Only 2 possibilities: 1 and 0.
//n == 2 After one operation, it has only 3 possibilities: 00, 10 and 01. After two and more operations, it has only 4 possibilities: 11, 10, 01 and 00.
//n == 3 After one operation, it has only 4 possibilities: 000, 101, 010 and 011. After two operations, it has 7 possibilities: 111,101,010,100,000,001 and 110. After three and more operations, it has 8 possibilities, plus 011 on above case.
//n >= 4 After one operation, it has only 4 possibilities: 0000, 1010, 0101 and 0110.
//After two or more operations: it has 8 possibilities, 1111,1010,0101,0111,0000,0011, 1100 and 1001.
class Solution {//8mspublic int flipLights(int n, int m) {n = Math.min(n, 3);if (m == 0) return 1;if (m == 1) return n == 1 ? 2 : n == 2 ? 3 : 4;if (m == 2) return n == 1 ? 2 : n == 2 ? 4 : 7;return n == 1 ? 2 : n == 2 ? 4 : 8;}
}

转载于:https://my.oschina.net/liyurong/blog/1606467

灯的开关 Bulb Switcher II相关推荐

  1. LeetCode(319):灯泡开关 Bulb Switcher(Java)

    2019.8.6 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新) 每个数i必能分解成任意两个数的乘积(最少会有1*i),因此只有平方数会进行单次开关,因此只需要统计截止n的平方 ...

  2. 有关计算机代码的游戏,七灯游戏是一款经典的益智类游戏。游戏中,有七盏灯排成一圈,如图a所示,初始时灯的开关状态随机生成,操作其中某一盏灯,则可以切换该灯的“开/关”状态,同时,这盏灯-组卷网...

    七灯游戏是一款经典的益智类游戏.游戏中,有七盏灯排成一圈,如图a所示,初始时灯的开关状态随机生成,操作其中某一盏灯,则可以切换该灯的"开/关"状态,同时,这盏灯对面的两盏灯也会切换 ...

  3. STM32——继电器控制灯的开关

    STM32--继电器控制灯的开关 文章目录 STM32--继电器控制灯的开关 继电器控制灯的开关 项目概述: 环境概述: 项目的开始: 第一步: 第二步: 1.配置GPIOA时钟 2.GPIOA3的结 ...

  4. 一个按钮控制灯的开关

    一个按钮控制灯的开关 可以通过Simulator进行模拟展示

  5. 如何使用NE555设计一个触摸延时灯或开关

    今天给大家分享一个使用NE555设计一个触摸延时灯或开关的电路 电路图如下,主要的原理就是,555的2脚分压电阻电阻大,分压电压抗干扰差,手触碰会有杂波信号耦合到引脚2,当2脚电压小于1/3VCC时输 ...

  6. Arduino-Mixly-超声波测距仪-实现距离控制LED灯的开关

    Arduino-Mixly-超声波测距仪-实现距离控制LED灯的开关 Arduino:个人理解那块电板就叫arduino,本质上它是一块单片机(功能不怎么强大的计算机),具备了接受信息处理信息的能力, ...

  7. (含代码)ESP8266+舵机 制作wifi灯控开关(arduino,点灯科技,小爱同学/天猫精灵实现)

    目录 项目涉及的主代码 前言 一.环境搭建 二.开发步骤 1.注册点灯科技平台 2.在点灯科技平台注册设备,获取Secret Key 3.在Arduino IDE中编写代码,写入ESP8266 4.编 ...

  8. TP-LINK 720N刷了OpenWRT后LED灯定时开关

    TP-LINK 720N刷了OpenWRT后LED灯定时开关 参考链接:https://blog.wangmao.me/openwrt-timing-off-led-lights.html 因为家里有 ...

  9. 100个人,100盏灯,全部熄灭,第一人过去按所有灯的开关,第二个人会每隔一盏灯按开关,第三个人每隔2盏灯按开关,以此类推,第100人隔99盏灯按开关,100个人都走完,剩多少盏灯亮着?

    100个人,100盏灯,全部熄灭,第一人过去按所有灯的开关,第二个人会每隔一盏灯按开关,第三个人每隔2盏灯按开关,以此类推, 第100人隔99盏灯按开关. 问:100个人都走完,剩多少盏灯亮着?    ...

最新文章

  1. 我的2008年(上)-《走出软件作坊》是怎样炼成的
  2. golang web 静态文件
  3. tensorflow tf.train.Saver.restore() (用于下次训练时恢复模型)
  4. 修改Linux系统时间
  5. mysql handlers,2 Handlers
  6. html中写随机数,为HTML生成一个随机数
  7. Science:“熬夜会变傻”终于有科学依据了
  8. 把UltraEdit改造成VC
  9. click事件的执行顺序
  10. 这 8款开源思维导图工具真的很神奇【程序员必备学习工具】
  11. 集群、分布式架构与SOA架构
  12. 你需要的三维激光LiDAR点云数据处理服务来了!
  13. amd显卡驱动目录linux,面向 Radeon、Radeon Pro、FirePro、APU、CPU、锐龙、台式机、笔记本的 AMD 驱动程序和支持...
  14. MySQL卸载不干净问题
  15. 学习一下企业管理信息化建设的“诺兰模型”
  16. 10个提高工作效率的超实用工具
  17. Taro 如何开始微信小程序的开发
  18. CSS3 制作正方体
  19. mysql 16g内存够用么_日常办公多大的内存够用 电脑内存8g和16g的区别
  20. 易软通WMS仓储管理系统介绍

热门文章

  1. linux_2.6内核内存缓冲与I/O调度机制:
  2. 打包静默安装参数(nsis,msi,InstallShield,InnoSetup)[转]
  3. C/C++输入输出函数(I/O)总结
  4. 解决pycharm输入法不跟随的方法
  5. HDU 5816 Hearthstone
  6. OK335xS CAN device register and deiver match hacking
  7. [DONE]ReferenceError: WebSocket is not defined pomelo
  8. VMware vSphere 5.1 学习系列之:安装 vCenter Server
  9. linux命令使用全集
  10. linux问答学知识