Leetcode周赛从contest-121开始。(一般是10个contest写一篇文章)

Posted zhangwanying

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode周赛从contest-121开始。(一般是10个contest写一篇文章)相关的知识,希望对你有一定的参考价值。

Contest 121 (题号981~984)(2019年1月27日)

链接:https://leetcode.com/contest/weekly-contest-121

总结:2019年2月22日补充的报告。当时不想写。rank:1093/3924,AC:2/4。还是太慢了。

【984】String Without AAA or BBB(第一题 4分)(Greedy, M)

给了两个数字,A 代表 A 个 ‘A’, B 代表 B 个‘B’ 在字符串里面。返回一个可行的字符串,字符串中包含 A 个‘A’, B 个 ‘B’,但是没有连续的 ‘AAA‘ 或者 ‘BBB‘。

题解:当时写的很长。现在说下greedy的方法,我们每次只想着只放一个a或者一个b,然后如果当前字符串的长度大于等于2,就需要判断前面是不是有两个连续的a或者连续的b。如果有的话,那么当前肯定不能放a或者放b。

技术图片
 1 class Solution {
 2 public:
 3     string strWithout3a3b(int A, int B) {
 4         string res;
 5         while (A > 0 || B > 0) {
 6             if (res.size() >= 2) {
 7                 int size = res.size();
 8                 if (res[size-1] == a && res[size-2] == a) {
 9                     res += "b"; --B;
10                 } else if (res[size-1] == b && res[size-2] == b) {
11                     res += "a"; --A;
12                 } else if (A >= B) {
13                     res += "a"; --A;
14                 } else {
15                     res += "b"; --B;
16                 }
17             } else {
18                 if (A >= B) {
19                     res += "a"; --A;
20                 } else {
21                     res += "b"; --B;
22                 }
23             }
24         }
25         return res;
26     }
27 };
View Code

 

【981】Time Based Key-Value Store(第二题 5分)

 

【983】Minimum Cost For Tickets(第三题 5分)(DP)

某个旅游景点有日票,周票和月票三种类型的票价。给了一个array,代表一年中的第几天去景点,问遍历完这个数组最少需要多少花费。

Train tickets are sold in 3 different ways:

  • a 1-day pass is sold for costs[0] dollars;
  • a 7-day pass is sold for costs[1] dollars;
  • a 30-day pass is sold for costs[2] dollars.
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation: 
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.

题解:动态规划,dp[i] 代表前 i 天的最小花费。转移方程:

如果第 i 天不在访问的列表里面: dp[i] = dp[i-1]

else: dp[i] = min(dp[i-1] + cost[0], dp[i-7] + cost[1] + dp[i-30] + cost[2])  

时间复杂度是: O(N)

技术图片
 1 class Solution {
 2 public:
 3     //dp[i] represents minimum money need cost in first i days
 4     int mincostTickets(vector<int>& days, vector<int>& costs) {
 5         const int totDays = 400;
 6         vector<int> dp(totDays, INT_MAX); 
 7         dp[0] = 0;
 8         set<int> st(days.begin(), days.end());
 9         for (int i = 1; i < totDays; ++i) {
10             if (st.find(i) == st.end()) {
11                 dp[i] = dp[i-1];
12                 continue;
13             }
14             dp[i] = dp[i-1] + costs[0];
15             dp[i] = min(dp[i], dp[max(i-7, 0)] + costs[1]);
16             dp[i] = min(dp[i], dp[max(i-30, 0)] + costs[2]);
17         }
18         return dp[366];
19     }
20 };
View Code

 

【982】Triples with Bitwise AND Equal To Zero(第四题 7分)

 

Contest 122(题号985~988)(2019年2月3日,新春大礼包)

链接:https://leetcode.com/contest/weekly-contest-122

总结:这周题目非常简单,四题都过了。

【985】Sum of Even Numbers After Queries(第一题 4分)

给了一个数组,以及一个修改的序列。序列中的一个 pair 代表 p[0] 代表在原来数字上加上的值 val , p[1] 代表数组中需要修改元素的下标。求每次修改之后整个数组的偶数的和。

数据规模:

  1. 1 <= A.length <= 10000
  2. -10000 <= A[i] <= 10000
  3. 1 <= queries.length <= 10000
  4. -10000 <= queries[i][0] <= 10000
  5. 0 <= queries[i][1] < A.length

题解: 用一个变量curSum存储当前数组偶数的和,然后每次执行一个query之后,看当前元素是奇数变成偶数,还是偶数变成奇数,还是偶数变成偶数。来做不同的处理。

技术图片
 1 class Solution {
 2 public:
 3     vector<int> sumEvenAfterQueries(vector<int>& A, vector<vector<int>>& queries) {
 4         const int n = A.size(), m = queries.size();
 5         vector<int> ret;
 6         int curSum = 0;
 7         for (auto num : A) {
 8             if ((num & 1) == 0) {
 9                 curSum += num;
10             }
11         }
12         for (auto q : queries) {
13             int val = q[0], idx = q[1];
14             int oriVal = A[idx];
15             int newVal = oriVal + val;
16             if ((oriVal & 1) == 0) {
17                 if (newVal & 1) {
18                     curSum -= oriVal;
19                 } else {
20                     curSum += val;
21                 }
22             } else {
23                 if ((newVal & 1) == 0) {
24                     curSum += newVal;
25                 }
26             }
27             ret.push_back(curSum);
28             A[idx] = newVal;
29         }
30         return ret;
31     }
32 };
View Code

 

【988】Smallest String Starting From Leaf(第二题 5分)

给了一棵二叉树,树上的每个结点代表一个字母(0-25),问从叶子结点开始到根节点的字典序最小的单词是哪个?

题解:dfs 这颗树,把所有单词找出来。然后排序。注意这个题要求是从叶子结点开始,如果一个结点只有一个儿子,那么它本身不能算叶子结点,从它开始的单词要忽略掉。

技术图片
 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     string smallestFromLeaf(TreeNode* root) {
13         vector<string> words;
14         string str;
15         if (!root) {
16             return "";
17         }
18         dfs(root, words, str);
19         sort(words.begin(), words.end());
20         return words[0];
21     }
22     void dfs(TreeNode* root, vector<string>& words, string& str) {
23         if (!root) { return; }
24         char c = root->val + a;
25         str += string(1, c);
26         if (!root->left && !root->right) {
27             reverse(str.begin(), str.end());
28             words.push_back(str);
29             reverse(str.begin(), str.end());
30             str.pop_back();
31             return;
32         }
33         if (root->left) {
34             dfs(root->left, words, str);
35         }
36         if (root->right) {
37             dfs(root->right, words, str);
38         }
39         str.pop_back();
40         return;
41     }
42 };
View Code

 

【986】Interval List Intersections(第三题 5分)

给了两组左闭右闭的线段,返回这两组线段的交集。

Input: A = [[0,2],[5,10],[13,23],[24,25]], B = [[1,5],[8,12],[15,24],[25,26]]
Output: [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Reminder: The inputs and the desired output are lists of Interval objects, and not arrays or lists.

题解:2 pointers 扫描一遍。

技术图片
 1 /**
 2  * Definition for an interval.
 3  * struct Interval {
 4  *     int start;
 5  *     int end;
 6  *     Interval() : start(0), end(0) {}
 7  *     Interval(int s, int e) : start(s), end(e) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<Interval> intervalIntersection(vector<Interval>& A, vector<Interval>& B) {
13         const int size1 = A.size(), size2 = B.size();
14         int p1 = 0, p2 = 0;
15         vector<Interval> ret;
16         while (p1 < size1 && p2 < size2) {
17             Interval seg1 = A[p1], seg2 = B[p2];
18             int s = max(seg1.start, seg2.start), e = min(seg1.end, seg2.end);
19             if (s > e) {
20                 if (seg1.end > seg2.end) { p2++; }
21                 else { p1++; }
22                 continue;
23             }
24             Interval seg(s, e);
25             ret.push_back(seg);
26             if (seg1.end > seg2.end) {
27                 p2++;
28             } else if (seg2.end > seg1.end) {
29                 p1++;
30             } else {
31                 p1++, p2++;
32             }
33         }
34         return ret;
35     }
36 };
View Code

 

【987】Vertical Order Traversal of a Binary Tree(第四题 5分)

给了一棵二叉树,给了树的结点上坐标的定义。根结点是 (0, 0), For each node at position (X, Y), its left and right children respectively will be at positions (X-1, Y-1) and (X+1, Y-1). 按照 X 轴升序, Y 轴降序, 然后 value 升序的条件,返回这颗树的值。 

技术图片

 题解:dfs一遍树,用map存起来。

技术图片
 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<vector<int>> verticalTraversal(TreeNode* root) {
13         vector<vector<int>> ret;
14         if (!root) {return ret;}
15         dfs(root, 0, 0);
16         for (auto& pp : memo) {
17             int x = pp.first; map<int, vector<int>, greater<int>> mp = pp.second;
18             vector<int> temp;
19             for (auto& ele : mp) {
20                 sort(ele.second.begin(), ele.second.end());
21                 for (auto& num : ele.second) {
22                     temp.push_back(num);
23                 }
24             }
25             ret.push_back(temp);
26         }
27         return ret;
28     }
29     map<int, map<int, vector<int>, greater<int>>> memo; // x-idx, list of values, and height
30     void dfs(TreeNode* root, int x, int y) {
31         if (!root) {return;}
32         memo[x][y].push_back(root->val);
33         dfs(root->left, x-1, y-1);
34         dfs(root->right, x+1, y-1);
35     }
36     
37 };
View Code

 

 

Contest 123(题号985~988)(2019年2月10日)

 

Contest 124(题号993~996)(2019年2月17日)

 

以上是关于Leetcode周赛从contest-121开始。(一般是10个contest写一篇文章)的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode周赛从contest-41开始。(一般是10个contest写一篇文章)

Leetcode周赛从contest-111开始。(一般是10个contest写一篇文章)

Leetcode周赛从contest-81开始。(一般是10个contest写一篇文章)

LeetCode Weekly Contest 121

LeetCode周赛252场:时隔半年再来练习

LeetCode周赛252场:时隔半年再来练习