1266. Minimum time to visit all points
There plane n
point, the position of the point represented by integer coordinates points[i] = [xi, yi]
. Please calculate the minimum time (in seconds) required to access all these points.
You can move on the plane according to the following rules:
- Every second moves one unit length in the horizontal or vertical direction, or crosses the diagonal (can be seen as moving one unit length in the horizontal and vertical directions in one second).
- These points must be accessed in the order in which they appear in the array.
Example 1
Input : points = [[1,1], [3,4], [-1,0]]
Output : 7
Explanation : An optimal access path is: [1,1] -> [2,2]- > [3,3]-> [3,4] -> [2,3]-> [1,2]-> [0,1]-> [-1,0]
from [1,1] to [ 3,4] takes 3 seconds
From [3,4] to [-1,0] takes 4 seconds A
total of 7 seconds
Example 2
Input : points = [[3,2], [-2,2]]
Output : 5
prompt
points.length == n
1 <= n <= 100
points[i].length == 2
-1000 <= points[i][0], points[i][1] <= 1000
Problem-solving ideas
[1,1]-> [2,2] Move two steps in 1 second
The time required to move between the coordinates: the maximum value of the difference between the two coordinates x and y
This Java solution is 100%, 100%♪(^∇^*)
Solution JAVA implementation
Click to view the source code of this week's problem solving
public int minTimeToVisitAllPoints(int[][] points) {
int result = 0;
for (int i = 1; i < points.length; i++) {
int x = Math.abs(points[i][0] - points[i - 1][0]);
int y = Math.abs(points[i][1] - points[i - 1][1]);
result += Math.max(x, y);
}
return result;
}
1267. Statistics of servers participating in communication
There is a distribution server, the server identifies the location m * n
of the integer matrix grid grid
, there is a server on a cell represents 1, 0 indicates no.
If the two servers are in the same row or column, we think they can communicate.
Please count and return the number of servers that can communicate with at least one other server.
Example 1
Input : grid = [[1,0], [0,1]]
Output : 0
Explanation : No server can communicate with other servers.
Example 2
Input : grid = [[1,0], [1,1]]
Output : 3
Explanation : All these servers can communicate with at least one other server.
Example 3
Input : grid = [[1,1,0,0], [0,0,1,0], [0,0,1,0], [0,0,0,1]]
Output : 4
Explanation : The two servers in the first row communicate with each other, and the two servers in the third column communicate with each other, but the server in the lower right corner cannot communicate with other servers.
prompt
m == grid.length
n == grid[i].length
1 <= m <= 250
1 <= n <= 250
grid[i][j] == 0 or 1
Problem-solving ideas
Calculate the number of machines in each row and column and all machine coordinates
Circulate the machine coordinates to determine whether the row and column are only one
Solution JAVA implementation
Click to view the source code of this week's problem solving
public int countServers(int[][] grid) {
Map<Integer, Integer> mapx = new HashMap<>();
Map<Integer, Integer> mapy = new HashMap<>();
List<Integer[]> list = new ArrayList<>();
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 1) {
mapx.put(i, mapx.getOrDefault(i, 0) + 1);
mapy.put(j, mapy.getOrDefault(j, 0) + 1);
list.add(new Integer[]{i, j});
}
}
}
int result = 0;
for (Integer[] integers : list) {
if (mapx.get(integers[0]) > 1 || mapy.get(integers[1]) > 1) {
result++;
}
}
return result;
}
1268. Search recommendation system
You are given a product array products and a string searchWord, each product in the products array is a string.
Please design a recommendation system. After entering each letter of the word searchWord in turn, recommend up to three products with the same prefix as searchWord in the products array. If there are more than three recommended products with the same prefix, please return the smallest three in lexicographical order.
Please return the list of recommended products after entering each letter of searchWord in the form of a two-dimensional list.
Example 1
Input : products = ["mobile", "mouse", "moneypot", "monitor", "mousepad"], searchWord = "mouse"
Output : [
["mobile", "moneypot", "monitor"],
[" mobile "," moneypot "," monitor "],
[" mouse "," mousepad "],
[" mouse "," mousepad "],
[" mouse "," mousepad "]
]
Explanation : after lexicographic order The product list is ["mobile", "moneypot", "monitor", "mouse", "mousepad"]
Enter m and mo, because all products have the same prefix, the system returns the three products with the smallest lexicographic order ["mobile "," Moneypot "," monitor "] After
entering mou, mous and mouse, the system will return [" mouse "," mousepad "]
Example 2
输入:products = [“havana”], searchWord = “havana”
输出:[[“havana”],[“havana”],[“havana”],[“havana”],[“havana”],[“havana”]]
Example 3
输入:products = [“bags”,“baggage”,“banner”,“box”,“cloths”], searchWord = “bags”
输出:[[“baggage”,“bags”,“banner”],[“baggage”,“bags”,“banner”],[“baggage”,“bags”],[“bags”]]
Example 4
Input : products = [“havana”], searchWord = “tatiana”
Output : [[], [], [], [], [], [], []]
prompt
1 <= products.length <= 1000
1 <= Σ products[i].length <= 2 * 10^4
products[i]
All characters in are lowercase English letters.1 <= searchWord.length <= 1000
searchWord
All characters in are lowercase English letters.
Problem-solving ideas
- Now sort the data, find the word with the same initial letter as the search word, and enter and exit the list
- Loop to find the first 0.1.2… searchWord.length-1 letter from the List and find the same word
- List more than 3 words, return the first three, no more than 3 words, return List
Solution JAVA implementation
Click to view the source code of this week's problem solving
public List<List<String>> suggestedProducts(String[] products, String searchWord) {
List<List<String>> result = new ArrayList<>(searchWord.length());
List<String> list = new ArrayList<>(products.length);
Arrays.sort(products);
char first = searchWord.toCharArray()[0];
for (String product : products) {
if (product.toCharArray()[0] == first) {
list.add(product);
}
}
for (int i = 0; i < searchWord.length(); i++) {
List<String> l = new ArrayList<>();
list = search(list, searchWord, i);
int size = list.size();
for (int j = 0; j < Math.min(size, 3); j++) {
l.add(list.get(j));
}
result.add(l);
}
return result;
}
public List<String> search(List<String> products, String searchWord, int x) {
List<String> list = new ArrayList<>();
for (String product : products) {
if (product.toCharArray().length > x && product.toCharArray()[x] == searchWord
.toCharArray()[x]) {
list.add(product);
}
}
return list;
}
1269. Number of options stopped in place
A length arrLen
of the array, a pointer index began 0
at.
In each step, you can move the pointer to the left or right 1
step, or stop in place (the pointer can not be moved out of range of the array).
Give you two integers steps
and arrLen
, you calculate and return: in just executed steps
after operations, the pointer still points to the index 0
number at the program.
Since the answer may be very large, please return program number mode 10^9 + 7
results after.
Example 1
Input : steps = 3, arrLen = 2
Output : 4
Explanation : After 3 steps, a total of 4 different methods can be stopped at index 0.
Right, left, immobile
immobile, right, left impressed
right, immobile, immobilized left
, immobile, immobile
Example 2
Input : steps = 2, arrLen = 4
Output : 2
Explanation : After 2 steps, there are a total of 2 different methods that can be stopped at index 0. Move
right, move left
Example 3
Input : steps = 4, arrLen = 2
Output : 8
prompt
1 <= steps <= 500
1 <= arrLen <= 10^6
Problem-solving ideas
If the first step X to stay in Y at the
The first X-1 Step should stay in the Y-1 or Y or Y + 1 at a
And so on (need to judge whether Y-1 and Y + 1 cross the boundary)
Solution JAVA implementation
Click to view the source code of this week's problem solving
public int numWays(int steps, int arrLen) {
Long mod = 1000000007L;
long[][] result = new long[steps + 1][steps + 1];
int min = Math.min(arrLen, steps);
result[0][0] = 1L;
for (int i = 1; i <= steps; i++) {
for (int j = 0; j < min; j++) {
// 1. 第i-1步到位置j,i步停留原地j
result[i][j] = result[i - 1][j];
if (j != 0) {
// 3. 第i-1步到位置j-1,i步右移到j
result[i][j] += result[i - 1][j - 1];
}
if (j != min - 1) {
// 2. 第i-1步到位置j+1,i步左移到j
result[i][j] += result[i - 1][j + 1];
}
result[i][j] %= mod;
}
}
return (int) result[steps][0];
}