Leetcode周赛164
访问所有点的最小时间
思路
由于每次移动有水平方向移动一格、竖直方向移动一格和对角方向一格,而水平方向一格\(+\)竖直方向一格\(=\)对角线方向一格,因此最后答案为相邻两点的切比雪夫距离之和。
代码
class Solution {
public:
int minTimeToVisitAllPoints(vector<vector<int>>& points) {
int n = points.size(), ans = 0;
for(int i = 1; i < n; ++i) {
int disx = abs(points[i][0]-points[i-1][0]), disy = abs(points[i][1]-points[i-1][1]);
ans += max(disx, disy);
}
return ans;
}
};
统计参与通信的服务器
思路
统计每一行每一列有几个服务器,然后对于每一个服务器看他同行或同列的服务器数量大于\(1\)。
代码
class Solution {
public:
int countServers(vector<vector<int>>& grid) {
int n = grid.size(), m = grid[0].size();
int row[255], col[255];
for(int i = 0; i < n; ++i) row[i] = 0;
for(int i = 0; i < m; ++i) col[i] = 0;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
if(grid[i][j]) ++row[i], ++col[j];
}
}
int ans = 0;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
if((row[i] > 1 || col[j] > 1) && grid[i][j]) ++ans;
}
}
return ans;
}
};
搜索推荐系统
思路
我们先将字符串按照字典序进行排序,然后用两个双指针进行移动,当某一个端点的字符串长度已经小于当前遍历的长度或者该位置上的字符与\(searchWord\)在这个位置上的字符不同时移动指针。
代码
class Solution {
public:
vector<vector<string>> suggestedProducts(vector<string>& products, string searchWord) {
int n = products.size(), l = 0, r = n - 1;
sort(products.begin(), products.end());
int len = searchWord.length();
vector<vector<string>> ans(len);
for(int i = 0; i < len; ++i) {
while(l <= r && (products[l].length() <= i || products[l][i] != searchWord[i])) ++l;
while(r >= l && (products[r].length() <= i || products[r][i] != searchWord[i])) --r;
if(l > r) continue;
for(int j = l; j <= min(l + 2, r); ++j) {
ans[i].push_back(products[j]);
}
}
return ans;
}
};
停在原地的方案数
思路
我们定义\(dp[i][j]\)表示在第\(i\)步处于\(j\)这个位置时的方案数,那么很容易发现转移方程为\(dp[i][j]=dp[i-1][j-1]+dp[i-1][j]+dp[i-1][j+1]\),注意我们每次的状态只和上一个状态有关,因此可以用滚动数组进行优化空间。
但是这个的复杂度是\(O(steps*arrLen)\),我们可以发现当我们最多可以移动到\(min(arrLen,steps)\)这个位置,因此大于这个值的地方就不用考虑,因此复杂度就变成了\(O(steps*min(steps,arrLen))\)。
代码
class Solution {
public:
const int mod = 1e9 + 7;
int dp[2][1000005];
int add(int x, int y) {
x += y;
if(x >= mod) x -= mod;
return x;
}
int numWays(int steps, int arrLen) {
arrLen = min(arrLen, steps);
for(int i = 0; i < arrLen; ++i) dp[0][i] = dp[1][i] = 0;
dp[0][0] = 1;
for(int i = 1; i <= steps; ++i) {
int nw = i % 2, pre = (i - 1) % 2;
for(int j = 0; j < arrLen; ++j) {
dp[nw][j] = dp[pre][j];
if(j > 0) dp[nw][j] = add(dp[nw][j], dp[pre][j-1]);
if(j + 1 < arrLen) dp[nw][j] = add(dp[nw][j], dp[pre][j+1]);
}
}
return dp[steps%2][0];
}
};
Leetcode周赛164的更多相关文章
- LeetCode 第 164 场周赛
访问所有点的最小时间 不难看出,从点(x1,y1) 到 (x2,y2) 的步数需要 min(dx,dy),其中 dx = abs(x1-x2),dy = abs(y1-y2) class Soluti ...
- 【Leetcode周赛】从contest-111开始。(一般是10个contest写一篇文章)
Contest 111 (题号941-944)(2019年1月19日,补充题解,主要是943题) 链接:https://leetcode.com/contest/weekly-contest-111 ...
- 拼写单词[哈希表]----leetcode周赛150_1001
题目描述: 给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars. 假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),那么我 ...
- 【Leetcode周赛】从contest-41开始。(一般是10个contest写一篇文章)
Contest 41 ()(题号) Contest 42 ()(题号) Contest 43 ()(题号) Contest 44 (2018年12月6日,周四上午)(题号653—656) 链接:htt ...
- 【Leetcode周赛】从contest-51开始。(一般是10个contest写一篇文章)
Contest 51 (2018年11月22日,周四早上)(题号681-684) 链接:https://leetcode.com/contest/leetcode-weekly-contest-51 ...
- 【Leetcode周赛】从contest-71开始。(一般是10个contest写一篇文章)
Contest 71 () Contest 72 () Contest 73 (2019年1月30日模拟) 链接:https://leetcode.com/contest/weekly-contest ...
- 【Leetcode周赛】从contest-81开始。(一般是10个contest写一篇文章)
Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranki ...
- 【Leetcode周赛】从contest-91开始。(一般是10个contest写一篇文章)
Contest 91 (2018年10月24日,周三) 链接:https://leetcode.com/contest/weekly-contest-91/ 模拟比赛情况记录:第一题柠檬摊的那题6分钟 ...
- 【Leetcode周赛】从contest-121开始。(一般是10个contest写一篇文章)
Contest 121 (题号981-984)(2019年1月27日) 链接:https://leetcode.com/contest/weekly-contest-121 总结:2019年2月22日 ...
随机推荐
- MySQL实战45讲学习笔记:第十一讲
一.如何在邮箱这样的字段上建立合理的索引 现在,几乎所有的系统都支持邮箱登录,如何在邮箱这样的字段上建立合理的索引,是我们今天要讨论的问题. 假设,你现在维护一个支持邮箱登录的系统,用户表是这么定义的 ...
- [LeetCode] 738. Monotone Increasing Digits 单调递增数字
Given a non-negative integer N, find the largest number that is less than or equal to N with monoton ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 挂载ESP(EFI)分区到Windows,并让资源管理器有权限读写
如果你的磁盘精灵经常原地自爆,但你又想读写ESP分区的文件,那么请看此贴 1 //例子如下 diskpart //运行Diskpart工具 list disk //列出所有磁盘 sel disk // ...
- Windows&Mac安装Redis
Windows&Mac安装Redis Window 下安装Redis Redis 支持 32 位和 64 位.这个需要根据你系统平台的实际情况选择,这里我下载 Redis-x64-xxx.zi ...
- jdk1.8 Stream 特性总结
不是数据结构 它没有内部存储,它只是用操作管道从 source(数据结构.数组.generator function.IO channel)抓取数据. 它也绝不修改自己所封装的底层数据结构的数据.例如 ...
- ElasticSearch6.3.2源码分析之节点连接实现
ElasticSearch6.3.2源码分析之节点连接实现 这篇文章主要分析ES节点之间如何维持连接的.在开始之前,先扯一下ES源码阅读的一些心得:在使用ES过程中碰到某个问题,想要深入了解一下,可源 ...
- ioc与bean管理
IOC称之为控制反转,简单来说就是将对象的创建的权利和对象的声明周期的管理过程交给Spring框架来处理,在这个开发过程中不再需要关注对象的创建和生命周期的管理,而是在需要的时由Spring框架提供, ...
- 深入V8引擎-写在前面
这一篇不打算讲技术,聊点别的吧,写这个的原因主要是看到了我博客园的签名,开始这个最终源码系列前想说点什么. 转行前端(达成) 入行1年vue源码(达成).webpack源码(半达成) 入行2年争取读通 ...
- Application类-应用程序生命周期
1.创建Application对象 新建WPF程序后,排除掉WPF自动创建的App.xaml,我们自定义一个类,在该类的Main()方法中,创建Application对象,然后调用创建一个窗口对象,最 ...