Skip to content

Commit 6fc774b

Browse files
committed
update 双周赛12 临时文件
1 parent 2e5eb15 commit 6fc774b

File tree

6 files changed

+236
-20
lines changed

6 files changed

+236
-20
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "public.h"
2+
3+
//
4+
//±©Á¦Ñݱä
5+
6+
class Solution {
7+
public:
8+
vector<int> transformArray(vector<int>& arr) {
9+
while (true)
10+
{
11+
vector<int> temp = arr;
12+
temp[0] = arr[0];
13+
for (int i = 1; i < arr.size() - 1; ++i)
14+
{
15+
if ((arr[i] > arr[i - 1]) && (arr[i] > arr[i + 1]))
16+
{
17+
temp[i] = arr[i] - 1;
18+
}
19+
else if ((arr[i] < arr[i - 1]) && (arr[i] < arr[i + 1]))
20+
{
21+
temp[i] = arr[i] + 1;
22+
}
23+
else temp[i] = arr[i];
24+
}
25+
temp[arr.size() - 1] = arr[arr.size() - 1];
26+
if (temp == arr) break;
27+
else arr = temp;
28+
}
29+
return arr; //dump
30+
}
31+
};
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "public.h"
2+
3+
//
4+
//±©Á¦
5+
6+
class Leaderboard {
7+
private:
8+
unordered_map<int, int> um;
9+
multiset<int, greater<int>> ms;
10+
11+
public:
12+
Leaderboard() {
13+
;
14+
}
15+
16+
void addScore(int playerId, int score) {
17+
if (um.find(playerId) == um.end())
18+
{
19+
um[playerId] = score;
20+
ms.insert(score);
21+
}
22+
else
23+
{
24+
ms.erase(ms.find(um[playerId]));
25+
um[playerId] += score;
26+
ms.insert(um[playerId]);
27+
}
28+
}
29+
30+
int top(int K) {
31+
int res = 0;
32+
multiset<int, greater<int>>::iterator iter = ms.begin();
33+
for (int i = 0; i < K; ++i)
34+
{
35+
res += *(iter);
36+
iter++;
37+
}
38+
return res;
39+
}
40+
41+
void reset(int playerId) {
42+
ms.erase(ms.find(um[playerId]));
43+
um.erase(playerId);
44+
}
45+
};
46+
47+
/**
48+
* Your Leaderboard object will be instantiated and called as such:
49+
* Leaderboard* obj = new Leaderboard();
50+
* obj->addScore(playerId,score);
51+
* int param_2 = obj->top(K);
52+
* obj->reset(playerId);
53+
*/
54+
55+
/*
56+
int main()
57+
{
58+
Leaderboard* obj = new Leaderboard();
59+
obj->addScore(1, 73);
60+
cout << obj->top(1) << endl;
61+
obj->reset(1);
62+
}
63+
*/
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include "public.h"
2+
3+
//
4+
//拓扑排序, BFS
5+
6+
class Solution {
7+
public:
8+
int treeDiameter(vector<vector<int>>& edges) {
9+
if (edges.empty()) return 0;
10+
11+
12+
int numEdges = edges.size();
13+
//拓扑排序
14+
struct Node { int indegree = 0; unordered_set<int> nexts = {}; }; //包含入度以及下一节点,值就是vector的下标
15+
16+
//init
17+
queue<int> q;
18+
vector<Node> courses(numEdges + 1, Node());
19+
20+
for (auto& edge : edges)
21+
{
22+
courses[edge[1]].nexts.insert(edge[0]);
23+
courses[edge[0]].indegree++;
24+
courses[edge[0]].nexts.insert(edge[1]);
25+
courses[edge[1]].indegree++;
26+
}
27+
for (int i = 0; i <= numEdges; i++)
28+
if (courses[i].indegree == 1) q.push(i);
29+
30+
int time = 0;
31+
32+
//拆分节点
33+
while (!q.empty())
34+
{
35+
if (q.size() == 1) return 2 * time;
36+
else
37+
{
38+
queue<int> next;
39+
time++;
40+
while (!q.empty())
41+
{
42+
int prenodeid = q.front();
43+
q.pop();
44+
if (!courses[prenodeid].nexts.empty())
45+
{
46+
int nei = *courses[prenodeid].nexts.begin();
47+
--courses[nei].indegree;
48+
courses[nei].nexts.erase(prenodeid);
49+
if (courses[nei].indegree == 1) next.push(nei);
50+
}
51+
}
52+
q = next;
53+
}
54+
}
55+
56+
return 2 * time - 1;
57+
}
58+
};
59+
60+
/*
61+
int main()
62+
{
63+
Solution* s = new Solution();
64+
vector<vector<int>> edges0 = { {0, 1}, { 1,2 }, { 0,3 }, { 3,4 }, { 2,5 }, { 3,6 } };
65+
cout << s->treeDiameter(edges0) << endl;
66+
67+
68+
vector<vector<int>> edges = { {0,1},{0,2} };
69+
cout << s->treeDiameter(edges) << endl;
70+
71+
vector<vector<int>> edges2 = { {0,1},{1,2},{2,3},{1,4},{4,5} };
72+
cout << s->treeDiameter(edges2) << endl;
73+
return 0;
74+
}
75+
*/
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include "public.h"
2+
3+
//
4+
//混合dp
5+
//dp[i][j] 删掉它们用的最小次数
6+
7+
//dp[i][j]等于多个内部分两块然后切开
8+
//如果arr[i] == arr[j] 那么 dp[i][j] = min(dp[i - 1][j - 1], dp[i][j])
9+
10+
//初始化dp[i][i] = 1
11+
// dp[i][i + 1] = 2 如果arr[i] != arr[i + 1] 否则 1
12+
13+
class Solution {
14+
public:
15+
int minimumMoves(vector<int>& arr) {
16+
int aSize = arr.size();
17+
vector<vector<int>> dp(aSize, vector<int>(aSize, INT_MAX));
18+
19+
for (int i = 0; i < aSize; ++i)
20+
dp[i][i] = 1;
21+
22+
for (int i = 0; i < aSize - 1; ++i)
23+
{
24+
if (arr[i] == arr[i + 1]) dp[i][i + 1] = 1;
25+
else dp[i][i + 1] = 2;
26+
}
27+
28+
for (int len = 3; len <= aSize; ++len)
29+
{
30+
for (int left = 0; left <= aSize - len; ++left)
31+
{
32+
int right = left + len - 1;
33+
for (int mid = left; mid < right; ++mid)
34+
{
35+
dp[left][right] = min(dp[left][right], dp[left][mid] + dp[mid + 1][right]);
36+
}
37+
if (arr[left] == arr[right])
38+
dp[left][right] = min(dp[left][right], dp[left + 1][right - 1]);
39+
}
40+
}
41+
return dp[0][aSize - 1];
42+
}
43+
};
44+
45+
/*
46+
int main()
47+
{
48+
Solution* s = new Solution();
49+
vector<int> arr = { 1,2 };
50+
cout << s->minimumMoves(arr) << endl; //2
51+
vector<int> arr2 = { 1,3,4,1,5 };
52+
cout << s->minimumMoves(arr2) << endl; //3
53+
vector<int> arr3 = { 2,5,1,3,4,1,6,5 };
54+
cout << s->minimumMoves(arr3) << endl; //4
55+
vector<int> arr4= { 17,6,5,18,17,4,18,8,16,8,12,1,5,14,14,6,17,18,2,19,11,15,8,18,7,8,20,2,10,3,18,17,18,18,8,9,20,3,16,19,6,9,8,8,16,19,13,8,5,20 };
56+
cout << s->minimumMoves(arr4) << endl; //25
57+
return 0;
58+
}
59+
*/

cpp/leetcode/leetcode.vcxproj

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -413,13 +413,10 @@
413413
<ClCompile Include="470. implement-rand10-using-rand7.cpp" />
414414
<ClCompile Include="472. concatenated-words.cpp" />
415415
<ClCompile Include="473. Matchsticks to Square.cpp" />
416-
<ClCompile Include="5230. check-if-it-is-a-straight-line.cpp" />
417-
<ClCompile Include="5231. remove-sub-folders-from-the-filesystem.cpp" />
418-
<ClCompile Include="5232. replace-the-substring-for-balanced-string.cpp" />
419-
<ClCompile Include="5233. maximum-profit-in-job-scheduling.cpp" />
420-
<ClCompile Include="5239. circular-permutation-in-binary-representation.cpp" />
421-
<ClCompile Include="5240. maximum-length-of-a-concatenated-string-with-unique-characters.cpp" />
422-
<ClCompile Include="5241. tiling-a-rectangle-with-the-fewest-squares.cpp" />
416+
<ClCompile Include="5096. array-transformation.cpp" />
417+
<ClCompile Include="5097. design-a-leaderboard.cpp" />
418+
<ClCompile Include="5098. tree-diameter.cpp" />
419+
<ClCompile Include="5115. palindrome-removal.cpp" />
423420
<ClCompile Include="摩尔投票法升级229. majority-element-ii.cpp" />
424421
<ClCompile Include="230. kth-smallest-element-in-a-bst.cpp" />
425422
<ClCompile Include="231. power-of-two.cpp" />

cpp/leetcode/leetcode.vcxproj.filters

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,25 +1166,16 @@
11661166
<ClCompile Include="472. concatenated-words.cpp">
11671167
<Filter>源文件\HardAlg\前缀树</Filter>
11681168
</ClCompile>
1169-
<ClCompile Include="5230. check-if-it-is-a-straight-line.cpp">
1169+
<ClCompile Include="5097. design-a-leaderboard.cpp">
11701170
<Filter>源文件</Filter>
11711171
</ClCompile>
1172-
<ClCompile Include="5231. remove-sub-folders-from-the-filesystem.cpp">
1172+
<ClCompile Include="5096. array-transformation.cpp">
11731173
<Filter>源文件</Filter>
11741174
</ClCompile>
1175-
<ClCompile Include="5232. replace-the-substring-for-balanced-string.cpp">
1175+
<ClCompile Include="5098. tree-diameter.cpp">
11761176
<Filter>源文件</Filter>
11771177
</ClCompile>
1178-
<ClCompile Include="5233. maximum-profit-in-job-scheduling.cpp">
1179-
<Filter>源文件</Filter>
1180-
</ClCompile>
1181-
<ClCompile Include="5239. circular-permutation-in-binary-representation.cpp">
1182-
<Filter>源文件</Filter>
1183-
</ClCompile>
1184-
<ClCompile Include="5240. maximum-length-of-a-concatenated-string-with-unique-characters.cpp">
1185-
<Filter>源文件</Filter>
1186-
</ClCompile>
1187-
<ClCompile Include="5241. tiling-a-rectangle-with-the-fewest-squares.cpp">
1178+
<ClCompile Include="5115. palindrome-removal.cpp">
11881179
<Filter>源文件</Filter>
11891180
</ClCompile>
11901181
</ItemGroup>

0 commit comments

Comments
 (0)