Delete the sequence of inscriptions
【 答 案 】
You are given a string s, which consists only of the characters ‘a’ and ‘b’. Each deletion removes a sequence of subrinons from S.
Returns the minimum number of deletions to delete all of the empty characters of the given string.
Subsequence Definition: A string is a subsequence of the original string if it can be obtained by deleting some characters from the original string without changing the character order.
Palindrome definition: A string is a palindrome if it is read backwards and forwards identically.
First, when s is a palindrome string, the minimum number of deletes is naturally 1.
If s is not a palindrome string, you can delete some characters from the original string. Then for any length of S, you can delete all b’s and get a sequence of all A’s.
So the minimum number of deletions for s not being a palindrome string is 2.
1333. Restaurant filter
【Medium】
Restaurants [I] = [idi, Ratingi, veganFriendlyi, Pricei, distancei] You must use the following three filters to filter the restaurant information.
The vegan-friendly filter can be true or false, which means you should only include restaurants where veganFriendlyi is true, and false means you can include any restaurant. In addition, we also have two filters, maxPrice and maxDistance, which take into account the maximum price factor and distance factor of the restaurant respectively.
After filtering, the restaurant ID is returned and sorted in ascending order of rating. If the rating is the same, sort by ID from highest to bottom. For simplicity, veganFriendlyi and veganFriendly are set to 1 if true and 0 if false.
This passage mainly examines the following two knowledge points:
-
Basic operations on arrays.
-
Multi-conditional sort.
The title description is more detailed, read carefully can be solved.
The city with the fewest neighbors within the threshold distance
【Medium】
There are n cities, numbered from 0 to n-1. Edges [I] = [fromi, toi, weighti] represents the bidirectional weighted edges between fromi and toi. The distanceThreshold is an integer distanceThreshold.
Returns the least number of other cities that can be reached through some paths, and the path distance is larger than the distanceThreshold. If there are more than one such city, the city with the largest number is returned.
Note that the distance of the path connecting cities I and J is equal to the sum of the weights of all the edges along the path.
How many cities can be reached by a city? How many cities can be reached by a city? How many cities can be reached by a city?
The structure of city map in this problem belongs to undirected positive weight graph, so you can choose Floyd-Warshall algorithm or Dijkstra algorithm.
Here we choose to implement the relatively simple Freudian algorithm.
The first step is to use a two-dimensional array to record the distances between vertices:
Then update the array I times to find out if [j, k] has a shortest path from [j, I] to [I, k] :
Finally, select the cities with the fewest neighbors:
1335. Minimum difficulty of work plan
You need to make a d – day work schedule. There are dependencies between the jobs, in order to perform the I job, you must complete all j jobs (0 <= j < I).
You need to complete at least one task each day. The total difficulty of the work plan is the sum of the difficulty of each day of the D day, and the difficulty of the work of this day is the maximum difficulty of the work that should be completed that day. You are given an integer array jobDifficulty and an integer D, representing the difficulty of the work and the number of days to plan, respectively. Task I is jobDifficulty[I].
Returns the minimum difficulty of the entire work plan. If a work plan cannot be made, -1 is returned.
What is the relationship between I and D?
The first case: I < d, unable to make a work plan, return -1.
In the second case: I == D, only one task can be completed every day, so the lowest difficulty is the sum of the difficulties of I tasks.
In the third case: I > d, take I = [6,5,4,3,2,1], d = 2 as an example, there are the following 5 arrangements.
-
[6], [5, 4, 3, 1]
-
[6, 5], [4, 3, 2, 1]
-
[6, 5, 4], [3, 2, 1]
-
[6, 5, 4, 3],[2, 1]
-
[6, 5, 4, 3, 2], [1]
So for any d and I (I > d), it is actually comparing the minimum difficulty of D-1 day and the maximum difficulty of the current day, there are a total of I-d + 1 possibilities (at least one task is completed every day), then the recursive formula is:
Therefore, the question is a standard dynamic programming question type, the specific solution code is as follows: