573. Squirrel Simulation
Problem statement:
There's a tree, a squirrel, and several nuts. Positions are represented by the cells in a 2D grid. Your goal is to find the minimal distance for the squirrel to collect all the nuts and put them under the tree one by one. The squirrel can only take at most one nut at one time and can move in four directions - up, down, left and right, to the adjacent cell. The distance is represented by the number of moves.
Example 1:
Input:
Height : 5
Width : 7
Tree position : [2,2]
Squirrel : [4,4]
Nuts : [[3,0], [2,5]]
Output: 12
Explanation:

Note:
- All given positions won't overlap.
- The squirrel can take at most one nut at one time.
- The given positions of nuts have no order.
- Height and width are positive integers. 3 <= height * width <= 10,000.
- The given positions contain at least one nut, only one tree and one squirrel.
Solution:
There is a matrix, it looks like BFS, DFS or DP, however, there is only some numbers, there is no any input board or matrix. So it is pure math.
The key is which nut as the first target for the squirrel to pick.
Frist, calculate the distance from squirrel and trees to all nuts, put them in two different arrays, and accumulate the total distance from tree to nuts.
The final step to find the solution. Loop to pick each nut as the first target. Subtract the distance from tree to this nuts and plus the distance from this nut to the squirrel, and choose the minimal distance.
The time complexity is O(n).
class Solution {
public:
int minDistance(int height, int width, vector<int>& tree, vector<int>& squirrel, vector<vector<int>>& nuts) {
vector<int> squi2nuts;
vector<int> tree2nuts;
int total_dis = ;
for(int i = ; i < nuts.size(); i++){
// calculate the distrance from squirrel to nuts
squi2nuts.push_back(abs(nuts[i][] - squirrel[]) + abs(nuts[i][] - squirrel[]));
// calculate total distance, double the distance from tree to all nuts
total_dis += (abs(nuts[i][] - tree[]) + abs(nuts[i][] - tree[])) * ;
// calculate the distrance from tree to nuts
tree2nuts.push_back(abs(nuts[i][] - tree[]) + abs(nuts[i][] - tree[]));
}
int min_dis = INT_MAX;
for(int i = ; i < tree2nuts.size(); i++){
min_dis = min(min_dis, total_dis - tree2nuts[i] + squi2nuts[i]);
}
return min_dis;
}
};
573. Squirrel Simulation的更多相关文章
- [LeetCode] Squirrel Simulation 松鼠模拟
There's a tree, a squirrel, and several nuts. Positions are represented by the cells in a 2D grid. Y ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- LeetCode All in One 题目讲解汇总(转...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- Squirrel: 通用SQL、NoSQL客户端
安装 配置数据库 配置驱动 配置连接 如果你的工作中,需要使用到多个数据库,又不想在多种客户端之间切换来切换去.那么就需要找一款支持多数据库的客户端工具了.如果你要连接多个关系型数据库,你就可以使用N ...
- Font Squirrel
Font Squirrel FontSquirrel:国外免费字体下载网是一个提供高质量商业字体下载网站,提供海量的英文字体库,用户可以随意下载并应用于各种商业用字,无需考虑其版权问题. 官网地址:h ...
- 使用Squirrel创建基于Electron开发的Windows 应用安装包
我们把自己开发的Electron应用发布之前,需要把app打包成简单的安装包,这样app更容易被获取,以此来发布我们的应用.我们可以参考Wix或其他的安装程序,但是对于Electron应用更好的打包程 ...
- Gate level Simulation(门级仿真)
1 什么是后仿真? 后仿真也成为时序仿真,门级仿真,在芯片布局布线后将时序文件SDF反标到网标文件上,针对带有时序信息的网标仿真称为后仿真. 2 后仿真是用来干嘛的? 检查电路中的timing vio ...
随机推荐
- 免安装版Tomcat配置内存
去安装目录下 找 bin 目录找到这个文件 catalina.bat在文件的头部加上 set JAVA_OPTS=-Xms512m -Xmx512m -Xss1024k具体大小自己调整
- SocketServer模块
在利用select实现伪并发的socket博文中我们说了: 如果要实现一个server端可以和多个客户端进行通信可以使用 1.多线程 2.多进程 3.select I/O多路复用 在那篇博文中我们介绍 ...
- React Native 可以走多远?
对于大多数APP开发者来说,能够同时开发出Android APP和IOS APP是不是很牛逼,可是它也不是天方夜谭,自从有了一个叫React Native的东西的出现,这一切就变得可以实现了. 那么到 ...
- icheck样式绑定与翻页保持
官方文档:http://icheck.fronteed.com/ 使用基本样式 $('input').iCheck({ checkboxClass : 'icheckbox_square-blue', ...
- 老李分享知识:性能测试之TPS和吞吐率
老李分享知识:性能测试之TPS和吞吐率 当增大系统的压力(或添加并发用户数)时,吞吐率和TPS的改变曲线呈大体一致,则系统基本稳定. 若压力增大时,吞吐率的曲线添加到一定程度后出现改变缓 ...
- Hibernate(二)之Hibernate-api详解
一.Hibernate体系结构 二.Hibernate-api详解 2.1.Configuration配置对象 Configuration是用来加载配置文件的 我们Hibernate中主要有两个配置文 ...
- Azure Messaging-ServiceBus Messaging消息队列技术系列8-服务总线配额
上篇博文中我们介绍了Azure ServiceBus Messaging的消息事务机制: Azure Messaging-ServiceBus Messaging消息队列技术系列7-消息事务(2017 ...
- Python javascript操作DOM
文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是,DOM把 ...
- 国付宝ecshop,shopex,shopnc在线支付接口,php版本支付接口开发
最近应一个客户的要求,给他的一个ecshop商城开发国付宝在线支付接口.国付宝估计大家比较陌生,但是他集成了很多银行的一些网银接口,所以比较方便.号称国家级的第三方支付平台.最近有增加了域名验证,就是 ...
- Jquery对复选框CheckBox的操作
checkbox: 多选框 //获取选中值 checkbox:$("#checkbox_id").attr("value"): 多选框checkbox,打勾: ...