Skip to content

Commit 8b40a17

Browse files
committed
feat: solve No.1266,2264
1 parent 0023580 commit 8b40a17

File tree

2 files changed

+189
-0
lines changed

2 files changed

+189
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# 1266. Minimum Time Visiting All Points
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Array, Math, Geometry.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
On a 2D plane, there are `n` points with integer coordinates `points[i] = [xi, yi]`. Return **the **minimum time** in seconds to visit all the points in the order given by **`points`.
10+
11+
You can move according to these rules:
12+
13+
14+
In `1` second, you can either:
15+
16+
17+
18+
- move vertically by one unit,
19+
20+
- move horizontally by one unit, or
21+
22+
- move diagonally `sqrt(2)` units (in other words, move one unit vertically then one unit horizontally in `1` second).
23+
24+
25+
26+
- You have to visit the points in the same order as they appear in the array.
27+
28+
- You are allowed to pass through points that appear later in the order, but these do not count as visits.
29+
30+
31+
 
32+
Example 1:
33+
34+
![](https://assets.leetcode.com/uploads/2019/11/14/1626_example_1.PNG)
35+
36+
```
37+
Input: points = [[1,1],[3,4],[-1,0]]
38+
Output: 7
39+
Explanation: One optimal path is [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0]
40+
Time from [1,1] to [3,4] = 3 seconds
41+
Time from [3,4] to [-1,0] = 4 seconds
42+
Total time = 7 seconds
43+
```
44+
45+
Example 2:
46+
47+
```
48+
Input: points = [[3,2],[-2,2]]
49+
Output: 5
50+
```
51+
52+
 
53+
**Constraints:**
54+
55+
56+
57+
- `points.length == n`
58+
59+
- `1 <= n <= 100`
60+
61+
- `points[i].length == 2`
62+
63+
- `-1000 <= points[i][0], points[i][1] <= 1000`
64+
65+
66+
67+
## Solution
68+
69+
```javascript
70+
/**
71+
* @param {number[][]} points
72+
* @return {number}
73+
*/
74+
var minTimeToVisitAllPoints = function(points) {
75+
return points.reduce((sum, point, index) => {
76+
if (index === 0) return 0;
77+
var diffX = Math.abs(point[0] - points[index - 1][0]);
78+
var diffY = Math.abs(point[1] - points[index - 1][1]);
79+
var second = Math.max(diffX, diffY);
80+
return sum + second;
81+
}, 0);
82+
};
83+
```
84+
85+
**Explain:**
86+
87+
nope.
88+
89+
**Complexity:**
90+
91+
* Time complexity : O(n).
92+
* Space complexity : O(1).
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# 2264. Largest 3-Same-Digit Number in String
2+
3+
- Difficulty: Easy.
4+
- Related Topics: String.
5+
- Similar Questions: Largest Odd Number in String.
6+
7+
## Problem
8+
9+
You are given a string `num` representing a large integer. An integer is **good** if it meets the following conditions:
10+
11+
12+
13+
- It is a **substring** of `num` with length `3`.
14+
15+
- It consists of only one unique digit.
16+
17+
18+
Return **the **maximum good **integer as a **string** or an empty string **`""`** if no such integer exists**.
19+
20+
Note:
21+
22+
23+
24+
- A **substring** is a contiguous sequence of characters within a string.
25+
26+
- There may be **leading zeroes** in `num` or a good integer.
27+
28+
29+
 
30+
Example 1:
31+
32+
```
33+
Input: num = "6777133339"
34+
Output: "777"
35+
Explanation: There are two distinct good integers: "777" and "333".
36+
"777" is the largest, so we return "777".
37+
```
38+
39+
Example 2:
40+
41+
```
42+
Input: num = "2300019"
43+
Output: "000"
44+
Explanation: "000" is the only good integer.
45+
```
46+
47+
Example 3:
48+
49+
```
50+
Input: num = "42352338"
51+
Output: ""
52+
Explanation: No substring of length 3 consists of only one unique digit. Therefore, there are no good integers.
53+
```
54+
55+
 
56+
**Constraints:**
57+
58+
59+
60+
- `3 <= num.length <= 1000`
61+
62+
- `num` only consists of digits.
63+
64+
65+
66+
## Solution
67+
68+
```javascript
69+
/**
70+
* @param {string} num
71+
* @return {string}
72+
*/
73+
var largestGoodInteger = function(num) {
74+
var maxDigit = '';
75+
var digitLength = 0;
76+
for (var i = 0; i < num.length; i++) {
77+
if (num[i] === num[i - 1]) {
78+
digitLength += 1;
79+
if (digitLength >= 3 && num[i] > maxDigit) {
80+
maxDigit = num[i];
81+
}
82+
} else {
83+
digitLength = 1;
84+
}
85+
}
86+
return maxDigit.repeat(3);
87+
};
88+
```
89+
90+
**Explain:**
91+
92+
nope.
93+
94+
**Complexity:**
95+
96+
* Time complexity : O(n).
97+
* Space complexity : O(1).

0 commit comments

Comments
 (0)