Rank Name Score Finish Time Q1 (3) Q2 (4) Q3 (5) Q4 (6)
393 / 9769 YoungForest 18 1:36:40 0:05:13 0:11:24 0:44:59 1:26:40 2

残酷群排名维持在14名了,看来这就是我的水平收敛的位置了。最近由于写毕业大论文比较忙,另一方面秋招也结束了,打卡题都没打了。11月才恢复开始打美服和国服每日一题,拿积分换衣服。残酷群由于基本可以免打卡,就一个月都没打了。

5561. Get Maximum in Generated Array

签到题。按照递推公式填写每一个元素。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
int getMaximumGenerated(int n) {
vector<int> nums(n + 1);
int ans = 0;
nums[0] = 0;
if (nums.size() <= 1) return 0;
nums[1] = 1;
ans = 1;
for (int i = 2; i <= n; ++i) {
if (i % 2 == 0) {
nums[i] = nums[i / 2];
} else {
nums[i] = nums[i / 2] + nums[i / 2 + 1];
}
ans = max(ans, nums[i]);
}
return ans;
}
};

时间复杂度: O(N),
空间复杂度: O(N).

5562. Minimum Deletions to Make Character Frequencies Unique

贪心。不断删除字符,直到有空缺的位置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
int minDeletions(string s) {
vector<int> cnt(26, 0);
for (char c : s) {
++cnt[c - 'a'];
}
set<int> count;
int ans = 0;
for (int a : cnt) {
if (a != 0) {
while (a != 0 && count.find(a) != count.end()) {
++ans;
--a;
}
count.insert(a);
}
}
return ans;
}
};

时间复杂度: O(N),
空间复杂度: O(26).

阅读全文 »

Rank Name Score Finish Time Q1 (3) Q2 (4) Q3 (5) Q4 (6)
139 / 10630 YoungForest 18 0:39:13 0:06:26 0:14:56 0:22:59 0:39:13

周赛已经连续4周表现良好了,开心。群排名也稳定在了15名,终究还是无法进入前10.不过我已经满意了。

5554. Check Array Formation Through Concatenation

签到题。需要注意arrpieces都是 distinct (互不相同)的。
所以,我们只需要记录pieces的开头元素对应的数组就可以了,直接找,然后匹配。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
bool canFormArray(vector<int>& arr, vector<vector<int>>& pieces) {
unordered_map<int, int> num2indexInPieces;
for (int i = 0; i < pieces.size(); ++i) {
const auto& v = pieces[i];
num2indexInPieces[v[0]] = i;
}
for (int i = 0; i < arr.size(); ) {
auto it = num2indexInPieces.find(arr[i]);
if (it == num2indexInPieces.end()) return false;
const int j = it->second;
const int oldi = i;
for (; i < arr.size() && i - oldi < pieces[j].size(); ++i) {
if (arr[i] != pieces[j][i - oldi]) return false;
}
}
return true;
}
};

时间复杂度: O(arr.size() + pieces.size()),
空间复杂度: O(pieces.size()).

5555. Count Sorted Vowel Strings

DP. 定义dp(n, i)为长度为n,开头是第i大的字母所对应的字符串的数量。
dp(n, i) = sum(dp(n-1, j) for j in range(i, 0, -1)).

1
2
3
4
5
6
7
8
9
class Solution:
def countVowelStrings(self, n: int) -> int:
@lru_cache(None)
def dp(n, i):
if n == 1:
return i
else:
return sum(dp(n-1, j) for j in range(i, 0, -1))
return dp(n, 5)

时间复杂度: O(n * 5 * 5),
空间复杂度: O(n * 5).

阅读全文 »

Rank Name Score Finish Time Q1 (3) Q2 (4) Q3 (5) Q4 (6)
318 / 7446 YoungForest 18 1:07:29 0:12:38 0:16:42 0:57:29 2 0:41:26

5539. Sort Array by Increasing Frequency

签到题。按照题意先统计频数,再按频数排序即可。

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
class Solution {
public:
vector<int> frequencySort(vector<int>& nums) {
unordered_map<int, int> cnt;
for (int i : nums) {
++cnt[i];
}
using pii = pair<int,int>;
vector<pii> frequency;
for (const auto& p : cnt) {
frequency.emplace_back(p.second, p.first);
}
sort(frequency.begin(), frequency.end(), [&](const auto& a, const auto& b) -> bool {
if (a.first == b.first) {
return a.second > b.second;
} else {
return a.first < b.first;
}
});
vector<int> ans;
for (const auto& p : frequency) {
for (int i = 0; i < p.first; ++i) {
ans.push_back(p.second);
}
}
return ans;
}
};

时间复杂度: O(N log N),
空间复杂度: O(N).

5540. Widest Vertical Area Between Two Points Containing No Points

虽然看起来很复杂,但其实就是一个按x排序,找最大间隔。题目故意说难了,绕了大家一圈。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int maxWidthOfVerticalArea(vector<vector<int>>& points) {
const int n = points.size();
vector<int> x;
x.reserve(n);
for (const auto& v : points) {
x.push_back(v[0]);
}
sort(x.begin(), x.end());
int ans = 0;
int lastx = x[0];
for (int i = 1; i < n; ++i) {
ans = max(ans, x[i] - lastx);
lastx = x[i];
}
return ans;
}
};

时间复杂度: O(N log N),
空间复杂度: O(N).

5541. Count Substrings That Differ by One Character

阅读全文 »

Rank Name Score Finish Time Q1 (3) Q2 (4) Q3 (5) Q4 (6)
103 / 10984 YoungForest 19 1:19:51 0:06:36 0:12:02 0:39:44 2 1:09:51

本周周赛继续高歌猛进,排名也很靠前。加上上周的名次,我在残酷群里的排名也上升到了新高,第11名。

1629. Slowest Key

一次遍历。使用一个变量维护上次按键的时刻。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
char slowestKey(vector<int>& releaseTimes, string keysPressed) {
const int n = releaseTimes.size();
int last = 0;
int maxDuration = 0;
char ans = ' ';
for (int i = 0; i < n; ++i) {
const int duration = releaseTimes[i] - last;
if (duration > maxDuration || (duration == maxDuration && keysPressed[i] > ans)) {
maxDuration = duration;
ans = keysPressed[i];
}
last = releaseTimes[i];
}
return ans;
}
};

空间复杂度: O(1),
时间复杂度: O(n).

1630. Arithmetic Subarrays

对于每个子数组,排序判断是否为等差数列。

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
class Solution {
bool check(const vector<int>& x) {
if (x.size() < 2) return false;
const int diff = x[1] - x[0];
for (int i = 2; i < x.size(); ++i) {
if (x[i] - x[i - 1] != diff) return false;
}
return true;
}
public:
vector<bool> checkArithmeticSubarrays(vector<int>& nums, vector<int>& l, vector<int>& r) {
// time: m * n * n log n
const int m = l.size();
const int n = nums.size();
vector<bool> ans(m);
for (int i = 0; i < m; ++i) {
vector<int> cp;
cp.reserve(r[i] - l[i] + 1);
for (int j = l[i]; j <= r[i]; ++j) {
cp.push_back(nums[j]);
}
sort(cp.begin(), cp.end());
ans[i] = check(cp);
}
return ans;
}
};

时间复杂度: O(m * n * n log n),
空间复杂度: O(m).

阅读全文 »

Rank Name Score Finish Time Q1 (3) Q2 (4) Q3 (5) Q4 (6)
224 / 11960 YoungForest 18 1:23:45 0:03:19 0:31:37 1:00:04 1:18:45 1

本周题目质量还不错,而且自己排名也不错。提前10分钟AC,就是喜欢这种紧张刺激感。而像上周那样提前40minAC反而没今天这么开心。
因为连续2次周赛排名都很靠前,我残酷群的排名也上升到15名了。久违的最高位置,继续保持。

1624. Largest Substring Between Two Equal Characters

签到题。记录每个字符的首次出现下标和最后出现下标,遍历做差即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int maxLengthBetweenEqualCharacters(string s) {
using pii = pair<int, int>;
vector<pii> index(26, {-1, -1});
for (int i = 0; i < s.size(); ++i) {
const int idx = s[i] - 'a';
if (index[idx].first == -1) {
index[idx].first = i;
}
index[idx].second = i;
}
int ans = -1;
for (auto p : index) {
ans = max(ans, p.second - p.first - 1);
}
return ans;
}
};

时间复杂度: O(s.size()),
空间复杂度: O(1).

1625. Lexicographically Smallest String After Applying Operations

第二题挺恶心,暴力就行,但实现起来还是比较复杂度的。写了50行代码,没成想竟然一次bug-free。
因为数据规模很小,100,brute force搞起.

先找到所有通过shift可以作为开头的下标。shift最为一个子问题也是LeetCode的原题,通过3次reverse可以方便的实现。
然后就不需要考虑shift操作了,只需要考虑增加操作。贪心即可。这时根据b是奇偶,可能有2或1位要增加。
最后找到所有下标可以达到的最小值即可。

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
class Solution {
public:
string findLexSmallestString(string s, int a, int b) {
const int n = s.size();
vector<int> head;
bool first = true;
for (int i = 0; ; ++i) {
const int condidate = ((n - i * b) % n + n) % n;
if (condidate == 0) {
if (!first) break;
first = false;
}
head.push_back(condidate);
}
string ans(n, '9');
auto shiftK = [&](string& a, const int k) -> void {
// shift left k unit
reverse(a.begin(), a.end());
auto it = a.begin() + k;
reverse(a.begin(), it);
reverse(it, a.end());
};
auto minIncrease = [&](const int x) -> int {
map<int, int> reach;
int i = x;
int ans = 0;
while (reach.find(i) == reach.end()) {
reach[i] = ans;
i = (i + a) % 10;
ans += a;
}
return reach.begin()->second;
};
auto fixHead = [&](const int d) -> string {
string ans = s;
shiftK(ans, d);
if (b % 2 != 0) {
const int increase = minIncrease(ans[0] - '0');
for (int i = 0; i < n; i += 2) {
ans[i] = '0' + ((ans[i] - '0' + increase) % 10);
}
}
const int increase = minIncrease(ans[1] - '0');
for (int i = 1; i < n; i += 2) {
ans[i] = '0' + ((ans[i] - '0' + increase) % 10);
}
return ans;
};
for (const int d : head) {
const string x = fixHead(d);
if (x < ans) {
ans = x;
}
}
return ans;
}
};
阅读全文 »

Rank Name Score Finish Time Q1 (3) Q2 (4) Q3 (5) Q4 (7)
893 / 8250 YoungForest 7 0:27:31 0:11:24 0:27:31 null null

连续2次双周赛遭遇滑铁卢了。
第3题在赛后2分钟通过了,本来是在能力范围内的题目,但最后心太急了。本来晚上状态就不好,反而是比赛结束后,就写出来了。

1619. Mean of Array After Removing Some Elements

签到题。先排序,后求和,在求平均。
这里需要注意题目中限制了arr.size() % 20 == 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
double trimMean(vector<int>& arr) {
sort(arr.begin(), arr.end());
const int n = arr.size();
const int x = n / 20;
double s = 0.0;
for (int i = 0; i < n; ++i) {
if (i < x || (n - 1 - i) < x) continue;
s += arr[i];
}
return s / (n - 2 * x);
}
};

时间复杂度: O(N log N),
空间复杂度: O(1).

1620. Coordinate With Maximum Network Quality

由于数据范围比较小,只有50,直接暴力。遍历每一个位置,遍历每个塔求它的信号。

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
class Solution {
public:
vector<int> bestCoordinate(vector<vector<int>>& towers, int radius) {
// 50 * 50 * 50
const double r = radius;
int maxSignal = 0;
int maxI = 0;
int maxJ = 0;
auto distance = [&](const int a1, const int b1, const int a2, const int b2) -> double {
return sqrt((a1 - a2) * (a1 - a2) + (b1 - b2) * (b1 - b2));
};
auto getSignal = [&](const int i, const int j) -> int {
int ans = 0;
for (const auto& t : towers) {
double d = distance(t[0], t[1], i, j);
if (d > r) continue;
ans += static_cast<int>(floor(t[2] / (1 + d)));
}
return ans;
};
for (int i = 0; i <= 50; ++i) {
for (int j = 0; j <= 50; ++j) {
const int signal = getSignal(i, j);
// cout << i << " " << j << " " << signal << endl;
if (signal > maxSignal) {
maxI = i;
maxJ = j;
maxSignal = signal;
}
}
}
return {maxI, maxJ};
}
};

时间复杂度: O(rows * cols * towers),
空间复杂度: O(1).

阅读全文 »

Rank Name Score Finish Time Q1 (3) Q2 (4) Q3 (5) Q4 (6)
94 / 11792 YoungForest 18 0:51:30 0:03:13 0:08:36 0:21:59 0:51:30

本场比赛都是常规题目,我没有遇到困难,久违地进入了前100名。太难了,残酷群排名也因此上升到25名。

5535. Maximum Nesting Depth of the Parentheses

签到题。括号嵌套层数,用栈的思路即可。左括号入栈,右括号出栈。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int maxDepth(string s) {
int left = 0;
int ans = 0;
for (char c : s) {
if (c == '(') {
++left;
} else if (c == ')') {
--left;
}
ans = max(ans, left);
}
return ans;
}
};

时间复杂度: O(N),
空间复杂度: O(1).

5536. Maximal Network Rank

暴力法。枚举所有的城市对,计算network rank。有个技巧是先统计单个城市的度,如果2城市间存在道路则减一。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
int maximalNetworkRank(int n, vector<vector<int>>& roads) {
using pii = pair<int, int>;
set<pii> roadmap;
vector<int> cnt(n);
for (const auto& r : roads) {
roadmap.insert({r[0], r[1]});
++cnt[r[0]];
++cnt[r[1]];
}
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int sub = 0;
if (roadmap.find({i, j}) != roadmap.end() || roadmap.find({j, i}) != roadmap.end()) {
++sub;
}
ans = max(ans, cnt[i] + cnt[j] - sub);
}
}
return ans;
}
};

时间复杂度: O(n^2 * log m + m log m),
空间复杂度: O(n + m).

阅读全文 »

Rank Name Score Finish Time Q1 (3) Q2 (4) Q3 (5) Q4 (6)
522 / 12138 YoungForest 12 0:49:07 0:05:12 0:11:28 0:39:07 2 null

1608. Special Array With X Elements Greater Than or Equal X

签到题。从小到大枚举可能的答案,进行检查。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int specialArray(vector<int>& nums) {
sort(nums.begin(), nums.end());
const int n = nums.size();
auto check = [&](const int i) -> bool {
auto it = lower_bound(nums.begin(), nums.end(), i);
const int d = distance(nums.begin(), it);
const int le = n - d;
return le == i;
};
for (int i = 1; i <= 100; ++i) {
if (check(i)) {
return i;
}
}
return -1;
}
};

时间复杂度: O(N * log N),

1609. Even Odd Tree

二叉树的层序遍历。使用广度优先搜索(BFS),记录层数和前一个元素即可。

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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isEvenOddTree(TreeNode* root) {
// bfs
queue<TreeNode*> q;
int level = 0;
if (root) q.push(root);
while (!q.empty()) {
const int s = q.size();
int last;
if (level % 2 == 0) last = -1;
else last = 1'000'006;
for (int i = 0; i < s; ++i) {
auto current = q.front();
q.pop();
if (level % 2 == 0) {
if (current->val % 2 == 0) return false;
if (current->val <= last) return false;
} else {
if (current->val % 2 == 1) return false;
if (current->val >= last) return false;
}
last = current->val;
if (current->left) q.push(current->left);
if (current->right) q.push(current->right);
}
++level;
}
return true;
}
};

时间复杂度: O(N),
空间复杂度: O(N).

1610. Maximum Number of Visible Points

阅读全文 »

Rank Name Score Finish Time Q1 (3) Q2 (4) Q3 (6) Q4 (7)
1667 / 8332 YoungForest 7 0:15:37 0:01:46 0:15:37 null null

这次双周赛有跪了,生活真是起起落落落落…三四题其实还是有机会做出来的,但比赛时状态不好,决策有失误。在第3题看了2分钟没思路时转到第四题了,然后第四题想复杂了,实现花了不少时间,最后还是被卡时间TLE了。

1603. Design Parking System

签到题。维护各个类型剩余车位数即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class ParkingSystem {
array<int, 4> cnt;
public:
ParkingSystem(int big, int medium, int small) {
cnt[1] = big;
cnt[2] = medium;
cnt[3] = small;
}

bool addCar(int carType) {
if (cnt[carType] > 0) {
--cnt[carType];
return true;
} else return false;
}
};

/**
* Your ParkingSystem object will be instantiated and called as such:
* ParkingSystem* obj = new ParkingSystem(big, medium, small);
* bool param_1 = obj->addCar(carType);
*/

时间复杂度: O(addCar calls),
空间复杂度: O(1).

1604. Alert Using Same Key-Card Three or More Times in a One Hour Period

先按时间进行排序,然后依次遍历,记录每个人进入的时间,检查最后3次进入是否处于同一个小时。

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
class Solution {
public:
vector<string> alertNames(vector<string>& keyName, vector<string>& keyTime) {
using pss = pair<string, string>;
set<string> ans;
const int n = keyName.size();
vector<pss> records;
records.reserve(n);
for (int i = 0; i < n; ++i) {
records.emplace_back(keyTime[i], keyName[i]);
}
sort(records.begin(), records.end());
int hour = 0;
unordered_map<string, vector<string>> enter;
auto convert2int = [&](const string& x) -> int {
int h = stoi(x.substr(0, 2));
int m = stoi(x.substr(3));
return h * 60 + m;
};
auto check = [&](const string& x) -> bool {
if (enter[x].size() < 3) return false;
else {
int a = convert2int(enter[x][enter[x].size() - 1]);
int b = convert2int(enter[x][enter[x].size() - 2]);
int c = convert2int(enter[x][enter[x].size() - 3]);
return a - c <= 60;
}
};
for (int i = 0; i < n; ++i) {
enter[records[i].second].push_back(records[i].first);
if (check(records[i].second)) {
ans.insert(records[i].second);
}
}
return {ans.begin(), ans.end()};
}
};

时间复杂度: O(N log N * nameLength),
空间复杂度: O(N).

阅读全文 »

距上次写周赛总结已经过去3个半月了。坚持了半年的写周赛题解中断确实挺可惜的,但却是当时的不得已之举。7月份一直在忙小论文的事儿,8月份接着就是开题和中期,9月份正式开学,并且从7月初就在Amazon开始了暑期实习。任务确实比之前要多,当时因为事务压身,感觉精力不足以把所有事都做好。因为每次周赛写题解都要花大半天的时间,再加上打周赛,基本1天时间。打比赛和写题解对精力的损耗也是不言而喻的。虽然这3个月题解断更了,但比赛还是在照常的打,毕竟加入了残酷刷题群,有更多的人一起打周赛,每周打比赛的反馈和热爱也更强了。
这3个月,我残酷群的排名也是起起落落落落落…最好时有15名,最差已经90名了。总的感觉是,自己更擅长常规不难的题目。遇到之前没见过或做的不多的就很容易最后一题做不出来。

昨天和Amazon的manager确认了转正事宜,之后基本上只用走流程了。有了理想offer后,秋招不知所措的弦终于可以放松下来了。有心情花时间做自己想做的事情了,读几本之前一直想看的书,认真打几场比赛,恢复写题解的习惯。当然实习和毕设大论文才是当前的主要任务,需要投入大部分时间。但终于有精力happy了。

这3个月来,因为国服赞助商礼物比较丰富,我比赛已经从美服转战国服了。效果还不错,除了coins奖励更容易拿到外,实体奖励也是很香的。有幸2次进入前50名,拿到一个帆布包和一个小米无线鼠标。

Rank Name Score Finish Time Q1 (3) Q2 (4) Q3 (5) Q4 (6)
392 / 11499 YoungForest 18 1:04:46 0:28:38 0:40:28 1:04:46 0:55:59

事实上,由于要处理突发的培养方案问题,我还迟到了25min,否则成绩会更好看。

本次比赛被大家吐槽为阅读理解,读题难、题目描述迷惑。确实如此。

1598. Crawler Log Folder

签到题。只需要关心向下一层和向上一层即可。可以用一个变量维护,在比赛中,我是用了一个栈去做,实际上是大材小用了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int minOperations(vector<string>& logs) {
stack<string> st;
for (auto& s : logs) {
if (s == "../") {
if (!st.empty()) {
st.pop();
}
} else if (s == "./") {
continue;
} else {
s.pop_back();
st.push(s);
}
}
return st.size();
}
};

时间复杂度: O(N),
空间复杂度: O(N).

阅读全文 »
0%