319. Bulb Switcher Medium

@problem@discussion
#Math#Brainteaser



1/**
2 * [319] Bulb Switcher
3 *
4 * There are n bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb.
5 * On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the i^th round, you toggle every i bulb. For the n^th round, you only toggle the last bulb.
6 * Return the number of bulbs that are on after n rounds.
7 *  
8 * Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2020/11/05/bulb.jpg" style="width: 421px; height: 321px;" />
10 * Input: n = 3
11 * Output: 1
12 * Explanation: At first, the three bulbs are [off, off, off].
13 * After the first round, the three bulbs are [on, on, on].
14 * After the second round, the three bulbs are [on, off, on].
15 * After the third round, the three bulbs are [on, off, off]. 
16 * So you should return 1 because there is only one bulb is on.
17 * Example 2:
18 * 
19 * Input: n = 0
20 * Output: 0
21 * 
22 * Example 3:
23 * 
24 * Input: n = 1
25 * Output: 1
26 * 
27 *  
28 * Constraints:
29 * 
30 * 	0 <= n <= 10^9
31 * 
32 */
33pub struct Solution {}
34
35// problem: https://leetcode.com/problems/bulb-switcher/
36// discuss: https://leetcode.com/problems/bulb-switcher/discuss/?currentPage=1&orderBy=most_votes&query=
37
38// submission codes start here
39
40impl Solution {
41    pub fn bulb_switch(n: i32) -> i32 {
42        0
43    }
44}
45
46// submission codes end
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn test_319() {
54    }
55}
56


Back
© 2025 bowen.ge All Rights Reserved.