活跃农民
- 积分
- 543
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2019-9-30
- 最后登录
- 1970-1-1
|
本帖最后由 我已全仓 于 2021-8-22 23:28 编辑
Day 3
两场打下来 双周1题 周赛2题;估计要掉10分
双周赛
5834. Minimum Time to Type Word Using Special Typewriter 距离没想出怎么表示 卡了半小时
现在想明白了,就是26+两个方向之差 再mod
5835. Maximum Matrix Sum 想用backtrack没做出,应该greedy来做。
因为操作次数不限,我们只在乎终态。任意相邻置为相反数可以把负数位置任意移动,最终导致要么全为正,要么将绝对值最小的那个数变为负数。
Time complexity: O(mn)
周赛:
5851. Find Unique Binary String (med): 17min WA2次
转set后 枚举数 再转为补零二进制字符串 后面发现输入规模很小,直接暴力就可以
正解是构造法: Implement cantor's diagonal argument to construct a unique string by setting its character in a position diff with an input string (in any order but cannot use one of the input strings twice)
利用康拓对角线构造(OS、Alogrithm证明学过)
5852. Minimize the Difference Between Target and Chosen Elements (med): 做了1h没过
先用的sort + n-pointer用堆贪心移动min Delta,后面发现小的数走过了后,不能凑上去。过了2/3样例。O(mlogn + nlogm)
转用backtrack,超时只能过1/3 test case, time complexity O(mlogn + n^m)
试图两者结合进行branch and bound,很难定义上下界,没过。。
正解: knapsack dp 相当于暴力枚举 数据范围和空间 70 * 70 * 800, time complexity O(m*n*target)
数据范围的暗示很明显了,但没想到用knapsack dp
Review process: Top100(22/100)
46. Permutations: 4min backtrack 用了set记录used num 但不是最优
Optimal solution: based on swap, at each invoke it products n possibilities, which represents what num after A will be swapped with A (if A swap itself, it means no change).
Time complexity: O(n * n!), O(n!) for backtrack, and O(n) for a copy of the collection
Space complexity: O(n)
53. Maximum Subarray: 10min 没做出来,尝试了sliding window失败,区间dp复杂度过高
是经典一维dp题,follow up的divide and conquer还没看
Code challenge:
789. Escape The Ghosts: 18min greedy 基本时间花在读题和举例子上
Firstly, the data set is 10^4, making it impossible to implement BFS to simulate the game. Secondly, when considering the policy of ghosts, the optimal approach is to get as close as possible to the target point and catch the player.
Only when any ghost's cost to get to the target is larger than player's, the player might win.
As for cost here, it is the sum of abs diff of x and y coordinates between target and ghost or player, called Manhattan Distance.
另外做了美团的一道笔试题: 1h类似856. Score of Parentheses
Evaluate the parentheses expression value. Eg. “()” = 1, “(())” = 2, “((()))(())” = 6
也就是同一层乘 不同层加
Divide and conquer: built left and right to record the freq of parentheses, when reached a balanced point, that is left == right. We are sure that the expression looks like “(x)”, “x” can be anything. So, the subproblem is the value of “x”.
The core code is ans *= 1 + dfs(s, i – left * 2 + 1, i), which divided the problem into subproblem and combine the return value of subproblem.
Time complexity: O(n^2)
Glossary:
Knapsack dp – 背包动态规划
branch and bound - 分支定界
divide and conquer - 分治法
Manhattan Distance - 曼哈顿距离
Euclidean distance – 欧式距离
cantor's diagonal argument - 康拓对角线证明
sliding window – 滑动窗口
|
|