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. Centos5.8 安装SVN并配置HTTP访问

    安装 svn sudo yum install subversion 测试 svn --version 安装 httpd 的 svn 模块 sudo yum install mod_dav_svn 前 ...

  2. Microsoft.Office.Interop.Excel, Version=12.0.0.0版本高于引用的程序集(已解决)

    Microsoft.Office.Interop.Excel, Version=12.0.0.0版本高于引用的程序集(已解决) 论坛里的帮助:http://bbs.csdn.net/topics/39 ...

  3. NodeJS 开篇 牛刀小试

    一.Node&NPM的安装与配置 下载:https://nodejs.org/en/ V4.2.x LTS(9.75MB)——长期支持版,成熟可靠 V5.x.x Stable(9.71MB)— ...

  4. Java 集合与队列的插入、删除在并发下的性能比较

    这两天在写一个java多线程的爬虫,以广度优先爬取网页,设置两个缓存: 一个保存已经访问过的URL:vistedUrls 一个保存没有访问过的URL:unVistedUrls 需要爬取的数据量不大,对 ...

  5. 读“日请求亿级的QQ会员AMS平台PHP7升级实践”博客心得笔记

    PHP7版本尚未普及,对于前辈们为了性能提升有勇气探索新技术敢于尝螃蟹的精神十分敬佩,倍受鼓舞. PHP7升级面临的风险和挑战 对于一个已经现网在线的大型公共Web服务来说,基础公共软件升级,通常是一 ...

  6. 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!

    今年微软Build 2016大会最让开发人员兴奋的消息之一,就是在Windows上可以原生运行Linux bash,对开发人员来说,这是一个喜闻乐见的消息. 1 安装 你必须安装开发者预览版本,才能使 ...

  7. jQuery经典学习笔记

    1.层次选择器: $("div> span") 获取div下的span元素 $(".one + div") 获取class为one的下一个div 2)过滤 ...

  8. 在nodeJs的Express框架下用TypeScript编写router路由出现import关键字错误的解决方案

    问题出现场景 在项目中采用nodejs做中间层,做页面的首屏渲染,同时采用express作为主web框架,其中express的router页面路由我采用ts语言来编写.如下: //page.ts 文件 ...

  9. 深入理解 cocos2d-x 坐标系

    首先对于初学的,带大家认识 cocos2d-x 中坐标系的几个概念,参考 http://blog.csdn.net/tskyfree/article/details/8292544.其他的往下看. 弄 ...

  10. 关于.Net的面试遐想

    概述 这几天更新相关的面试题目,主是要针对有4年或以上经验的面试者,总体来说,发现面试人员的答题效果和预期相差比较大,我也在想是不是我出的题目偏离现实,但我更愿意相信,是我们一些.Net开发者在工作中 ...