forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
38 lines (29 loc) · 979 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/// Source : https://leetcode.com/problems/squirrel-simulation/
/// Author : liuyubobobo
/// Time : 2021-02-01
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
/// Brute Force
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int minDistance(int height, int width, vector<int>& tree, vector<int>& squirrel, vector<vector<int>>& nuts) {
vector<int> treeToNuts, squToNuts;
for(const vector<int>& nut: nuts) {
treeToNuts.push_back(abs(nut[0] - tree[0]) + abs(nut[1] - tree[1]));
squToNuts.push_back(abs(nut[0] - squirrel[0]) + abs(nut[1] - squirrel[1]));
}
int total = accumulate(treeToNuts.begin(), treeToNuts.end(), 0);
int res = INT_MAX;
for(int i = 0; i < nuts.size(); i ++){
res = min(res, squToNuts[i] + treeToNuts[i] + total * 2 - 2 * treeToNuts[i]);
}
return res;
}
};
int main() {
return 0;
}