灯的开关 Bulb Switcher II
为什么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:
- Flip all the lights.
- Flip lights with even numbers.
- Flip lights with odd numbers.
- 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相关推荐
- LeetCode(319):灯泡开关 Bulb Switcher(Java)
2019.8.6 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新) 每个数i必能分解成任意两个数的乘积(最少会有1*i),因此只有平方数会进行单次开关,因此只需要统计截止n的平方 ...
- 有关计算机代码的游戏,七灯游戏是一款经典的益智类游戏。游戏中,有七盏灯排成一圈,如图a所示,初始时灯的开关状态随机生成,操作其中某一盏灯,则可以切换该灯的“开/关”状态,同时,这盏灯-组卷网...
七灯游戏是一款经典的益智类游戏.游戏中,有七盏灯排成一圈,如图a所示,初始时灯的开关状态随机生成,操作其中某一盏灯,则可以切换该灯的"开/关"状态,同时,这盏灯对面的两盏灯也会切换 ...
- STM32——继电器控制灯的开关
STM32--继电器控制灯的开关 文章目录 STM32--继电器控制灯的开关 继电器控制灯的开关 项目概述: 环境概述: 项目的开始: 第一步: 第二步: 1.配置GPIOA时钟 2.GPIOA3的结 ...
- 一个按钮控制灯的开关
一个按钮控制灯的开关 可以通过Simulator进行模拟展示
- 如何使用NE555设计一个触摸延时灯或开关
今天给大家分享一个使用NE555设计一个触摸延时灯或开关的电路 电路图如下,主要的原理就是,555的2脚分压电阻电阻大,分压电压抗干扰差,手触碰会有杂波信号耦合到引脚2,当2脚电压小于1/3VCC时输 ...
- Arduino-Mixly-超声波测距仪-实现距离控制LED灯的开关
Arduino-Mixly-超声波测距仪-实现距离控制LED灯的开关 Arduino:个人理解那块电板就叫arduino,本质上它是一块单片机(功能不怎么强大的计算机),具备了接受信息处理信息的能力, ...
- (含代码)ESP8266+舵机 制作wifi灯控开关(arduino,点灯科技,小爱同学/天猫精灵实现)
目录 项目涉及的主代码 前言 一.环境搭建 二.开发步骤 1.注册点灯科技平台 2.在点灯科技平台注册设备,获取Secret Key 3.在Arduino IDE中编写代码,写入ESP8266 4.编 ...
- TP-LINK 720N刷了OpenWRT后LED灯定时开关
TP-LINK 720N刷了OpenWRT后LED灯定时开关 参考链接:https://blog.wangmao.me/openwrt-timing-off-led-lights.html 因为家里有 ...
- 100个人,100盏灯,全部熄灭,第一人过去按所有灯的开关,第二个人会每隔一盏灯按开关,第三个人每隔2盏灯按开关,以此类推,第100人隔99盏灯按开关,100个人都走完,剩多少盏灯亮着?
100个人,100盏灯,全部熄灭,第一人过去按所有灯的开关,第二个人会每隔一盏灯按开关,第三个人每隔2盏灯按开关,以此类推, 第100人隔99盏灯按开关. 问:100个人都走完,剩多少盏灯亮着? ...
最新文章
- 我的2008年(上)-《走出软件作坊》是怎样炼成的
- golang web 静态文件
- tensorflow tf.train.Saver.restore() (用于下次训练时恢复模型)
- 修改Linux系统时间
- mysql handlers,2 Handlers
- html中写随机数,为HTML生成一个随机数
- Science:“熬夜会变傻”终于有科学依据了
- 把UltraEdit改造成VC
- click事件的执行顺序
- 这 8款开源思维导图工具真的很神奇【程序员必备学习工具】
- 集群、分布式架构与SOA架构
- 你需要的三维激光LiDAR点云数据处理服务来了!
- amd显卡驱动目录linux,面向 Radeon、Radeon Pro、FirePro、APU、CPU、锐龙、台式机、笔记本的 AMD 驱动程序和支持...
- MySQL卸载不干净问题
- 学习一下企业管理信息化建设的“诺兰模型”
- 10个提高工作效率的超实用工具
- Taro 如何开始微信小程序的开发
- CSS3 制作正方体
- mysql 16g内存够用么_日常办公多大的内存够用 电脑内存8g和16g的区别
- 易软通WMS仓储管理系统介绍
热门文章
- linux_2.6内核内存缓冲与I/O调度机制:
- 打包静默安装参数(nsis,msi,InstallShield,InnoSetup)[转]
- C/C++输入输出函数(I/O)总结
- 解决pycharm输入法不跟随的方法
- HDU 5816 Hearthstone
- OK335xS CAN device register and deiver match hacking
- [DONE]ReferenceError: WebSocket is not defined pomelo
- VMware vSphere 5.1 学习系列之:安装 vCenter Server
- linux命令使用全集
- linux问答学知识