当前位置:网站首页>Leetcode-zj-future03: location of express transfer station
Leetcode-zj-future03: location of express transfer station
2022-07-22 13:33:00 【Chrysanthemum headed bat】
leetcode-zj-future03: Location of express transfer station
subject
Topic linking
A map of an area is recorded in k Two dimensional array area, among 0 Representing vacant land ,1 Express delivery point .
If you want to choose a place to set up a transfer station , Make the transfer station to each express delivery point 「 Manhattan distance 」 The sum is the smallest . Please return to this Minimum The sum of the distances .
Be careful :
- Manhattan distance :distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|
- All locations can be used as the set-up points of express transfer stations .
Example 1:
Input : area = [[0,1,0,0,0],[0,0,0,0,1],[0,0,1,0,0]]
Output : 5
explain : Three express delivery points are located in (0,1),(1,4) and (2,2):
(1,2) It is the best transit station location , The total distance is 2 + 2 + 1 = 5.
Example 2:
Input : area = [[1,1],[1,1]]
Output : 4
explain : (0,0) It is one of the best transfer station sites , The total distance is 0 + 1 + 1 + 2 = 4.
Problem solving
Method 1 :
Due to Manhattan distance , In fact, it can solve the problem of two dimensions into one dimension
For the following , Select the one indicated by the arrow as the transfer station , Shortest distance
class Solution {
public:
int buildTransferStation(vector<vector<int>>& area) {
vector<int> x;
vector<int> y;
for(int i=0;i<area.size();i++){
for(int j=0;j<area[0].size();j++){
if(area[i][j]==1){
x.push_back(i);
y.push_back(j);
}
}
}
sort(x.begin(),x.end());
sort(y.begin(),y.end());
int mid=x.size()/2;
int res=0;
for(int xi:x){
res+=abs(xi-x[mid]);
}
for(int yi:y){
res+=abs(yi-y[mid]);
}
return res;
}
};
边栏推荐
- Supply chain collaborative management system of pharmaceutical machinery industry: full link digital coverage to realize the visualization of industrial supply chain
- Addition, deletion, query and modification of [mdsql] table (Advanced)
- 【C】信息管理系统/通讯录通用模板(介绍静态、动态、文件三个版本)
- ideal关于 log显示问号且双击无法打不开的解决方法
- Mysql8 stored procedure generates calendar table and exception handling
- 边框动态效果实现
- requires_ grad,grad_ FN, the meaning and use of grad 2
- [advanced C language] learning about flexible arrays
- 如何正确使用call、bind、apply
- MySQL basic functions
猜你喜欢
(Applied intelligence-2022) transgait: gait recognition and ensemble transformer based on multimodality
Common management problems and solutions in automation equipment manufacturing industry
Reading notes of top performance version 2 (V) -- Disks monitoring
Use the browser plug-in to run JS to delete the "disable copy" function of a specific website
Elephant Swap的LaaS方案优势分析,致eToken表现强势
ARM+SD2405 IIC_ RTC driver writing and IIC communication protocol
麒麟系统 QT代码 出现 使用未声明的标识符“MainWindow“
如何正确使用call、bind、apply
Easy to use task queue beanstalkd
Border dynamic effect implementation
随机推荐
[mysql] index transactions
Applet project summary
Digital business cloud: under the trend of multiple supplier scenarios, how can garment enterprises build a flexible SRM management system?
mySQL基本函数
Go list 修改元素值
如何将沥青高位槽液位数值无线传输至载热体记录仪监测?
2022/07/20--- convert string to integer; Maximum value of sliding window
webservice接口测试
617. Merge binary tree
Autojs微信研究:多次测试发现偶尔出现调用了click()返回了true,但实际并未点击成功的情况,例如“通讯录”(已解决)
It's a holiday. I need to read books carefully
[MySQL]表的增删查改(基础)
Common management problems and solutions in automation equipment manufacturing industry
【PostgreSQL 15】PostgreSQL 15对UNIQUE和NULL的改进
图表绘制总结
[mysql] basic database operations
leetcode-面试题 17.15:最长单词
2022/07/18------ clockwise printing matrix
[MdSQL]表的增删查改(进阶)
Download picture function, full screen function, copy function