【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 };

【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 };

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

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

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

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

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica }

【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 };

【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 };

【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 };

【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 };

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

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

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica }

原文地址:https://www.cnblogs.com/zhangwanying/p/10350397.html

时间: 2024-08-30 05:20:24

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

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

Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranking: 440/2797.这次题目似乎比较简单,因为我比赛的时候前三题全做出来了(1:12:39),然后第四题有思路,正在写,没写完,比赛完了写完提交也对了. p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [821]

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

Contest 41 ()(题号) Contest 42 ()(题号) Contest 43 ()(题号) Contest 44 (2018年12月6日,周四上午)(题号653-656) 链接:https://leetcode.com/contest/leetcode-weekly-contest-44 比赛情况记录:就做出来两题,第三题不难,然而就是在算坐标的时候卡住了.orz.结果:2/4,ranking:637/2272.第四题没看题,第三题搞得心情不好了orz. [653]Two Sum

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

Contest 71 () Contest 72 () Contest 73 (2019年1月30日模拟) 链接:https://leetcode.com/contest/weekly-contest-73 Contest 74 (2019年1月31日模拟) 链接:https://leetcode.com/contest/weekly-contest-74 Contest 75 (2019年1月31日模拟) 链接:https://leetcode.com/contest/weekly-conte

LeetCode Weekly Contest 121

上周因为感冒没有刷题,两个星期没有刷题,没手感了,思维也没有那么活跃了,只刷了一道,下个星期努力. 984. String Without AAA or BBB Given two integers A and B, return any string S such that: S has length A + B and contains exactly A 'a' letters, and exactly B 'b' letters; The substring 'aaa' does not

[leetcode 周赛 157] 1217 玩筹码

1217 Play With Chips 玩筹码 题目描述 数轴上放置了一些筹码,每个筹码的位置存在数组 chips 当中. 你可以对 任何筹码 执行下面两种操作之一(不限操作次数,0 次也可以): 将第 i 个筹码向左或者右移动 2 个单位,代价为 0. 将第 i 个筹码向左或者右移动 1 个单位,代价为 1. 最开始的时候,同一位置上也可能放着两个或者更多的筹码. 返回将所有筹码移动到同一位置(任意位置)上所需要的最小代价. 示例 1: 输入:chips = [1,2,3] 输出:1 解释:

[leetcode 周赛 157] 1218 最长定差子序列

1218 Longest Arithmetic Subsequence of Given Difference 最长定差子序列 问题描述 给你一个整数数组 arr 和一个整数 difference,请你找出 arr 中所有相邻元素之间的差等于给定 difference 的等差子序列,并返回其中最长的等差子序列的长度. 示例 1: 输入:arr = [1,2,3,4], difference = 1 输出:4 解释:最长的等差子序列是 [1,2,3,4]. 示例 2: 输入:arr = [1,3,

[leetcode 周赛 157] 1219 黄金矿工

1219 Path with Maximum Gold 黄金矿工 问题描述 你要开发一座金矿,地质勘测学家已经探明了这座金矿中的资源分布,并用大小为 m * n 的网格 grid 进行了标注.每个单元格中的整数就表示这一单元格中的黄金数量:如果该单元格是空的,那么就是 0. 为了使收益最大化,矿工需要按以下规则来开采黄金: 每当矿工进入一个单元,就会收集该单元格中的所有黄金. 矿工每次可以从当前位置向上下左右四个方向走. 每个单元格只能被开采(进入)一次. 不得开采(进入)黄金数目为 0 的单元

[leetcode 周赛 159] 1233 删除子文件夹

1233 Remove Sub-Folders from the Filesystem 删除子文件夹 问题描述 你是一位系统管理员,手里有一份文件夹列表 folder,你的任务是要删除该列表中的所有 子文件夹,并以 任意顺序 返回剩下的文件夹. 我们这样定义「子文件夹」: 如果文件夹?folder[i]?位于另一个文件夹?folder[j]?下,那么?folder[i]?就是?folder[j]?的子文件夹. 文件夹的「路径」是由一个或多个按以下格式串联形成的字符串: /?后跟一个或者多个小写英

【Leetcode周赛】比赛目录索引

contest 1 ~ contest 10 contest 11 ~ contest 20 contest 21 ~ contest 30 : https://www.cnblogs.com/zhangwanying/p/6753040.html contest 31 ~ contest 40 contest 41 ~ contest 50 contest 51 ~ contest 60:https://www.cnblogs.com/zhangwanying/p/9998523.html c