-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathmain.cpp
More file actions
55 lines (50 loc) · 980 Bytes
/
main.cpp
File metadata and controls
55 lines (50 loc) · 980 Bytes
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
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
public:
int bulbSwitch(int n) {
int len = 3, state = 0;
int curr = 0;
while (n > curr) {
curr += len;
len += 2;
state++;
}
return state;
}
int bulbSwitchBruteForce(int n) {
vector<bool> state(n + 1, false);
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j += i) {
state[j] = !state[j];
}
}
int ans = 0;
for (int i = 1; i <=n; i++) {
if (state[i]) {
ans++;
}
}
return ans;
}
};
int main() {
Solution sol;
for (int i = 1; i < 30; i++) {
cout << i << " " << sol.bulbSwitch(i) << endl;
cout << i << " " << sol.bulbSwitchBruteForce(i) << endl;
}
return 0;
}