Skip to content

Commit c5d742b

Browse files
authored
feat: add solutions to lc problems: No.0319,0323 (#2755)
* No.0319.Bulb Switcher * No.0323.Number of Connected Components in an Undirected Graph
1 parent 42e99e8 commit c5d742b

File tree

19 files changed

+1047
-181
lines changed

19 files changed

+1047
-181
lines changed

solution/0300-0399/0319.Bulb Switcher/README.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,28 @@
5656

5757
## 解法
5858

59-
### 方法一
59+
### 方法一:数学
60+
61+
我们不妨将 $n$ 个灯泡编号为 $1, 2, 3, \cdots, n$,那么对于第 $i$ 个灯泡,它会在第 $d$ 轮被操作,当且仅当 $d$ 是 $i$ 的因子。
62+
63+
对于一个数 $i$,它的因子个数是有限的,且因子个数为奇数时,最后的状态是开启的,否则是关闭的。
64+
65+
因此,我们只需要找到 $1$ 到 $n$ 中因子个数为奇数的数的个数即可。
66+
67+
对于一个数 $i$,如果它有因子 $d$,那么它一定有因子 $i/d$,因此因子个数为奇数的数一定是平方数。
68+
69+
举个例子,数字 $12$ 的因子有 $1, 2, 3, 4, 6, 12$,因子个数为 $6$,是偶数;而对于数字 $16$ 这个平方数,因子有 $1, 2, 4, 8, 16$,因子个数为 $5$,是奇数。
70+
71+
因此,我们只需要找到 $1$ 到 $n$ 中有多少个平方数即可,即 $\lfloor \sqrt{n} \rfloor$。
72+
73+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
6074

6175
<!-- tabs:start -->
6276

6377
```python
6478
class Solution:
6579
def bulbSwitch(self, n: int) -> int:
66-
return int(n ** (1 / 2))
80+
return int(sqrt(n))
6781
```
6882

6983
```java
@@ -74,6 +88,27 @@ class Solution {
7488
}
7589
```
7690

91+
```cpp
92+
class Solution {
93+
public:
94+
int bulbSwitch(int n) {
95+
return (int) sqrt(n);
96+
}
97+
};
98+
```
99+
100+
```go
101+
func bulbSwitch(n int) int {
102+
return int(math.Sqrt(float64(n)))
103+
}
104+
```
105+
106+
```ts
107+
function bulbSwitch(n: number): number {
108+
return Math.floor(Math.sqrt(n));
109+
}
110+
```
111+
77112
<!-- tabs:end -->
78113

79114
<!-- end -->

solution/0300-0399/0319.Bulb Switcher/README_EN.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,28 @@ So you should return 1 because there is only one bulb is on.</pre>
4747

4848
## Solutions
4949

50-
### Solution 1
50+
### Solution 1: Mathematics
51+
52+
We can number the $n$ bulbs as $1, 2, 3, \cdots, n$. For the $i$-th bulb, it will be operated in the $d$-th round if and only if $d$ is a factor of $i$.
53+
54+
For a number $i$, the number of its factors is finite. If the number of factors is odd, the final state is on; otherwise, it is off.
55+
56+
Therefore, we only need to find the number of numbers from $1$ to $n$ with an odd number of factors.
57+
58+
For a number $i$, if it has a factor $d$, then it must have a factor $i/d$. Therefore, numbers with an odd number of factors must be perfect squares.
59+
60+
For example, the factors of the number $12$ are $1, 2, 3, 4, 6, 12$, and the number of factors is $6$, which is even. For the perfect square number $16$, the factors are $1, 2, 4, 8, 16$, and the number of factors is $5$, which is odd.
61+
62+
Therefore, we only need to find how many perfect squares there are from $1$ to $n$, which is $\lfloor \sqrt{n} \rfloor$.
63+
64+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
5165

5266
<!-- tabs:start -->
5367

5468
```python
5569
class Solution:
5670
def bulbSwitch(self, n: int) -> int:
57-
return int(n ** (1 / 2))
71+
return int(sqrt(n))
5872
```
5973

6074
```java
@@ -65,6 +79,27 @@ class Solution {
6579
}
6680
```
6781

82+
```cpp
83+
class Solution {
84+
public:
85+
int bulbSwitch(int n) {
86+
return (int) sqrt(n);
87+
}
88+
};
89+
```
90+
91+
```go
92+
func bulbSwitch(n int) int {
93+
return int(math.Sqrt(float64(n)))
94+
}
95+
```
96+
97+
```ts
98+
function bulbSwitch(n: number): number {
99+
return Math.floor(Math.sqrt(n));
100+
}
101+
```
102+
68103
<!-- tabs:end -->
69104

70105
<!-- end -->
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
int bulbSwitch(int n) {
4+
return (int) sqrt(n);
5+
}
6+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func bulbSwitch(n int) int {
2+
return int(math.Sqrt(float64(n)))
3+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class Solution:
22
def bulbSwitch(self, n: int) -> int:
3-
return int(n ** (1 / 2))
3+
return int(sqrt(n))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function bulbSwitch(n: number): number {
2+
return Math.floor(Math.sqrt(n));
3+
}

0 commit comments

Comments
 (0)