Skip to content

Commit fb0f94e

Browse files
committed
daily update
1 parent 4d48560 commit fb0f94e

File tree

5 files changed

+152
-2
lines changed

5 files changed

+152
-2
lines changed

daily/25-Feb12.cc

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <vector>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
/**
7+
* @brief Time: O(NlogU), U = max(nums)
8+
* Space: O(1)
9+
*
10+
* @param nums
11+
* @param maxOperations
12+
* @return int
13+
*/
14+
int minimumSize(vector<int>& nums, int maxOperations) {
15+
auto check = [&](int m) -> bool {
16+
long long cnt = 0;
17+
for (int x : nums) {
18+
cnt += (x - 1) / m;
19+
}
20+
return cnt <= maxOperations;
21+
};
22+
23+
int left = 0;
24+
int right = ranges::max(nums);
25+
while (left + 1 < right) {
26+
int mid = left + (right - left) / 2;
27+
(check(mid) ? right : left) = mid;
28+
}
29+
return right;
30+
}
31+
};

daily/25-Feb15.cc

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <vector>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
/**
7+
* @brief 1706: Where will the ball fall
8+
* Time: O(row * col), Space: O(1)
9+
*
10+
* @param grid
11+
* @return vector<int>
12+
*/
13+
vector<int> findBall(vector<vector<int>>& grid) {
14+
int n = grid[0].size();
15+
vector<int> ans(n);
16+
for (int j = 0; j < n; j++) {
17+
int cur_col = j;
18+
for (auto& row : grid) {
19+
int d = row[cur_col];
20+
cur_col += d;
21+
22+
if (cur_col < 0 || cur_col == n || row[cur_col] != d) {
23+
cur_col = -1;
24+
break;
25+
}
26+
}
27+
ans[j] = cur_col;
28+
}
29+
return ans;
30+
}
31+
};

daily/25-Feb19.cc

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <unordered_map>
2+
#include <vector>
3+
using namespace std;
4+
5+
class RangeFreqQuery {
6+
unordered_map<int, vector<int>> pos;
7+
8+
public:
9+
RangeFreqQuery(vector<int>& arr) {
10+
for (int i = 0; i < arr.size(); i++) {
11+
pos[arr[i]].push_back(i);
12+
}
13+
}
14+
15+
int query(int left, int right, int value) {
16+
auto it = pos.find(value);
17+
if (it == pos.end()) { // not found
18+
return 0;
19+
}
20+
21+
auto& a = it->second;
22+
return ranges::upper_bound(a, right) - ranges::lower_bound(a, left);
23+
}
24+
};
25+
26+
class Solution {
27+
public:
28+
/**
29+
* @brief LC 624: Max Distance in Arrays
30+
* Time: O(len), Space: O(1)
31+
*
32+
* @param arrays
33+
* @return int
34+
*/
35+
int maxDistance(vector<vector<int>>& arrays) {
36+
int ans = 0;
37+
int mn = INT_MAX / 2, mx = INT_MIN / 2;
38+
39+
for (auto& array : arrays) {
40+
ans = max({ans, array.back() - mn, mx - array[0]});
41+
mn = min(mn, array[0]);
42+
mx = max(mx, array.back());
43+
}
44+
45+
return ans;
46+
}
47+
};

daily/25-Feb20.cc

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <vector>
2+
using namespace std;
3+
4+
class Solution {
5+
enum Bits {
6+
bit0 = 0b0000000001,
7+
bit1 = 0b0000000010,
8+
bit2 = 0b0000000100,
9+
bit3 = 0b0000001000,
10+
bit4 = 0b0000010000,
11+
bit5 = 0b0000100000,
12+
bit6 = 0b0001000000,
13+
bit7 = 0b0010000000,
14+
bit8 = 0b0100000000,
15+
bit9 = 0b1000000000,
16+
};
17+
18+
public:
19+
/**
20+
* @brief LC2595: Number of Even and Odd Bits
21+
* Time: O(1), Space: O(1)
22+
* Bit operation is really fast, haha <3
23+
*
24+
* @param n
25+
* @return vector<int>
26+
*/
27+
vector<int> evenOddBit(int n) {
28+
vector<int> ans(2, 0);
29+
Bits mybits[] = {bit0, bit1, bit2, bit3, bit4, bit5, bit6, bit7, bit8, bit9};
30+
int sentinel = 0;
31+
for (auto bit : mybits) {
32+
if ((bit & n) != 0 && sentinel % 2 == 0) {
33+
ans[0]++;
34+
} else if ((bit & n) != 0 && sentinel % 2 == 1) {
35+
ans[1]++;
36+
}
37+
sentinel++;
38+
}
39+
return ans;
40+
}
41+
};

daily/Aug18.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ class Solution {
1111
* @return true, false
1212
*/
1313
bool checkRecord(string s) {
14-
return ranges::counts(s, 'A') && s.find("LLL") == string::npos;
14+
return ranges::count(s, 'A') && s.find("LLL") == string::npos;
1515
}
16-
};
16+
};

0 commit comments

Comments
 (0)