Skip to content

Commit ae6e07d

Browse files
committed
update 双周赛11场临时文件
1 parent d88dde3 commit ae6e07d

File tree

7 files changed

+270
-2
lines changed

7 files changed

+270
-2
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "public.h"
2+
3+
//
4+
//±©Á¦, ͳ¼Ædelta
5+
6+
class Solution {
7+
public:
8+
int missingNumber(vector<int>& arr) {
9+
int delta1 = 0;
10+
int delta2 = 0;
11+
12+
for (int index = 1; index < arr.size(); ++index)
13+
{
14+
if (delta1 == 0)
15+
{
16+
delta1 = arr[index] - arr[index - 1];
17+
}
18+
else if (delta1 == (arr[index] - arr[index - 1]))
19+
{
20+
continue;
21+
}
22+
else if (delta2 == 0)
23+
{
24+
delta2 = arr[index] - arr[index - 1];
25+
}
26+
else
27+
{
28+
continue;
29+
}
30+
}
31+
32+
int targetdel;
33+
if (delta1 == 2 * delta2) targetdel = delta2;
34+
else targetdel = delta1;
35+
36+
for (int index = 1; index < arr.size(); ++index)
37+
{
38+
if ((arr[index] - arr[index - 1]) != targetdel)
39+
{
40+
return arr[index - 1] + targetdel;
41+
}
42+
}
43+
return arr[0];
44+
}
45+
};
46+
47+
/*
48+
int main()
49+
{
50+
Solution* s = new Solution();
51+
vector<int> arr = { 5,7,11,13 };
52+
cout << s->missingNumber(arr);
53+
return 0;
54+
}
55+
*/
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include "public.h"
2+
3+
//
4+
//类似滑动窗口, 双指针
5+
//先排序
6+
7+
class Solution {
8+
private:
9+
static bool msort(vector<int> const& v1, vector<int> const& v2)
10+
{
11+
if (v1[0] < v2[0]) return true;
12+
else if (v1[0] > v2[0]) return false;
13+
else return v1[1] < v2[1];
14+
}
15+
16+
public:
17+
vector<int> minAvailableDuration(vector<vector<int>>& slots1, vector<vector<int>>& slots2, int duration) {
18+
int s1Index = 0;
19+
int s2Index = 0;
20+
21+
int slots1Size = slots1.size();
22+
int slots2Size = slots2.size();
23+
24+
//排序
25+
sort(slots1.begin(), slots1.end(), msort);
26+
sort(slots2.begin(), slots2.end(), msort);
27+
28+
while (s1Index < slots1Size && s2Index < slots2Size)
29+
{
30+
if (slots1[s1Index][0] >= slots2[s2Index][1])
31+
{
32+
s2Index++;
33+
}
34+
else if (slots2[s2Index][0] >= slots1[s1Index][1])
35+
{
36+
s1Index++;
37+
}
38+
else
39+
{
40+
if (slots1[s1Index][0] <= slots2[s2Index][0]) //以slots2[s2Index][0]为起始
41+
{
42+
if (slots1[s1Index][1] <= slots2[s2Index][1]) //以slots1[s1Index][1]为结束
43+
{
44+
if ((slots1[s1Index][1] - slots2[s2Index][0]) >= duration)
45+
{
46+
return vector<int>{slots2[s2Index][0], slots2[s2Index][0] + duration};
47+
}
48+
else
49+
{
50+
s1Index++;
51+
}
52+
}
53+
else //以slots2[s2Index][1]为结束
54+
{
55+
if ((slots2[s2Index][1] - slots2[s2Index][0]) >= duration)
56+
{
57+
return vector<int>{slots2[s2Index][0], slots2[s2Index][0] + duration};
58+
}
59+
else
60+
{
61+
s2Index++;
62+
}
63+
}
64+
}
65+
else //以slots1[s1Index][0]为起始
66+
{
67+
if (slots1[s1Index][1] <= slots2[s2Index][1]) //以slots1[s1Index][1]为结束
68+
{
69+
if ((slots1[s1Index][1] - slots1[s1Index][0]) >= duration)
70+
{
71+
return vector<int>{slots1[s1Index][0], slots1[s1Index][0] + duration};
72+
}
73+
else
74+
{
75+
s1Index++;
76+
}
77+
}
78+
else //以slots2[s2Index][1]为结束
79+
{
80+
if ((slots2[s2Index][1] - slots1[s1Index][0]) >= duration)
81+
{
82+
return vector<int>{slots1[s1Index][0], slots1[s1Index][0] + duration};
83+
}
84+
else
85+
{
86+
s2Index++;
87+
}
88+
}
89+
}
90+
}
91+
}
92+
return vector<int>{};
93+
}
94+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include "public.h"
2+
3+
//
4+
//dp?
5+
//dp[i][j]: 抛了i枚硬币之后值是j的情况
6+
7+
class Solution {
8+
public:
9+
double probabilityOfHeads(vector<double>& prob, int target) {
10+
int pSize = prob.size();
11+
12+
vector<vector<double>> dp(pSize + 1, vector<double>(target + 1, 0));
13+
dp[0][0] = 1.0;
14+
15+
for (int p = 1; p <= pSize; ++p)
16+
{
17+
dp[p][0] = dp[p - 1][0] * (1 - prob[p - 1]);
18+
for (int j = 1; j <= target; ++j)
19+
{
20+
dp[p][j] = dp[p - 1][j - 1] * prob[p - 1] + dp[p - 1][j] * (1 - prob[p - 1]);
21+
}
22+
}
23+
24+
return dp[pSize][target];
25+
}
26+
};
27+
28+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include "public.h"
2+
3+
//
4+
//¼«Ð¡»¯¼«´ó
5+
//¶þ·Ö+̰ÐÄ
6+
7+
class Solution {
8+
public:
9+
int maximizeSweetness(vector<int>& sweetness, int K) {
10+
K++;
11+
12+
int l = sweetness[0], r = 0;
13+
int n = sweetness.size();
14+
for (int i = 0; i < n; i++) {
15+
r += sweetness[i];
16+
if (l > sweetness[i]) {
17+
l = sweetness[i];
18+
}
19+
}
20+
21+
int ans = -1;
22+
while (l <= r)
23+
{
24+
int mid = (l + r) >> 1;
25+
int sum = 0;
26+
int cnt = 1;
27+
for (int i = 0; i < n; i++) {
28+
if (sum >= mid)
29+
{
30+
cnt++;
31+
sum = sweetness[i];
32+
}
33+
else{
34+
sum += sweetness[i];
35+
}
36+
}
37+
if (cnt < K) {
38+
r = mid - 1;
39+
}
40+
else if (cnt > K){
41+
ans = max(ans, mid);
42+
l = mid + 1;
43+
}
44+
else
45+
{
46+
if (sum >= mid)
47+
{
48+
ans = max(ans, mid);
49+
l = mid + 1;
50+
}
51+
else
52+
{
53+
r--;
54+
}
55+
}
56+
}
57+
return ans;
58+
}
59+
};
60+
61+
/*
62+
int main()
63+
{
64+
Solution* s = new Solution();
65+
vector<int> sweetness0 = { 90670,55382,95298,95795,73204,41464,18675,30104,47442,55307 };
66+
cout << s->maximizeSweetness(sweetness0, 6) << endl;
67+
68+
vector<int> sweetness = { 1,2,3,4,5,6,7,8,9 };
69+
cout << s->maximizeSweetness(sweetness, 5) << endl;
70+
vector<int> sweetness2 = { 5,6,7,8,9,1,2,3,4 };
71+
cout << s->maximizeSweetness(sweetness2, 8) << endl;
72+
vector<int> sweetness3 = { 1,2,2,1,2,2,1,2,2 };
73+
cout << s->maximizeSweetness(sweetness3, 2) << endl;
74+
return 0;
75+
}
76+
*/

cpp/leetcode/leetcode.vcxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,10 @@
412412
<ClCompile Include="470. implement-rand10-using-rand7.cpp" />
413413
<ClCompile Include="472. concatenated-words.cpp" />
414414
<ClCompile Include="473. Matchsticks to Square.cpp" />
415+
<ClCompile Include="5088. missing-number-in-arithmetic-progression.cpp" />
416+
<ClCompile Include="5089. meeting-scheduler.cpp" />
417+
<ClCompile Include="5090. toss-strange-coins.cpp" />
418+
<ClCompile Include="5111. divide-chocolate.cpp" />
415419
<ClCompile Include="5223. queens-that-can-attack-the-king.cpp" />
416420
<ClCompile Include="5224. dice-roll-simulation.cpp" />
417421
<ClCompile Include="5225. maximum-equal-frequency.cpp" />

cpp/leetcode/leetcode.vcxproj.filters

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,5 +1172,17 @@
11721172
<ClCompile Include="472. concatenated-words.cpp">
11731173
<Filter>源文件</Filter>
11741174
</ClCompile>
1175+
<ClCompile Include="5088. missing-number-in-arithmetic-progression.cpp">
1176+
<Filter>源文件</Filter>
1177+
</ClCompile>
1178+
<ClCompile Include="5089. meeting-scheduler.cpp">
1179+
<Filter>源文件</Filter>
1180+
</ClCompile>
1181+
<ClCompile Include="5090. toss-strange-coins.cpp">
1182+
<Filter>源文件</Filter>
1183+
</ClCompile>
1184+
<ClCompile Include="5111. divide-chocolate.cpp">
1185+
<Filter>源文件</Filter>
1186+
</ClCompile>
11751187
</ItemGroup>
11761188
</Project>

cpp/leetcode/test.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include "public.h"
1+
#include <iostream>
22

33
using namespace std;
4-

0 commit comments

Comments
 (0)