There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the nth round, you only toggle the last bulb. Find how many bulbs are on after n rounds.

Example:

Given n = 3. 

At first, the three bulbs are [off, off, off].
After first round, the three bulbs are [on, on, on].
After second round, the three bulbs are [on, off, on].
After third round, the three bulbs are [on, off, off]. So you should return 1, because there is only one bulb is on.

如果直接按照题目给出的思路来解答,会导致超时。

观察后可以看出一下的过程,假设我们观察第9个灯泡,这个灯泡会在第1, 3, 9轮中分别被toggle一次,所以最后会保持点亮。第10个灯泡,会在1,2,5,10轮被toggle四次,最后保持熄灭。

所以如果一个数的因子个数是奇数个,那么这个灯泡最后就是点亮的。反之,这个灯泡最后就是熄灭的。显然之后完全平方数才有奇数个因子,因为因子都是成对儿出现的。

所以这个问题其实就是问,<=n的正整数中有多少个完全平方数。也就是<= sqrt(n) 的最大正整数是哪个?

return int(math.sqrt(n))

Leetcode Bulb Switcher的更多相关文章

  1. [LeetCode] Bulb Switcher 灯泡开关

    There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...

  2. [LeetCode] Bulb Switcher II 灯泡开关之二

    There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...

  3. LeetCode Bulb Switcher 319

    变换灯泡颜色 There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...

  4. 【LeetCode】319. Bulb Switcher 解题报告(Python)

    [LeetCode]319. Bulb Switcher 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/bulb ...

  5. LeetCode 319 ——Bulb Switcher——————【数学技巧】

    319. Bulb Switcher My Submissions QuestionEditorial Solution Total Accepted: 15915 Total Submissions ...

  6. LC 672. Bulb Switcher II

    There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...

  7. Java [Leetcode 319]Bulb Switcher

    题目描述: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...

  8. LeetCode 319. Bulb Switcher

    There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...

  9. leetcode【67】-Bulb Switcher

    题目描述: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ...

随机推荐

  1. Exploit利用学习1:MS09-001

    目标 IP: Kali系统IP: 1.使用Metasploit框架,在kali终端输入msfconsole打开Metasploit:如下图 2.搜索相关模块:search ms09-001 找到一个可 ...

  2. VMWare发布ESXi 6.0

    发布声明 https://www.vmware.com/support/vsphere6/doc/vsphere-esxi-vcenter-server-60-release-notes.html 获 ...

  3. Oracle 常用函数

    主要是对项目中用过的 oracle 函数进行总结,并做出目录,方便后续项目是快速查找,提高效率. 01.Round (数值的四舍五入) 描述:传回一个数值,该数值是按照指定的小数位元数进行四舍五入运算 ...

  4. lecture11-hopfiled网络与玻尔兹曼机

    Hinton课程第11课 这部分的课程算是个知识背景,讲述RBM的来源吧,毕竟是按照hopfield--BM-RBM的路线过来的. 因为水平有限,都是直译,如果纠结某句话,肯定看不懂,所以这些课程只需 ...

  5. 【开源分享:入门到精通ASP.NET MVC+EF6+Bootstrap】从这里开始,一起搭框架(1)开篇介绍

    框架简介 这几年一直在做ASP.NET开发,几年前做项目都是老老实实一行行的写代码,后来发现那些高手基本都会有自己积累起来的代码库,现在称之为开发框架,基础代码不用再去堆,主要精力可以集中在业务逻辑实 ...

  6. 前端人员一定要掌握的PS技巧

    一.PS与前端知多少 一般我们会认为PS是用来修改图片的,这些工作是美工人员做的事不是前端人员做的,其实这样想你就错了,因为在前端人员也是要学会一些简单的关于PS的技巧的,这样就不会应为一点点小小的需 ...

  7. [Codevs 1421]秋静叶&秋穣子(最大-最小博弈)

    题目:http://codevs.cn/problem/1421/ 分析:有向树上的最大-最小博弈 先手与后手的策略不同: 先手A:让对方取得尽量少的前提下,自己取得尽量大 后手B:让自己取得尽量多的 ...

  8. UltraEdit 编译输出中文乱码的解决办法

    配置UE的时候,都告诉大家javac   %n%e 接可以了,但是再运行的时候,会出现乱码,再加上-J-Duser.language=GBK就可以了,也就是下面的样子. javac -J-Duser. ...

  9. matlab中的卷积——filter,conv之间的区别

    %Matlab提供了计算线性卷积和两个多项式相乘的函数conv,语法格式w=conv(u,v),其中u和v分别是有限长度序列向量,w是u和v的卷积结果序列向量. %如果向量u和v的长度分别为N和M,则 ...

  10. windows server2008 r2 下启用 sqlserver 2008的远程连接

    首先说明,本文转自互联网. TMD 花了二天,终于找到怎么开启这个远程连接了.....娘的,累死了,写下来,希望能帮助同胞们... 用win server 2008 r2 和sql server 20 ...