-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.xml
414 lines (397 loc) · 14 KB
/
atom.xml
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<id>https://yulan.net.cn</id>
<title>语阑gz</title>
<updated>2021-08-29T16:36:22.110Z</updated>
<generator>https://github.com/jpmonette/feed</generator>
<link rel="alternate" href="https://yulan.net.cn"/>
<link rel="self" href="https://yulan.net.cn/atom.xml"/>
<subtitle>独语斜阑</subtitle>
<logo>https://yulan.net.cn/images/avatar.png</logo>
<icon>https://yulan.net.cn/favicon.ico</icon>
<rights>All rights reserved 2021, 语阑gz</rights>
<entry>
<title type="html"><![CDATA[881. 救生艇 2021-08-26]]></title>
<id>https://yulan.net.cn/post/erlm2m_ugz5wq/</id>
<link href="https://yulan.net.cn/post/erlm2m_ugz5wq/">
</link>
<updated>2021-08-26T07:14:49.000Z</updated>
<content type="html"><![CDATA[<pre><code class="language-cpp">class Solution {
public:
int numRescueBoats(vector<int>& people, int limit) {
sort(people.begin(), people.end());
int count = 0;
vector<bool> used(people.size(), false);
for (int i = (int) people.size() - 1; i >= 0; --i) {
if (used[i])
continue;
++count;
auto pt = upper_bound(people.begin(), people.begin() + i, limit - people[i]);
if (pt == people.begin())
continue;
do {
--pt;
if (!used[pt - people.begin()]) {
used[pt - people.begin()] = true;
break;
}
} while (pt != people.begin());
}
return count;
}
};
</code></pre>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[797. 所有可能的路径 2021-08-25]]></title>
<id>https://yulan.net.cn/post/erlm2m_tmg8zs/</id>
<link href="https://yulan.net.cn/post/erlm2m_tmg8zs/">
</link>
<updated>2021-08-25T05:24:15.000Z</updated>
<content type="html"><![CDATA[<pre><code class="language-cpp">class Solution {
private:
vector<vector<int> > ans;
vector<int> path;
void DFS(int node, vector<vector<int>> &G) {
if (node == G.size() - 1) {
path.push_back(node);
ans.push_back(path);
path.pop_back();
return;
}
path.push_back(node);
for (auto i : G[node]) {
DFS(i, G);
}
path.pop_back();
}
public:
vector<vector<int>> allPathsSourceTarget(vector<vector<int>> &graph) {
DFS(0, graph);
return ans;
}
};
</code></pre>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[787. K 站中转内最便宜的航班 2021-08-24]]></title>
<id>https://yulan.net.cn/post/erlm2m_cdiz9f/</id>
<link href="https://yulan.net.cn/post/erlm2m_cdiz9f/">
</link>
<updated>2021-08-24T07:57:26.000Z</updated>
<content type="html"><![CDATA[<pre><code class="language-cpp">class Solution {
private:
const int NOT_CONNECTED = 10000000;
public:
int findCheapestPrice(int n, vector<vector<int>> &flights, int src, int dst, int k) {
vector<vector<int> > G(n, vector<int>(n, NOT_CONNECTED));
for (auto &flight : flights) {
G[flight[0]][flight[1]] = flight[2];
}
for (int i = 0; i < n; ++i)
G[i][i] = 0;
vector<vector<int> > dp(k + 2, vector<int>(n, NOT_CONNECTED));
dp[0][src] = 0;
for (int i = 1; i <= k + 1; ++i) {
for (int j = 0; j < n; ++j) {
for (int l = 0; l < n; ++l) {
dp[i][j] = min(dp[i][j], dp[i - 1][l] + G[l][j]);
}
}
}
return dp[k + 1][dst] == NOT_CONNECTED ? -1 : dp[k + 1][dst];
}
};
</code></pre>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[789. 逃脱阻碍者 2021-08-22]]></title>
<id>https://yulan.net.cn/post/erlm2m_iaa622/</id>
<link href="https://yulan.net.cn/post/erlm2m_iaa622/">
</link>
<updated>2021-08-21T23:48:17.000Z</updated>
<content type="html"><![CDATA[<pre><code class="language-cpp">class Solution {
public:
bool escapeGhosts(vector<vector<int>>& ghosts, vector<int>& target) {
int steps = abs(target[0]) + abs(target[1]);
for (auto ghost : ghosts) {
if (abs(ghost[0] - target[0]) + abs(ghost[1] - target[1]) <= steps)
return false;
}
return true;
}
};
</code></pre>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[443. 压缩字符串 2021-08-21]]></title>
<id>https://yulan.net.cn/post/erlm2m_vaxcsq/</id>
<link href="https://yulan.net.cn/post/erlm2m_vaxcsq/">
</link>
<updated>2021-08-21T05:03:29.000Z</updated>
<content type="html"><![CDATA[<pre><code class="language-cpp">class Solution {
private:
inline int digital_length(int a) {
int count = 0;
while (a > 0) {
++count;
a /= 10;
}
return count;
}
inline int put_num(vector<char> &chars, int start, int num) {
int len = digital_length(num);
for (int i = 1; i <= len; ++i) {
chars[start + len - i] = '0' + (num % 10);
num /= 10;
}
return len;
}
public:
int compress(vector<char> &chars) {
char pre;
int count;
int ans = 0;
pre = chars[0];
count = 1;
int i, j;
for (i = 1, j = 0; i < chars.size(); ++i) {
if (chars[i] == pre)
++count;
else {
chars[j++] = pre;
ans += 1;
if (count > 1) {
int len = put_num(chars, j, count);
j += len;
ans += len;
}
count = 1;
pre = chars[i];
}
}
chars[j++] = pre;
ans += 1;
if (count > 1) {
int len = put_num(chars, j, count);
ans += len;
}
return ans;
}
};
</code></pre>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[541. 反转字符串 II 2021-08-20]]></title>
<id>https://yulan.net.cn/post/erlm2m_gy90w7/</id>
<link href="https://yulan.net.cn/post/erlm2m_gy90w7/">
</link>
<updated>2021-08-19T23:31:30.000Z</updated>
<content type="html"><![CDATA[<pre><code class="language-cpp">class Solution {
public:
string reverseStr(string s, int k) {
auto it = s.begin();
while (it + 2 * k < s.end()) {
reverse(it, it + k);
it += 2 * k;
}
if (s.end() - it >= k)
reverse(it, it + k);
else
reverse(it, s.end());
return s;
}
};
</code></pre>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[345. 反转字符串中的元音字母 2021-08-19]]></title>
<id>https://yulan.net.cn/post/erlm2m_omq5ic/</id>
<link href="https://yulan.net.cn/post/erlm2m_omq5ic/">
</link>
<updated>2021-08-19T05:31:22.000Z</updated>
<content type="html"><![CDATA[<pre><code class="language-cpp">class Solution {
private:
set<char> vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
public:
string reverseVowels(string s) {
int p1 = 0, p2 = s.length() - 1;
while (p1 < p2) {
while (p1 < p2 && vowels.count(s[p1]) == 0) {
++p1;
}
while (p1 < p2 && vowels.count(s[p2]) == 0) {
--p2;
}
if (p1 < p2)
swap(s[p1], s[p2]);
++p1;
--p2;
}
return s;
}
};
</code></pre>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[552. 学生出勤记录 II 2021-08-18]]></title>
<id>https://yulan.net.cn/post/erlm2m_whqg9c/</id>
<link href="https://yulan.net.cn/post/erlm2m_whqg9c/">
</link>
<updated>2021-08-18T01:38:03.000Z</updated>
<content type="html"><![CDATA[<h2 id="dp">DP</h2>
<pre><code class="language-cpp">class Solution {
private:
static constexpr int CEIL = 1000000007;
public:
int checkRecord(int n) {
vector<vector<long long> > dp(n, vector<long long>(6, 0));
// dp[i][0~2]表示在第i天,总A数为0,连续L数为0/1/2时的总组合数,
// dp[i][3~5]表示在第i天,总A数为1,连续L数为0/1/2时的总组合数
dp[0][0] = 1; // 第一天为P
dp[0][1] = 1; // 第一天为L
dp[0][3] = 1; // 第一天为A
for (int i = 1; i < n; ++i) {
// 第i天为P
for (int j = 0; j < 3; ++j) {
dp[i][0] = (dp[i][0] + dp[i - 1][j]) % CEIL;
}
for (int j = 3; j < 6; ++j) {
dp[i][3] = (dp[i][3] + dp[i - 1][j]) % CEIL;
}
// 第i天为A
for (int j = 0; j < 3; ++j) {
dp[i][3] = (dp[i][3] + dp[i - 1][j]) % CEIL;
}
// 第i天为L
dp[i][1] = (dp[i][1] + dp[i - 1][0]) % CEIL;
dp[i][2] = (dp[i][2] + dp[i - 1][1]) % CEIL;
dp[i][4] = (dp[i][4] + dp[i - 1][3]) % CEIL;
dp[i][5] = (dp[i][5] + dp[i - 1][4]) % CEIL;
}
long long count = 0;
for (int i = 0; i < 6; ++i) {
count = (count + dp[n - 1][i]) % CEIL;
}
return count;
}
};
</code></pre>
<h2 id="优化空间复杂度">优化空间复杂度</h2>
<pre><code class="language-cpp">class Solution {
private:
static constexpr int CEIL = 1000000007;
public:
int checkRecord(int n) {
vector<long long> dp1(6, 0), dp2(6, 0);
dp1[0] = 1; // 第一天为P
dp1[1] = 1; // 第一天为L
dp1[3] = 1; // 第一天为A
for (int i = 1; i < n; ++i) {
// 第i天为P
for (int j = 0; j < 3; ++j) {
dp2[0] = (dp2[0] + dp1[j]) % CEIL;
}
for (int j = 3; j < 6; ++j) {
dp2[3] = (dp2[3] + dp1[j]) % CEIL;
}
// 第i天为A
for (int j = 0; j < 3; ++j) {
dp2[3] = (dp2[3] + dp1[j]) % CEIL;
}
// 第i天为L
dp2[1] = (dp2[1] + dp1[0]) % CEIL;
dp2[2] = (dp2[2] + dp1[1]) % CEIL;
dp2[4] = (dp2[4] + dp1[3]) % CEIL;
dp2[5] = (dp2[5] + dp1[4]) % CEIL;
dp1.swap(dp2);
fill(dp2.begin(), dp2.end(), 0);
}
long long count = 0;
for (int i = 0; i < 6; ++i) {
count = (count + dp1[i]) % CEIL;
}
return count;
}
};
</code></pre>
<p>优化后时间复杂度为</p>
<p class='katex-block'><span class="katex-display"><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>O</mi><mo>(</mo><mi>n</mi><mo>)</mo></mrow><annotation encoding="application/x-tex">O(n)
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.02778em;">O</span><span class="mopen">(</span><span class="mord mathdefault">n</span><span class="mclose">)</span></span></span></span></span></p>
<p>空间复杂度为</p>
<p class='katex-block'><span class="katex-display"><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mi>O</mi><mo>(</mo><mn>1</mn><mo>)</mo></mrow><annotation encoding="application/x-tex">O(1)
</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathdefault" style="margin-right:0.02778em;">O</span><span class="mopen">(</span><span class="mord">1</span><span class="mclose">)</span></span></span></span></span></p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[551. 学生出勤记录 I 2021-08-17]]></title>
<id>https://yulan.net.cn/post/erlm2m_hqc1dh/</id>
<link href="https://yulan.net.cn/post/erlm2m_hqc1dh/">
</link>
<updated>2021-08-17T05:24:05.000Z</updated>
<content type="html"><![CDATA[<pre><code class="language-cpp">class Solution {
public:
bool checkRecord(string s) {
int countA = 0;
int countL = 0;
for (char c : s) {
if (c == 'A') {
++countA;
if (countA == 2)
return false;
}
if (c == 'L') {
++countL;
if (countL == 3)
return false;
}
else
countL = 0;
}
return true;
}
};
</code></pre>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[526. 优美的排列 2021-08-16]]></title>
<id>https://yulan.net.cn/post/erlm2m_gvrog8/</id>
<link href="https://yulan.net.cn/post/erlm2m_gvrog8/">
</link>
<updated>2021-08-15T20:50:00.000Z</updated>
<content type="html"><![CDATA[<pre><code class="language-cpp">class Solution {
private:
vector<bool> used;
int n;
int DFS(int ind) {
if (ind == n + 1) return 1;
int count = 0;
for (int i = 1; i <= n; ++i) {
if (ind % i == 0 || i % ind == 0)
if (!used[i]) {
used[i] = true;
count += DFS(ind + 1);
used[i] = false;
}
}
return count;
}
public:
int countArrangement(int n_) {
this->n = n_;
used.resize(n + 1, false);
return DFS(1);
}
};
</code></pre>
]]></content>
</entry>
</feed>