Skip to content

Commit 475613c

Browse files
committed
update leetcode
1 parent db6da9e commit 475613c

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
bool hasAlternatingBits(int n) {
4+
int end = 0;
5+
int nn = n;
6+
while (nn > 0) {
7+
nn -= nn & (1 << end);
8+
++end;
9+
}
10+
11+
for (int i = end - 1; i >= 1; --i) {
12+
bool high_bit = (n & (1 << i)) > 0;
13+
bool low_bit = (n & (1 << (i - 1))) > 0;
14+
if (high_bit == low_bit) {
15+
return false;
16+
}
17+
}
18+
return true;
19+
}
20+
};

leetcode/EmployeeImportance.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
// Definition for Employee.
3+
class Employee {
4+
public:
5+
int id;
6+
int importance;
7+
vector<int> subordinates;
8+
};
9+
*/
10+
11+
class Solution {
12+
public:
13+
int getImportance(vector<Employee*> employees, int id) {
14+
map<int, Employee*> id_to_emp;
15+
for (auto& employee : employees) {
16+
id_to_emp[employee->id] = employee;
17+
}
18+
int res = 0;
19+
queue<Employee*> q;
20+
if (id_to_emp.find(id) != id_to_emp.end()) {
21+
q.push(id_to_emp[id]);
22+
}
23+
24+
while (!q.empty()) {
25+
Employee* tp = q.front();
26+
q.pop();
27+
res += tp->importance;
28+
for (auto& sub_id : tp->subordinates) {
29+
q.push(id_to_emp[sub_id]);
30+
}
31+
}
32+
return res;
33+
}
34+
};

leetcode/MaxAreaOfIsland.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define FOR(i, n) for (int i = 0; i < (int)n; ++i)
4+
typedef long long ll;
5+
typedef pair<int, int> pii;
6+
7+
8+
int dir_x[] = {0, 0, 1, -1};
9+
int dir_y[] = {1, -1, 0, 0};
10+
11+
class Solution {
12+
private:
13+
vector<vector<bool> > visited;
14+
int bfs(vector<vector<int> >& grid, int r, int c) {
15+
queue<pii> q;
16+
q.push({r, c});
17+
visited[r][c] = true;
18+
int area = 1;
19+
20+
while (!q.empty()) {
21+
pii tp = q.front();
22+
q.pop();
23+
FOR(i, 4) {
24+
int nr = tp.first + dir_x[i];
25+
int nc = tp.second + dir_y[i];
26+
if (nr < 0 || nr >= grid.size() || nc < 0 || nc >= grid[0].size()) continue;
27+
if (visited[nr][nc] || grid[nr][nc] == 0) continue;
28+
visited[nr][nc] = true;
29+
q.push({nr, nc});
30+
++area;
31+
}
32+
}
33+
return area;
34+
}
35+
public:
36+
int maxAreaOfIsland(vector<vector<int> >& grid) {
37+
int row = grid.size();
38+
int col = grid[0].size();
39+
this->visited = vector<vector<bool> >(row, vector<bool>(col, false));
40+
41+
int res = 0;
42+
FOR(i, row) FOR(j, col) {
43+
if (visited[i][j] || grid[i][j] == 0) continue;
44+
res = max(res, bfs(grid, i, j));
45+
}
46+
return res;
47+
}
48+
};

leetcode/TopKFrequentWords.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
vector<string> topKFrequent(vector<string>& words, int k) {
4+
map<string, int> word_cnt;
5+
for (auto& w : words) {
6+
++word_cnt[w];
7+
}
8+
9+
vector<pair<int, string> > freq_word_pairs;
10+
for (auto& it : word_cnt) {
11+
freq_word_pairs.push_back({-it.second, it.first});
12+
}
13+
sort(freq_word_pairs.begin(), freq_word_pairs.end());
14+
15+
vector<string> res;
16+
for (int i = 0; i < k; ++i) {
17+
res.push_back(freq_word_pairs[i].second);
18+
}
19+
return res;
20+
}
21+
};

0 commit comments

Comments
 (0)