Topic 1
Force buckle: 2087. Minimum cost of robot home in grid graph
thinking
When you get the title, you must read it first!!!!! To sum up: This is the brain turn to TM....
1. At first glance, it is very similar to our usual robot walking problem. We must read the question carefully!
2. His cost calculation rule is to go to line I and spend rowCosts[i], only one step at a time. Then I go directly from the current line to the destination without detour, and the line I pass is fixed
3. If we don't go there directly, instead of going there directly, the above expenses will be inevitable, and there will be more expenses than useless
4. In conclusion, the direct cost is the smallest.
code
class Solution { public: int minCost(vector<int>& startPos, vector<int>& homePos, vector<int>& rowCosts, vector<int>& colCosts) { if (startPos[0] == homePos[0] && startPos[1] == homePos[1]) { return 0; } int sx = startPos[0], sy = startPos[1];//Start row int hx = homePos[0], hy = homePos[1];//Terminate row and column int ans = 0; //that 's ok if (sx <= hx) {//Judge whether it is small to large or large to small for (int i = sx + 1; i <= hx; i++) { ans += rowCosts[i]; } } else { for (int i = sx - 1; i >= hx; i--) { ans += rowCosts[i]; } } //column if (sy <= hy) {//Judge whether it is small to large or large to small for (int i = sy + 1; i <= hy; i++) { ans += colCosts[i]; } } else { for (int i = sy - 1; i >= hy; i--) { ans += colCosts[i]; } } return ans; } };
All codes pass the buckle test
(after multiple tests, the shortest time is):
Topic 2
Force buckle: 2086. Minimum number of buckets required to collect rainwater from the house
Idea: greedy method
1. We should put the bucket on the open space to receive the water from the eaves, and the bucket should be at least, so only the open space beside the house is meaningful. If there is no open space on both sides of a house, there is no solution
2. At the same time, not all the open spaces beside the house should remember our purpose to minimize the barrels, so a barrel should be used as much as possible
3. Therefore, when examining the value of an open space, if there are houses on both sides and there is no bucket next to the two houses, it is 2, otherwise it is 1, and 0 means it can't be put.
4. Finally, each time the bucket is put on the side with high value.
5. At this point, the analysis is completed.
code
class Solution { public: int minimumBuckets(string street) { unordered_set<int> item;//All open space locations, disappear after draining the bucket unordered_set<int> item_H;//It is also the location of all houses for fast matching unordered_set<int> right;//A house with a bucket vector<int> H;//Save all house locations int ans = 0; for (int i = 0; i < street.size(); i++) {//initialization if (street[i] == 'H') { H.push_back(i); item_H.insert(i); } else { item.insert(i); } } for (int i = 0; i < H.size(); i++) { if (right.count(H[i]) == 0) {//A house with a bucket ans++; int pos_l = H[i] - 1;//Left and right bucket positions int pos_r = H[i] + 1; int cnt_l = 0;//value int cnt_r = 0; if (item.count(pos_l) > 0) {//Can hold a bucket if (item_H.count(pos_l - 1) > 0 && right.count(pos_l - 1) == 0) {//There are rooms without buckets on the left and right sides of the bucket cnt_l = 2; } else { cnt_l = 1; } } if (item.count(pos_r) > 0) {//The same is true for the right side if (item_H.count(pos_r + 1) > 0 && right.count(pos_l + 1) == 0) { cnt_r = 2; } else { cnt_r = 1; } } if (cnt_l == 0 && cnt_r == 0) {//Not on either side. unsolvable return -1; } else if (cnt_l > cnt_r) {//Pick the side with the greatest value item.erase(pos_l);//Erase position if (cnt_l == 2) { right.insert(H[i]); right.insert(H[i] - 2); } else { right.insert(H[i]); } } else { item.erase(pos_r); if (cnt_r == 2) { right.insert(H[i]); right.insert(H[i] + 2); } else { right.insert(H[i]); } } } } return ans; } };
All codes pass the buckle test
(after multiple tests, the shortest time is):
Topic 3
Force buckle: 2085. Count the public strings that have appeared once
thinking
Violence simulation
code
class Solution { public: int countWords(vector<string>& words1, vector<string>& words2) { unordered_map<string, int> item1; unordered_map<string, int> item2; int ans = 0; for (string tmp : words1) { item1[tmp]++; } for (string tmp : words2) { item2[tmp]++; } for (int i = 0; i < words1.size(); i++) { if (item1[words1[i]] == 1) { if (item2[words1[i]] == 1) { ans++; } } } return ans; } };
All codes pass the buckle test
(after multiple tests, the shortest time is):