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 ...
随机推荐
- MySql Table错误:is marked as crashed and last (automatic?) 和 Error: Table "mysql"."innodb_table_stats" not found
一.mysql 执行select 的时候报Table错误:is marked as crashed and last (automatic?) 解决方法如下: 找到mysql的安装目录的bin/myi ...
- Dive in python Chapter3 实例
def buildConnectionString(params): """Build a connection string from a dictionary Ret ...
- linux 私房菜 CH8 linux 磁盘与文件系统管理
索引式文件系统 superblock 记录此系统的整体信息,包括 inode/block 的总量.使用量.剩余量,以及文件系统的格式与相关信息等: inode 记录档案的属性,一个档案占用一个 ino ...
- 一个基于php+mysql的外卖订餐网站(带源码)
订饭组 一个基于php+mysql的外卖订餐网站,包括前端和后台.源码地址 源码演示地址:http://dingfanzu.com 商家后台系统:http://dingfanzu.com/admin ...
- WebServiceWSDLWeb
WSDL 文档仅仅是一个简单的 XML 文档. 它包含一系列描述某个 web service 的定义. WSDL 文档是利用这些主要的元素来描述某个 web service 的: 元素 定义 < ...
- POST和GET的详细解释以及区别
Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上的资源,而HTTP ...
- OOP的完美点缀—AOP之SpringAOP实现原理
OOP的完美点缀-AOP之SpringAOP实现原理 前言 OOP与AOP OOP(Object Oriented Programming,面向对象编程),通过封装.继承将程序抽象为各个层次的对象,进 ...
- Letter Combinations of a Phone Number:深度优先和广度优先两种解法
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- How To Use ggplot2
0. Preparation and Introduction ggplot2是R中新颖的数据可视化包,这得益于Leland Wilkinson在他的著作<The Grammar of Grap ...
- 20+个很棒的Android开源项目
20+个很棒的Android开源项目 本文摘自文章: 20+ Awesome Open-Source Android Apps To Boost Your Development Skills. 考虑 ...