码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • leetcode - 319. Bulb Switcher


    Description

    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 ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb.

    Return the number of bulbs that are on after n rounds.

    Example 1:
    在这里插入图片描述

    Input: n = 3
    Output: 1
    Explanation: At first, the three bulbs are [off, off, off].
    After the first round, the three bulbs are [on, on, on].
    After the second round, the three bulbs are [on, off, on].
    After the third round, the three bulbs are [on, off, off]. 
    So you should return 1 because there is only one bulb is on.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Example 2:

    Input: n = 0
    Output: 0
    
    • 1
    • 2

    Example 3:

    Input: n = 1
    Output: 1
    
    • 1
    • 2

    Constraints:

    0 <= n <= 109
    
    • 1

    Solution

    Solved after others’ solutions…

    Ref: https://leetcode.com/problems/bulb-switcher/solutions/77104/math-solution/

    A bulb ends up on iff it is switched an odd number of times.

    Call them bulb 1 to bulb n. Bulb i is switched in round d if and only if d divides i. So bulb i ends up on if and only if it has an odd number of divisors.

    Divisors come in pairs, like i=12 has divisors 1 and 12, 2 and 6, and 3 and 4. Except when i is a square, like 36 has divisors 1 and 36, 2 and 18, 3 and 12, 4 and 9, and double divisor 6. So bulb i ends up on if and only if i is a square.

    So just count the square numbers.

    Let R = int(sqrt(n)). That’s the root of the largest square in the range [1,n]. And 1 is the smallest root. So you have the roots from 1 to R, that’s R roots. Which correspond to the R squares. So int(sqrt(n)) is the answer.

    Time complexity: o ( 1 ) o(1) o(1)
    Space complexity: o ( 1 ) o(1) o(1)

    Code

    class Solution:
        def bulbSwitch(self, n: int) -> int:
            return int(sqrt(n))
    
    • 1
    • 2
    • 3
  • 相关阅读:
    Mac电脑无法识别移动硬盘怎么办?
    Unity性能监控工具-为你的项目性能保驾护航(开源了)
    两个pdf文件合并为一个怎么操作?分享pdf合并操作步骤
    C# +SQL 存储过程 实现系统数据权限审查AOP效果
    服务注册发现_服务自保和服务剔除机制
    【读书笔记】《寻路中国-从乡村到工厂的自驾之旅》
    全新UI简洁又不失美观的短视频去水印微信小程序源码,支持多做流量主模式
    部署一套单Master的K8s集群(kubeadm-V1.20)
    Redis 属于单线程还是多线程?不同版本之间有什么区别?
    mysql基础
  • 原文地址:https://blog.csdn.net/sinat_41679123/article/details/133938188
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号