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日 ...
随机推荐
- appium--连续滑动
TouchAction 在之前说过了滑动swip,那种是两点之间的滑动,比如上滑,左滑等.但实际工作中会遇到一些复杂的场景,如九宫格的滑动等待,这时候就要使用TouchAction,TouchActi ...
- [LeetCode] 902. Numbers At Most N Given Digit Set 最大为 N 的数字组合
We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}. (Not ...
- 使用Intellij idea新建Java Web项目(servlet) 原理及初步使用
准备 JDK (配置JDK_HOME\bin 和 CLASSPATH) 注:JDK8下载已经需要注册了,请使用JDK11(现在是官方长期支持的版本) 对于我们新手来说,JD ...
- 递归函数详解——VS调试教你理解透彻递归
#include <stdio.h> #include <stdlib.h> int recursion(int); ; int main(void) { recursion( ...
- plsql安装
1.plsql都需要安装oracle的客户端,不过也可以安装瘦客户端,完整的客户端太大了,俗称胖客户端,一般瘦客户端就可以满足, 本人下载的是instantclient-basic-win32-11. ...
- 推荐支付宝 Android 专项测试工具SoloPi
推荐支付宝 Android 专项测试工具SoloPi 1 介绍 SoloPi是一个无线化.非侵入式的Android自动化工具,公测版拥有录制回放.性能测试.一机多控三项主要功能,能为测试开发人员节省宝 ...
- springmvc单例
默认情况下springmvc都是单例的,用@Controller注解的web页面,下次请求时,可以拿到controller成员变量的上次运行的信息. 比如:controller类里面有: Linked ...
- Intellij插件之MavenHelper
作用: 一键查看maven依赖,查看冲突的依赖,一键进行exclude依赖 插件提供地址: https://plugins.jetbrains.com/plugin/7179-maven-helper ...
- docker-composer 简单教程
原文地址:https://blog.51cto.com/9291927/2310444 Docker快速入门——Docker-Compose 一.Docker-Compose简介 1.Docker-C ...
- python 进程数据通信
进程通信的第一种方式from multiprocessing import Process,Queue def f(q): q.put([42,2,'hello']) print('zi q id:' ...