-
Notifications
You must be signed in to change notification settings - Fork 2
/
319.BulbSwitcher.cpp
70 lines (67 loc) · 1.35 KB
/
319.BulbSwitcher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* @lc app=leetcode id=319 lang=cpp
*
* [319] Bulb Switcher
*
* https://leetcode.com/problems/bulb-switcher/description/
*
* algorithms
* Medium (46.38%)
* Likes: 772
* Dislikes: 1454
* Total Accepted: 103.5K
* Total Submissions: 222.7K
* Testcase Example: '3'
*
* 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 i^th round, you toggle every i bulb. For
* the n^th 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.
*
* Example 2:
*
*
* Input: n = 0
* Output: 0
*
*
* Example 3:
*
*
* Input: n = 1
* Output: 1
*
*
*
* Constraints:
*
*
* 0 <= n <= 10^9
*
*
*/
// @lc code=start
#include<cmath>
class Solution {
public:
int bulbSwitch(int n) {
return static_cast<int>(sqrt(n));
}
};
// @lc code=end