-
Notifications
You must be signed in to change notification settings - Fork 0
/
BulbSwitcher.ts
71 lines (65 loc) · 2.02 KB
/
BulbSwitcher.ts
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
70
71
// Source : https://leetcode.com/problems/bulb-switcher/
// Author : squxq
// Date : 2023-09-20
/*****************************************************************************************************
*
* 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
******************************************************************************************************/
// number -> number
// return the number of bulbs that are on (toggled even times) after n rounds
// function bulbSwitch(n: number): number {
// let count: number = 0
// for (let i: number = 1; i <= n; i++) {
// let divisors: number = getNumDivisors(i)
// console.log(divisors)
// if (divisors % 2 === 1) count++
// }
// return count
// };
// function getNumDivisors(num: number): number {
// let count: number = 0
// for (let i: number = 1; i <= num; i++) {
// if (num % i === 0) count++
// }
// return count
// }
// function bulbSwitch(n: number): number {
// let count: number = 0
// for (let i: number = 1; i <= n; i++) {
// if (Number.isInteger(Math.sqrt(i))) count++
// }
// return count
// }
export function bulbSwitch(n: number): number {
return Math.floor(Math.sqrt(n));
}