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:

  1. All given positions won't overlap.
  2. The squirrel can take at most one nut at one time.
  3. The given positions of nuts have no order.
  4. Height and width are positive integers. 3 <= height * width <= 10,000.
  5. 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的更多相关文章

  1. [LeetCode] Squirrel Simulation 松鼠模拟

    There's a tree, a squirrel, and several nuts. Positions are represented by the cells in a 2D grid. Y ...

  2. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  3. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  4. LeetCode All in One 题目讲解汇总(转...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通 ...

  5. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  6. Squirrel: 通用SQL、NoSQL客户端

    安装 配置数据库 配置驱动 配置连接 如果你的工作中,需要使用到多个数据库,又不想在多种客户端之间切换来切换去.那么就需要找一款支持多数据库的客户端工具了.如果你要连接多个关系型数据库,你就可以使用N ...

  7. Font Squirrel

    Font Squirrel FontSquirrel:国外免费字体下载网是一个提供高质量商业字体下载网站,提供海量的英文字体库,用户可以随意下载并应用于各种商业用字,无需考虑其版权问题. 官网地址:h ...

  8. 使用Squirrel创建基于Electron开发的Windows 应用安装包

    我们把自己开发的Electron应用发布之前,需要把app打包成简单的安装包,这样app更容易被获取,以此来发布我们的应用.我们可以参考Wix或其他的安装程序,但是对于Electron应用更好的打包程 ...

  9. Gate level Simulation(门级仿真)

    1 什么是后仿真? 后仿真也成为时序仿真,门级仿真,在芯片布局布线后将时序文件SDF反标到网标文件上,针对带有时序信息的网标仿真称为后仿真. 2 后仿真是用来干嘛的? 检查电路中的timing vio ...

随机推荐

  1. PHP生成随机水印图片

    基于PHP的GD图形库,自己生成一张图片.仅限初识GD库,实例学习. 一.需求 网站的布局用到了类似慕课网课程列表的风格,每一个课程是一个banner图,图下面是标题加简介.因为课程的数量较大没有为所 ...

  2. Exchange无法发送邮件 未找到匹配的连接器来路由外部收件人解决办法

    使用命令行管理程序创建发送连接器 本示例创建发送连接器,用于集线器传输服务器 HubA 向 Internet 发送电子邮件.   复制 New-SendConnector -Name "In ...

  3. javascript 将数字(金额)转成大写

    将计算好的金额转换成大写,这些功能非常多,下面我改进了一下代码(原文在这里:http://www.cnblogs.com/zsanhong/p/3509464.html). /** * _SetNum ...

  4. flex盒子里面元素linehight对高度的影响

    那天,高高兴兴的测试flex属性.大家都知道,当父盒子display:flex的时候,子盒子即使宽高和大于父盒子,也会由于flex的特性进行收缩:然而,在给父盒子加了flex-direction:co ...

  5. Spring+SpringMVC+MyBatis+easyUI整合优化篇(二)Log4j讲解与整合

    日常啰嗦 上一篇文章主要讲述了一下syso和Log间的一些区别与比较,重点是在项目的日志功能上,因此,承接前文<Spring+SpringMVC+MyBatis+easyUI整合优化篇(一)Sy ...

  6. python之函数学习

    #!/usr/bin/env python # # =============================================== # 位置参数说明 # 位置参数 通过参数传递的位置来 ...

  7. 深入学习 DUBBO

    1.什么是 RPC 协议? RPC 的全称是 Remote Procedure Call 是一种进程间通信方式.它允许程序调用另一个地址空间(通常是共享网络的另一台机器上)的过程或函数,而不用程序员显 ...

  8. JSP的学习

    JSP的学习 1. (1).服务器的名字:Tomcat (2).服务器浏览器访问的地址为: http://localhost:8080 http://127.0.0.1:8080 2.简单的知识 (1 ...

  9. 在IIS上部署你的ASP.NET Core项目

    概述 与ASP.NET时代不同,ASP.NET Core不再是由IIS工作进程(w3wp.exe)托管,而是使用自托管Web服务器(Kestrel)运行,IIS则是作为反向代理的角色转发请求到Kest ...

  10. css实现超出部分用...代替

    如果是一行的话 css为 white-space: nowrap: 保证文本内容不会自动换行,如果多余的内容会在水平方向撑破单元格. overflow: hidden: 隐藏超出单元格的部分. tex ...