# [朝花夕拾] LeetCode每日一题

20220304 2104; 20220309 798; 20220330 1606;

20220228 1601. 最多可达成的换楼请求数目

 1 #include <cstring>
2
3 const int N = 25;
4
5 class Solution {
6 public:
7     int a[N][N], b[N];
8     int dfs(int o, int x, int n, int m) {
9         int res = 0;
10         if (o == m) {
11             int tot = 0;
12             for (int i = 0; i < n; i++) {
13                 for (int j = 0; j < m; j++)
14                     tot += (b[j] ? a[j][i] : 0);
15                 if (tot != 0) return 0;
16             }
17             return x;
18         }
19         b[o] = 1;
20         res = dfs(o + 1, x + 1, n, m);
21         b[o] = 0;
22         res = max(res, dfs(o + 1, x, n, m));
23         return res;
24     }
25     int maximumRequests(int n, vector<vector<int>>& requests) {
26         int m = requests.size();
27         for (int i = 0; i < m; i++)
28             a[i][requests[i][0]] += -1, a[i][requests[i][1]] += 1;
29         memset(b, 0, sizeof(b));
30         return dfs(0, 0, n, m);
31     }
32 };

 1 const int N = 25;
2
3 class Solution {
4 public:
5     int maximumRequests(int n, vector<vector<int>>& r) {
6         int a[N], m = r.size(), ans = 0;
7         for (int o = 0; o < (1 << m); o++) {
8             memset(a, 0, sizeof(a));
9             int res = 0;
10             for (int i = 0; i < m; i++)
11                 if (o & (1 << i))
12                     a[r[i][0]]--, a[r[i][1]]++, res++;
13             int tot = 0;
14             for (int i = 0; i < n; i++)
15                 tot += (a[i] == 0);
16             if (tot == n) ans = max(res, ans);
17         }
18         return ans;
19     }
20 };

20220301 6. Z 字形变换

 1 class Solution {
2 public:
3     string convert(string s, int r) {
4         string a;
5         int l = s.length(), x = 0;
6         if (r == 1) return s;
7         for (int i = 0; i < r; i++) {
8             int o = i;
9             if (o >= l) return a;
10             while (1) {
11                 if (i != r - 1) {
12                     a += s[o];
13                     o += (r - i - 1) * 2;
14                     if (o >= l) break;
15                 }
16                 if (i != 0) {
17                     a += s[o];
18                     o += i * 2;
19                     if (o >= l) break;
20                 }
21             }
22         }
23         return a;
24     }
25 };

20220302 564. 寻找最近的回文数

> stoi, stol, to_string... 等字符串转换函数

> rbegin(), rend() 反向迭代器同样适用于 string

> i & 1 非常方便的区分奇数偶数的写法

> for (int i : {...}) 以及 for (auto& i: list)

 1 class Solution {
2 public:
3     vector<long> getlist(const string& str) {
4         int l = str.length();
5         vector<long> list = {(long)pow(10, l - 1) - 1, (long)pow(10, l) + 1};
6         long pre = stol(str.substr(0, (l + 1) / 2));
7         for (int i : {pre - 1, pre, pre + 1}) {
8             string pres = to_string(i);
9             string o = pres + string(pres.rbegin() + (l & 1), pres.rend());
10             list.push_back(stol(o));
11         }
12         return list;
13     }
14
15     string nearestPalindromic(string str) {
16         long n = stol(str), ans = -1;
17         const vector<long>& list = getlist(str);
18         for (auto& i : list) {
19             if (i != n) {
20                 long a1 = abs(i - n), a2 = abs(ans - n);
21                 if (ans == -1 || a1 < a2 || a1 == a2 && i < ans)
22                     ans = i;
23             }
24         }
26     }
27 };

20220303 258. 各位相加

 1 class Solution {
2 public:
3     int work(int num) {
4         if (num < 10) return num;
5         int res = 0;
6         while (num)
7             res += num % 10, num /= 10;
8         return work(res);
9     }
10
12         return work(num);
13     }
14 };

1 class Solution {
2 public:
4         return (num - 1) % 9 + 1;
5     }
6 };

20220304 2104. 子数组范围和

 1 class Solution {
2 public:
3     long long subArrayRanges(vector<int>& a) {
4         long long n = a.size(), ans = 0;
5         for (int i = 0; i < n; i++) {
6             int mx = a[i], mi = a[i];
7             for (int j = i + 1; j < n; j++) {
8                 mx = max(mx, a[j]);
9                 mi = min(mi, a[j]);
10                 ans += mx - mi;
11             }
12         }
13         return ans;
14     }
15 };

 1 class Solution {
2 public:
3     long long subArrayRanges(vector<int>& a) {
4         int n = a.size();
5         long long ans = 0;
6         stack<int> s1, s2;
7         vector<int> mil(n), mxl(n), mir(n), mxr(n);
8         for (int i = 0; i < n; i++) {
9             while (!s1.empty() && a[s1.top()] > a[i])
10                 s1.pop();
11             mil[i] = s1.empty() ? -1 : s1.top();
12             s1.push(i);
13             while (!s2.empty() && a[s2.top()] <= a[i])
14                 s2.pop();
15             mxl[i] = s2.empty() ? -1 : s2.top();
16             s2.push(i);
17         }
18         s1 = s2 = stack<int>();
19         for (int i = n - 1; i >= 0; i--) {
20             while (!s1.empty() && a[s1.top()] >= a[i])
21                 s1.pop();
22             mir[i] = s1.empty() ? n : s1.top();
23             s1.push(i);
24             while (!s2.empty() && a[s2.top()] < a[i])
25                 s2.pop();
26             mxr[i] = s2.empty() ? n : s2.top();
27             s2.push(i);
28         }
29         for (int i = 0; i < n; i++)
30             ans += (long long)a[i] * (long long)((i - mxl[i]) * (mxr[i] - i) - (i - mil[i]) * (mir[i] - i));
31         return ans;
32     }
33 };

20220305 521. 最长特殊序列 I

1 class Solution {
2 public:
3     int findLUSlength(string a, string b) {
4         return a == b ? -1 : max(a.size(), b.size());
5     }
6 };

20220306 2100. 适合打劫银行的日子

 1 class Solution {
2 public:
3     vector<int> goodDaysToRobBank(vector<int>& a, int t) {
4         int n = a.size();
5         vector<int> dec(n);
6         vector<int> inc(n);
7         for (int i = 1; i < n; i++) {
8             if (a[i] <= a[i - 1])
9                 dec[i] = dec[i - 1] + 1;
10             if (a[n - i - 1] <= a[n - i])
11                 inc[n - i - 1] = inc[n - i] + 1;
12         }
13         vector<int> ans;
14         for (int i = t; i < n - t; i++) {
15             if (dec[i] >= t && inc[i] >= t) {
16                 ans.emplace_back(i);
17             }
18         }
19         return ans;
20     }
21 };

20220307 504. 七进制数

 1 class Solution {
2 public:
3     string convertToBase7(int num) {
4         string ans;
5         int x;
6         if (num < 0) num = -num, ans.push_back('-');
7         for (x = 1; x * 7 <= num; x *= 7);
8         while (x) {
9             int o = 0, i;
10             for (i = 0; o + x <= num; i++, o += x);
11             num -= o, x /= 7, ans.push_back((char)i + '0');
12         }
13         return ans;
14     }
15 };

20220308 2055. 蜡烛之间的盘子

 1 class Solution {
2 public:
3     vector<int> platesBetweenCandles(string s, vector<vector<int>>& q) {
4         int len = s.length();
5         vector<int> ans, l(len), r(len), sum(len);
6         sum[0] = s[0] == '|', l[0] = s[0] == '|' ? 0 : -1;
7         for (int i = 1; i < len; i++) {
8             sum[i] = sum[i - 1] + (s[i] == '|');
9             l[i] = s[i] == '|' ? i : l[i - 1];
10         }
11         r[len - 1] = s[len - 1] == '|' ? len - 1 : -1;
12         for (int i = len - 2; i >= 0; i--)
13             r[i] = s[i] == '|' ? i : r[i + 1];
14         int tot = q.size();
15         for (int i = 0; i < tot; i++)
16             if (l[q[i][1]] == -1 || r[q[i][0]] == -1) ans.push_back(0);
17             else ans.push_back(max(0, l[q[i][1]] - r[q[i][0]] + 1 - sum[q[i][1]] + (q[i][0] == 0 ? 0 : sum[q[i][0] - 1])));
18         return ans;
19     }
20 };

20220309 798. 得分最高的最小轮调

 1 class Solution {
2 public:
3     int bestRotation(vector<int>& nums) {
4         int n = nums.size(), ans = 0, sum = 0, mx = 0;
5         vector<int> diffs(n);
6         for (int i = 0; i < n; i++) {
7             int l = (i + 1) % n, r = (i - nums[i] + n + 1) % n;
8             diffs[l]++, diffs[r]--;
9             if (l >= r) diffs[0]++;
10         }
11         for (int i = 0; i < n; i++)
12             if (mx < (sum += diffs[i]))
13                 mx = sum, ans = i;
14         return ans;
15     }
16 };

20220310. N 叉树的前序遍历

 1 class Solution {
2 public:
3     void dfs(const Node* root, vector<int> & res) {
4         if (root == nullptr) {
5             return;
6         }
7         res.emplace_back(root->val);
8         for (auto & ch : root->children) {
9             dfs(ch, res);
10         }
11     }
12
13     vector<int> preorder(Node* root) {
14         vector<int> res;
15         dfs(root, res);
16         return res;
17     }
18 };

20220311 2049. 统计最高分的节点数目

 1 const int N = 1e5 + 5;
2
3 class Solution {
4     long long l[N], r[N], tl[N], tr[N], b[N];
5 public:
6     int dfs(int o) {
7         if (!tl[o]) return 1;
8         l[o] = dfs(tl[o]);
9         if (tr[o]) r[o] = dfs(tr[o]);
10         return l[o] + r[o] + 1;
11     }
12     int countHighestScoreNodes(vector<int>& p) {
13         int n = p.size(), ans;
14         long long mx = 0;
15         for (int i = 1; i < n; i++)
16             if (tl[p[i]]) tr[p[i]] = i;
17             else tl[p[i]] = i;
18         int root = dfs(0);
19         for (int i = 0 ; i < n; i++) {
20             for (auto &j : {&l[i], &r[i], &(b[i] = root - l[i] - r[i] - 1)})
21                 if (!*j) *j = 1;
22             long long res = l[i] * r[i] * b[i];
23             if (res > mx) mx = res, ans = 1;
24             else if (res == mx) ans++;
25         }
26         return ans;
27     }
28 } s;

20220312 590. N 叉树的后序遍历

 1 class Solution {
2 public:
3     void helper(const Node* root, vector<int> & res) {
4         if (root == nullptr) {
5             return;
6         }
7         for (auto & ch : root->children) {
8             helper(ch, res);
9         }
10         res.emplace_back(root->val);
11     }
12
13     vector<int> postorder(Node* root) {
14         vector<int> res;
15         helper(root, res);
16         return res;
17     }
18 };

20220313 393. UTF-8 编码验证

 1 class Solution {
2 public:
3     bool validUtf8(vector<int>& data) {
4         int cnt = 0;
5         for(int x : data) {
6             if((x >> 7) & 1) {
7                 if((x >> 6) & 1) {
8                     if((x >> 5) & 1) {
9                         if((x >> 4) & 1) {
10                             if((x >> 3) & 1) {
11                                 return false;
12                             } else {
13                                 if(cnt) {
14                                     return false;
15                                 }
16                                 cnt = 3;
17                             }
18                         } else {
19                             if(cnt) {
20                                 return false;
21                             }
22                             cnt = 2;
23                         }
24                     } else {
25                         if(cnt) {
26                             return false;
27                         }
28                         cnt = 1;
29                     }
30                 } else {
31                     cnt--;
32                 }
33             }
34         }
35
36         return cnt == 0;
37     }
38 };

22020314 599. 两个列表的最小索引总和

 1 class Solution {
2 public:
3     vector<string> findRestaurant(vector<string>& list1, vector<string>& list2) {
4         unordered_map<string, int> index;
5         for (int i = 0; i < list1.size(); i++) {
6             index[list1[i]] = i;
7         }
8
9         vector<string> ret;
10         int indexSum = INT_MAX;
11         for (int i = 0; i < list2.size(); i++) {
12             if (index.count(list2[i]) > 0) {
13                 int j = index[list2[i]];
14                 if (i + j < indexSum) {
15                     ret.clear();
16                     ret.push_back(list2[i]);
17                     indexSum = i + j;
18                 } else if (i + j == indexSum) {
19                     ret.push_back(list2[i]);
20                 }
21             }
22         }
23         return ret;
24     }
25 };

20220315 2044. 统计按位或能得到最大值的子集数目

 1 class Solution {
2 public:
3     int countMaxOrSubsets(vector<int> const& nums) {
4         const int n = nums.size(), maxsum = accumulate(nums.begin(), nums.end(), 0, bit_or<int>{});
5         const auto dfs = [&] (auto&& dfs, int p = 0, int sum = 0) {
6             if (sum == maxsum) return 1 << (n - p);
7             if (n == p) return 0;
8             assert(p < n);
9             return dfs(dfs, p + 1, sum) + dfs(dfs, p + 1, sum | nums[p]);
10         };
11         return dfs(dfs);
12     }
13 };

20220316 432. 全 O(1) 的数据结构

 1 class AllOne {
2     list<pair<unordered_set<string>, int>> l;
3     unordered_map<string, list<pair<unordered_set<string>, int>> :: iterator> mp;
4 public:
5     AllOne() {}
6
7     void inc(string key) {
8         if (mp.count(key)) {
9             auto cur = mp[key], nxt = next(cur);
10             if (nxt == l.end() || nxt->second > cur->second + 1) {
11                 unordered_set<string> s({key});
12                 mp[key] = l.emplace(nxt, s, cur->second + 1);
13             }
14             else {
15                 nxt->first.emplace(key);
16                 mp[key] = nxt;
17             }
18             cur->first.erase(key);
19             if (cur->first.empty())
20                 l.erase(cur);
21         } else {
22             if (l.empty() || l.begin()->second > 1) {
23                 unordered_set<string> s({key});
24                 l.emplace_front(s, 1);
25             } else {
26                 l.begin()->first.emplace(key);
27             }
28             mp[key] = l.begin();
29         }
30     }
31
32     void dec(string key) {
33         auto cur = mp[key];
34         if (cur->second == 1) {
35             mp.erase(key);
36         } else {
37             auto pre = prev(cur);
38             if (cur == l.begin() || pre->second < cur->second - 1) {
39                 unordered_set<string> s({key});
40                 mp[key] = l.emplace(cur, s, cur->second - 1);
41             } else {
42                 pre->first.emplace(key);
43                 mp[key] = pre;
44             }
45         }
46         cur->first.erase(key);
47         if (cur->first.empty()) {
48             l.erase(cur);
49         }
50     }
51
52     string getMaxKey() {
53         return l.empty() ? "" : *l.rbegin()->first.begin();
54     }
55
56     string getMinKey() {
57         return l.empty() ? "" : *l.begin()->first.begin();
58     }
59 };

20220317 720. 词典中最长的单词

 1 class Solution {
2 public:
3     string longestWord(vector<string>& words) {
4         sort(words.begin(), words.end(), [](const string &a, const string &b){
5             return a.size() == b.size() ? a > b : a.size() < b.size();
6         });
7         string ans = "";
8         unordered_set<string> s;
9         s.emplace("");
10         for (auto & word : words) {
11             if (s.count(word.substr(0, word.size() - 1))) {
12                 s.emplace(word);
13                 ans = word;
14             }
15         }
16         return ans;
17     }
18 };

 1 class Trie {
2     vector<Trie*> child;
3     bool isEnd;
4 public:
5     Trie() {
6         this->child = vector<Trie*>(26, nullptr);
7         this->isEnd = 0;
8     }
9     void insert(const string& word) {
10         Trie* node = this;
11         for (const auto &ch : word) {
12             int o = ch - 'a';
13             Trie* nxt = node->child[o];
14             if (node->child[o] == nullptr)
15                 node->child[o] = new Trie();
16             node = node->child[o];
17         }
18         node->isEnd = 1;
19     }
20     bool dfs(const string& word) {
21         Trie* node = this;
22         for (const auto &ch : word) {
23             int o = ch - 'a';
24             if (node->child[o] == nullptr || !node->child[o]->isEnd)
25                 return 0;
26             node = node->child[o];
27         }
28         return node != nullptr && node->isEnd;
29     }
30 };
31
32 class Solution {
33 public:
34     string longestWord(vector<string>& words) {
35         Trie t;
36         for (const auto &word : words)
37             t.insert(word);
38         string ans = "";
39         int mx = 0;
40         for (const auto &word : words) {
41             int l = word.size();
42             if (t.dfs(word))
43                 if (l > mx || l == mx && word < ans)
44                 ans = word, mx = l;
45         }
46         return ans;
47     }
48 };

20220329 2024. 考试的最大困扰度

 1 class Solution {
2 public:
3     int work(string s, int k) {
4         int l = 0, r = 0, ans = 0, o = 0, len = s.length();
5         while (r < len) {
6             while (s[r] == 'T') r++;
7             while (s[r] == 'F' && o < k)
8                 r++, o++;
9             ans = max(ans, r - l);
10             while (o == k && s[r] == 'F')
11                 if (s[l] == 'F') l++, o--;
12                 else while (s[l] == 'T') l++;
13         }
14         return ans;
15     }
16     int maxConsecutiveAnswers(string s, int k) {
17         int len = s.length();
18         string t = s;
19         for (int i = 0; i < len; i++)
20             t[i] = (s[i] == 'F' ? 'T' : 'F');
21         return max(work(s, k), work(t, k));
22     }
23 };

20220330 1606. 找到处理最多请求的服务器

 1 class Solution {
2 public:
3     vector<int> busiestServers(int k, vector<int>& a, vector<int>& l) {
4         set<int> avail;
5         vector<int> tot(k), ans;
6         priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> busy;
7         int n = a.size();
8         for (int i = 0; i < k; i++)
9             avail.insert(i);
10         for (int i = 0; i < n; i++) {
11             while (!busy.empty() && busy.top().first <= a[i])
12                 avail.insert(busy.top().second), busy.pop();
13             if (avail.empty())
14                 continue;
15             auto o = avail.lower_bound(i % k);
16             if (o == avail.end())
17                 o = avail.begin();
18             tot[*o]++;
19             busy.emplace(a[i] + l[i], *o);
20             avail.erase(o);
21         }
22         int mx = *max_element(tot.begin(), tot.end());
23         for (int i = 0; i < k; i++)
24             if (tot[i] == mx)
25                 ans.push_back(i);
26         return ans;
27     }
28 };

20220331 728. 自除数

 1 class Solution {
2 public:
3     vector<int> selfDividingNumbers(int l, int r) {
4         vector<int> ans;
5         for (int i = l; i <= r; i++) {
6             int o = i, x = i, nob = 0;
7             while (x) {
8                 if (!(x % 10) || o % (x % 10)) {
9                     nob = 1;
10                     break;
11                 }
12                 x /= 10;
13             }
14             if (!nob) ans.push_back(i);
15         }
16         return ans;
17     }
18 } s;

posted @ 2022-02-28 13:50  jinkun113  阅读(68)  评论(0编辑  收藏  举报