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. bzoj3339 rmq problem (range mex query)

    给一个长度为n的数列a,q个询问,每次询问一段区间的mex.(没有出现过的最小非负整数) 1<=n,q<=200000,0<=ai<=200000. 题解1 莫队 我们将权值分 ...

  2. DEDECMS之九 文章采集

    到很多网友都为织梦(DEDECMS)的采集教程头疼,的确,官方出的教程太笼统了,什么都没说,换个网站你什么都做不了,这个教程是最详尽的教程,让你一看即会! 一.列表采集 第一步.我们打开织梦后台点击采 ...

  3. IE6 P标签下DIV无法inline-block

    IE6 P标签下的DIV标签无法inline-block,使其触发了hasLayout属性再用csshack 使其inline还是不行,始终要换行 解决:把div标签替换成非div标签,比如span等 ...

  4. node基础01:简要介绍

    1.node vs php 优点 性能高(机制问题) 开发效率高(省了不少优化的事) 应用范围广(可以开发桌面系统,electron框架) 缺点 新,人少 中间件少 IDE不完善 2.node的劣势和 ...

  5. caffe的python接口学习(3):训练模型(training)

    如果不进行可视化,只想得到一个最终的训练model, 那么代码非常简单,如下 : import caffe caffe.set_device(0) caffe.set_mode_gpu() solve ...

  6. ubuntu-12.10-server安装图形界面

    1.首先你需要确定你的源文件中 /etc/apt/sources.list 已经使用Universe和Multiverse库.然后使用下面的命令来进行更新源列表和安装图形桌面. sudo apt-ge ...

  7. GBK 编码时 url 中带中文参数的问题

    项目中遇到的 GBK 编码问题,记录如下. 将代码精简为: <!DOCTYPE HTML> <html> <meta charset="gb2312" ...

  8. iptables规则组成

    一.四张表五条链 组成部分:四张表 + 5条链(Hook point) + 规则 四张表:filter nat mangle raw 五条链:PREROUTING INPUT FORWARD OUTP ...

  9. 基于PHP的AJAX学习笔记(教程)

    本文转载自:http://www.softeng.cn/?p=107 这是本人在学习ajax过程所做的笔记,通过本笔记的学习,可以完成ajax的快速入门.本笔记前端分别使用原生态的javascript ...

  10. unittest使用过程中sys.exit(not self.result.wasSuccessful())

    起因: 在运行下面的unittest过程中出现了个Traceback: 被测试脚本: # splitter.py def split(line, types=None, delimiter=None) ...