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日 ...
随机推荐
- 【Step-By-Step】第 三 周
本周面试题一览: 什么是XSS攻击,XSS 攻击可以分为哪几类?我们如何防范XSS攻击? 如何隐藏页面中的某个元素? 浏览器事件代理机制的原理是什么? setTimeout 倒计时为什么会出现误差? ...
- [LeetCode] 263. Ugly Number 丑陋数
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- Spring security 知识笔记【自定义登录页面】
一.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- Golang(十一)TLS 相关知识(二)OpenSSL 生成证书
0. 前言 接前一篇文章,上篇文章我们介绍了数字签名.数字证书等基本概念和原理 本篇我们尝试自己生成证书 参考文献:TLS完全指南(二):OpenSSL操作指南 1. OpenSSL 简介 OpenS ...
- addpath(),genpath()
clear all:clear clc: addpath(): 打开不在同一目录下的文件 addpath('sparse-coding');%sparse-coding,SIFT均表示路径,此目录下的 ...
- pta作业错误点--总结
pta作业错误点--总结 注释:在做pta题目的时候,发现有许多题目的错误点是值得归纳总结起来的,今后翻阅博客园的时候能够明白之前有哪些是可以值得复习的. 7-2 换硬币 习题2-3 求平方与倒数序列 ...
- python输入一个字符串,输出翻转后的字符串(翻转字符串)
题目:输出一个字符串,输出翻转后的字符串.例如:输入字符串a123,输出321a. 方法一:使用列表的reverse方法 string=input('请输入一个字符串:') lst=list(stri ...
- Notes:Unit Testing
UnitTesting shanzm 右键-->在新标签页中打开图片,即可查看原图,图片超乎你想像的大!
- web api与mvc的区别
MVC主要用来构建网站,既关心数据也关心页面展示,而Web API只关注数据 Web API支持格式协商,客户端可以通过Accept header通知服务器期望的格式 Web API支持Self Ho ...
- C#利用newtonsoft.json读取.so配置文件内容
今天花 了点时间来使用 C#读取json文件 ,文件后缀为 .so文件 ,也是基于文件流的形式 获取 对象 ,然后解析; 之所以尝试 使用 json读取 ,是因为其配置文件的格式 更为友好 和方便,直 ...