作者pandix (麵包屌)
看板Marginalman
標題Re: [閒聊] 每日LeetCode
時間Fri Apr 28 01:09:10 2023
319. Bulb Switcher
有 n 個燈泡,執行 n 個回合以下操作:
假設是第 i 個回合,每隔 i 個燈泡切換燈泡開關(開->關 / 關->開)
問最後有幾顆燈泡亮著
Example 1:
Input: n = 3
Output: 1
第一輪: 亮 / 亮 / 亮
第二輪: 亮 / 不亮 / 亮
第三輪: 亮 / 不亮 / 不亮
Example 2:
Input: n = 0
Output: 0
Example 3:
Input: n = 4
Output: 2
第一輪: 亮 / 亮 / 亮 / 亮 #按1234
第二輪: 亮 / 不亮 / 亮 / 不亮 #按24
第三輪: 亮 / 不亮 / 不亮 / 不亮 #按3
第四輪: 亮 / 不亮 / 不亮 / 亮 #按4
思路:
第i個燈泡亮著的條件 -> i 有奇數個因數
平方數才會有奇數個因數 所以找小於等於 n 的平方數個數就好
直接開根號就是答案了
下系下井= = 幹!
class Solution:
def bulbSwitch(self, n: int) -> int:
return int(sqrt(n))
--
蛤?
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.252.3.181 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1682615354.A.233.html
→ SecondRun: 大師 04/28 01:10
推 Benbenyale: 大師 04/28 01:11
推 bmtuspd276b: 大師 04/28 01:12
推 NTHUlagka: 好強 04/28 13:20