# BFS and DP -- the 70th biweekly match of Li Kou

Posted by sparrrow on Wed, 02 Feb 2022 18:05:46 +0100

⭐ New pit in winter vacation -- daily question notes of code Fox
First question, greedy thought (take the largest two at a time, and then the largest third for free - the amount of sugar you can take for free is limited. Why not take one expensive one for free)
The second problem is mathematical thinking (all numbers are in a range, so you only need to record the maximum and minimum values. The difference array indicates that each value changes uniformly, and the maximum and minimum values are unified + n/-n; you can find the possibility to meet the range)
The third question, BFS (I have to simplify my code structure and sort out the classic methods 😢)
Question 4, DP (there's nothing to say, not even as good as question 3)

### 2144. Minimum cost of buying candy at a discount

A shop is selling candy at a discount. Every time you buy two sweets, the store will give you one free candy.

The only limitation of free candy is that its price needs to be less than or equal to the smaller value of the two candy prices purchased.

• For example, there are four candies with prices of 1, 2, 3 and 4 respectively. If a customer buys candies with prices of 2 and 3, he can get candy with price of 1 for free, but can't get candy with price of 4.

Give you an integer array cost with subscript starting from 0, where cost[i] represents the price of the ith candy. Please return the minimum total cost of obtaining all candy.

```class Solution {
public int minimumCost(int[] cost) {
Arrays.sort(cost);
int i=cost.length-1;
int sum=0;
while(i>=0){
sum+=cost[i--];
if(i>=0)
sum+=cost[i--];
if(i>=0){
i--;
}
}
return sum;
}
}
```

### 2145. Count the number of hidden arrays - Mid

Give you an integer array differences with subscript starting from 0 and length n, which represents the difference between adjacent elements of a hidden array with length n + 1. A more formal expression is: if we record the hidden array as hidden, then differences[i] = hidden[i + 1] - hidden[i].

Give you two integers lower and upper at the same time. They mean that the values of all numbers in the hidden array are between the closed interval [lower, upper].

• For example,

```differences = [1, -3, 4]
```

```lower = 1
```

```upper = 6
```

, then the hidden array is a length of

```4
```

And all values are in

```1
```

and

```6
```

(including both).

• [3, 4, 1, 5] and [4, 5, 2, 6] are all qualified hidden arrays.
• It does not meet the requirements because it contains elements greater than 6, 6, 6.
• [1, 2, 3, 4] does not meet the requirements because the difference between adjacent elements does not meet the given data.

Please return the number of qualified hidden arrays. If there is no qualified hidden array, please return 0.

```class Solution {
public int numberOfArrays(int[] differences, int lower, int upper) {
int max=0;
int min=0;
int dp=0;
for(int i:differences){
dp+=i;
if(dp<min){
min=dp;
}
if(dp>max){
max=dp;
}
//Prune, or you may cross the border
if(upper-lower-(max-min)+1<0){
return 0;
}
}
return upper-lower-(max-min)+1<0?0:upper-lower-(max-min)+1;
}
}
```

### 2146. K-sample items with the highest ranking in the price range - Mid

Give you a two-dimensional integer array grid with subscript starting from 0. Its size is m x n, which represents the distribution map of items in a store. The meanings of integers in the array are:

• 0 indicates a wall that cannot be crossed.
• 1 indicates an empty grid that can pass freely.
• All other positive integers represent the price of the same item in the grid. You are free to pass through these squares.

It takes one step to go from one grid to the upper, lower, left and right adjacent grids.

At the same time, give you an integer array pricing and start, where pricing = [low, high] and start = [row, col], indicating that your starting position is (row, col), and you are only interested in items whose price is within the closed range [low, high]. And give you an integer k.

You want to know the location of the top k items in a given range. The ranking is based on the following rules from high to low priority:

1. Distance: defined as the number of steps required for the shortest path from start to an item (higher ranking for closer distances).
2. Price: items with lower prices have higher priority, but only the prices within a given range are considered.
3. Row coordinates: smaller row coordinates have higher priority.
4. Column coordinates: smaller column coordinates have higher priority.

Please return the coordinates of the top k items in the given price, sort them according to the ranking and return. If there are less than k items in a given price, please return all their coordinates.

```class Solution {
public List<List<Integer>> highestRankedKItems(int[][] grid, int[] pricing, int[] start, int k) {
List<int[]> listAns=new ArrayList<>();
List<int[]> listCur=new ArrayList<>();
int countCur=0;
int countAns=0;
int m=grid.length;
int n=grid.length;
int[][] d=new int[][]{{-1,0},{1,0},{0,-1},{0,1}};

int p=grid[start][start];
if(p<=pricing&&p>=pricing){
countAns++;
}

grid[start][start]=0;
countCur++;
int step=0;

while(!listCur.isEmpty()){
//countCur the current number of steps and the number of nodes to go through. listCur saves the nodes
step++;
int midCount=countCur;
countCur=0;
while(midCount-->0){
int[] curPoint=listCur.remove(0);

for(int[] i:d){
int x=curPoint+i;
int y=curPoint+i;

if(x<0||x>=m||y<0||y>=n){
continue;
}
int price=grid[x][y];
if(price==0){
continue;
}

if(price>=pricing&&price<=pricing){
countAns++;
}

grid[x][y]=0;
countCur++;
}
}

if(countAns>=k){
break;
}
}

int[][] listA=new int[listAns.size()];
for(int i=0;i<listAns.size();i++){
listA[i]=listAns.get(i);
}
Arrays.sort(listA,new MyComparator());

List<List<Integer>> ans=new ArrayList<>();
int need=0;
for(int[] i:listA){
List<Integer> mid=new ArrayList<>();
if(++need>=k){
break;
}
}
return ans;
}

class MyComparator implements Comparator<int[]>{
public int compare(int[] a, int[] b) {
if(a>b){
return 1;
}
else if(a==b){
if(a>b){
return 1;
}
else if(a==b){
if(a>b){
return 1;
}
else if(a==b){
if(a>b){
return 1;
}
}
}
}
return -1;
}
}
}
```

### 2147. Number of schemes for separated corridors - Hard

In the corridor of a library, there are some seats and decorative plants lined up. Give you a string corridor with subscript starting from 0 and length of n, which contains the letters'S' and 'P', where each'S' represents a seat and each 'P' represents a plant.

A screen has been placed to the left of subscript 0 and to the right of subscript n - 1. You need to put some extra screens. At most one screen can be placed between each position i - 1 and i (1 < = i < = n - 1).

Please divide the corridor into several sections with screens, and there are exactly two seats in each section, and the number of plants in each section is not required. There may be a variety of partition schemes. If any of the two schemes has a different screen position, they are regarded as different schemes.

Please return to the number of schemes for dividing the corridor. Since the answer may be large, please return the result of its remainder of 109 + 7. If there is no scheme, please return 0.

```class Solution {
public int numberOfWays(String corridor) {
int i=0;
int midCountP=-1;
long ans=1;
while(i<corridor.length()){
//Find two chairs
int midCountTwoC=0;
while(midCountTwoC!=2&&i<corridor.length()){
if(corridor.charAt(i)=='S'){
midCountTwoC++;
}
i++;
}
//If there are not enough two, it means that it is not an even number, it is impossible to score successfully, or there is no chair, and the score method is 0
if(midCountTwoC!=2){
return 0;
}

//Count the score before the current position
if(midCountP!=-1)
ans=(ans*(midCountP+1))%1000000007;
midCountP=0;

//Count the number of consecutive plants after this position, and insert the position of the screen + 1
while(i<corridor.length()){
if(corridor.charAt(i)=='P'){
midCountP++;
i++;
}
else{
break;
}
}

}

return (int)ans;
}
}
```