# 5922. Public strings that have been counted once

I haven't done a two-week race for a long time. I didn't expect this first game to run into such a water problem. It makes me give ak an awkward game.

## Title Description

Give you two string arrays words1 and words2, and return the number of strings that occur exactly once in both string arrays.

Example 1:

Input: words1 = ["leetcode","is","amazing","as","is"], words2 = ["amazing","leetcode","is"] Output: 2 Explanation: - "leetcode" Occurs exactly once in both arrays and counts into the answer. - "amazing" Occurs exactly once in both arrays and counts into the answer. - "is" In both arrays, but in words1 There are 2 occurrences, not counting in the answer. - "as" stay words1 Appears once, but words2 No, do not count in the answer. So, two strings happen to occur once in both arrays.

## Solving problems

Create two hash tables to record the frequency of words that appear, then iterate through a hash table to determine if the string only appears once in both.

public int countWords(String[] words1, String[] words2) { Map<String,Integer> wordMap1 = new HashMap<>(); Map<String,Integer> wordMap2 = new HashMap<>(); for(String s:words1){ wordMap1.put(s, wordMap1.getOrDefault(s, 0)+1); } for(String s:words2){ wordMap2.put(s, wordMap2.getOrDefault(s, 0)+1); } int ans = 0; for(String s:wordMap1.keySet()) { if(wordMap1.get(s)==1&&wordMap2.getOrDefault(s, 0)==1) { ans++; } } return ans; }

# 5923. Minimum number of buckets needed to collect rainwater from a house

Give you a string street with subscripts starting at 0. Each character in the street represents either an'H'of the house or an empty'.'.

You can put a bucket in an empty space to collect rainwater from adjacent houses. Buckets located at i - 1 or i + 1 can collect rainwater from houses located at i. A bucket can collect rainwater from two houses if there are houses in two adjacent locations.

Make sure there is at least one bucket next to each house. Return to the minimum number of buckets you need. Return to -1 if there is no solution.

Example 1:

Input: street = "H..H" Output: 2 Explanation: We can put the water buckets in the labels 1 and 2 below. "H..H" -> "HBBH"('B' Represents placing a bucket). Houses marked 0 have buckets on the right and houses marked 3 have buckets on the left. So every house has at least one bucket next to it to collect rainwater.

## Solving problems

My idea is to be greedy. If the current H judges whether the right pass can be put, it can be put, it can't be put to the left, and it can't be put back to -1.

public int minimumBuckets(String street) { int[] nums = new int[street.length()]; for(int i=0;i<street.length();i++) { if(street.charAt(i)=='H') { if(i>=1&&nums[i-1]==1) { continue; } if(i==street.length()-1) { if(i<1||street.charAt(i-1)=='H') { return -1; } nums[i-1]=1; }else { if(street.charAt(i+1)=='H') { if(i<1||street.charAt(i-1)=='H') { return -1; } nums[i-1]=1; }else { nums[i+1]=1; } } } } int ans=0; for(int i=0;i<nums.length;i++) { if(nums[i]==1) { ans++; } } return ans; }

# 5924. Minimum cost for a robot to go home in the grid

Give you a grid graph of m x n, where (0, 0) is the leftmost top grid, (m-1, n-1) is the rightmost bottom grid. Give you an integer array startPos, startPos = [startrow, startcol] to indicate that there was initially a robot in the grid (startrow, startcol). Also give you an integer array homePos, homePos = [homerow, homecol] to indicate that the robot's home is in a grid (homerow, homecol).

The robot needs to go home. Each step it can move in four directions: up, down, left and right, while the robot cannot move out of the boundary. There is a price to pay for every move. Give you two more integer arrays with subscripts starting at 0: the rowCosts with length m and the colCosts with length n.

If the robot moves up or down to the grid in row r, the cost is rowCosts[r].

If the robot moves left or right to the grid in column c, the cost is colCosts[c].

The minimum total cost of returning the robot home.

Example 1:

Input: startPos = [1, 0], homePos = [2, 3], rowCosts = [5, 4, 3], colCosts = [8, 2, 6, 7] Output: 18 Interpretation: An optimal path is: from (1, 0) start -> Go down to (2, 0) . The price is rowCosts[2] = 3 . -> Go right (2, 1) . The price is colCosts[1] = 2 . -> Go right (2, 2) . The price is colCosts[2] = 6 . -> Go right (2, 3) . The price is colCosts[3] = 7 . The total cost is 3 + 2 + 6 + 7 = 18

## Solving problems

This question is out of line. At first, I only thought about using bfs. Later, I found that this is not only the shortest path.

I don't know what this question is about to examine.

public static int minCost(int[] startPos, int[] homePos, int[] rowCosts, int[] colCosts) { int ans = 0; if(startPos[0]<homePos[0]) { for(int i=startPos[0]+1;i<=homePos[0];i++) { ans+=rowCosts[i]; } } if(startPos[0]>homePos[0]) { for(int i=homePos[0];i<startPos[0];i++) { ans+=rowCosts[i]; } } if(startPos[1]<homePos[1]) { for(int i=startPos[1]+1;i<=homePos[1];i++) { ans+=colCosts[i]; } } if(startPos[1]>homePos[1]) { for(int i=homePos[1];i<startPos[1];i++) { ans+=colCosts[i]; } } return ans; }

# 5925. Count the number of fertile pyramids on the farm

## Title Description

There is a rectangular grid of farms divided into cells in m rows and n columns. Each grid is either fertile (in 1) or barren (in 0). All and grids except the grid are considered barren.

Pyramid areas on farms are defined as follows:

The number of grids in the area is greater than 1 and all grids are fertile.

The top of the pyramid is the top grid of the pyramid. The height of the pyramid is the number of rows it covers. If (r, c) is the top of the pyramid and the height is h, then any grid (i, j) contained within the pyramid area must satisfy R <= I <= R + H - 1 and C - (i - r) <= J <= C + (i - r).

An inverted pyramid is similarly defined as follows:

The number of grids in the area is greater than 1 and all grids are fertile.

The top of the inverted pyramid is the lowest grid of the inverted pyramid. The height of the inverted pyramid is the number of rows it covers. If (r, c) is the top of the pyramid and the height is h, then any lattice (i, j) contained within the pyramid area must satisfy R-H + 1 <= I <= R and C - (r - i) <= J <= C + (r - i).

The following figure shows some of the pyramid areas that are defined and undefined. The black areas represent fertile grids.

Give you a binary matrix grid with subscripts starting at 0 and size of m x n, which represents a farm. Please return the total number of pyramids and inverted pyramids in the grid.

Example 1:

Input: grid = [[0,1,1,0],[1,1,1,1]] Output: 2 Explanation: 2 The possible pyramid areas are shown in the blue and red areas shown above. There is no inverted pyramid area in this grid. So the total number of pyramid areas is 2 + 0 = 2 .

## Solving problems

It's also a ridiculous question, as direct violence can go through it. Judge whether a pyramid can be built at each point, and add a radius if you can.

public int countPyramids(int[][] grid) { int ans = 0; int[][] grid2 = new int[grid.length][grid[0].length]; for(int i=0;i<grid.length;i++) { grid2[i] = grid[grid.length-i-1]; } for(int i=0;i<grid.length;i++) { for(int j=0;j<grid[i].length;j++) { if(grid[i][j]==1) { int r = 0; if((r=isOk(grid,i,j))>0) { ans+=r; } if((r=isOk(grid2,i,j))>0) { ans+=r; } } } } return ans; } private int isOk(int[][] grid, int x, int y) { int r = 1; while(true) { if(isOok(x,y,r,grid)) { r++; }else { return r-1; } } } private boolean isOok(int x, int y, int r, int[][] grid) { int row = x+r; if(row>=grid.length||y-r<0||y+r>=grid[row].length) { return false; } for(int i=y-r;i<=y+r;i++) { if(grid[row][i]!=1) { return false; } } return true; }

After this week's match, there was no sense of accomplishment at all.

Record it, after all, it's ak.