Skip to content

Commit 9a2661b

Browse files
authored
feat: add solutions to lcof2 problems: No.035,039 (doocs#1403)
* No.035.最小时间差 * No.039.直方图最大矩形面积
1 parent da71a72 commit 9a2661b

File tree

12 files changed

+446
-101
lines changed

12 files changed

+446
-101
lines changed

lcof2/剑指 Offer II 035. 最小时间差/README.md

+44-21
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,18 @@
3939

4040
<!-- 这里可写通用的实现逻辑 -->
4141

42-
我们注意到,时间点最多只有 `24 * 60` 个,因此,当 timePoints 长度超过 `24 * 60`,说明有重复的时间点,提前返回 0。
42+
**方法一:排序**
4343

44-
接下来:
44+
我们注意到,时间点最多只有 $24 \times 60$ 个,因此,当 $timePoints$ 长度超过 $24 \times 60$,说明有重复的时间点,提前返回 $0$。
4545

46-
首先,遍历时间列表,将其转换为“分钟制”列表 `mins`,比如,对于时间点 `13:14`,将其转换为 `13 * 60 + 14`
46+
接下来,我们首先遍历时间列表,将其转换为“分钟制”列表 $mins$,比如,对于时间点 `13:14`,将其转换为 $13 \times 60 + 14$
4747

48-
接着将“分钟制”列表按升序排列,然后将此列表的最小时间 `mins[0]` 加上 `24 * 60` 追加至列表尾部,用于处理最大值、最小值的差值这种特殊情况。
48+
接着将“分钟制”列表按升序排列,然后将此列表的最小时间 $mins[0]$ 加上 $24 \times 60$ 追加至列表尾部,用于处理最大值、最小值的差值这种特殊情况。
4949

5050
最后遍历“分钟制”列表,找出相邻两个时间的最小值即可。
5151

52+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为时间点个数。
53+
5254
<!-- tabs:start -->
5355

5456
### **Python3**
@@ -62,10 +64,7 @@ class Solution:
6264
return 0
6365
mins = sorted(int(t[:2]) * 60 + int(t[3:]) for t in timePoints)
6466
mins.append(mins[0] + 24 * 60)
65-
res = mins[-1]
66-
for i in range(1, len(mins)):
67-
res = min(res, mins[i] - mins[i - 1])
68-
return res
67+
return min(b - a for a, b in pairwise(mins))
6968
```
7069

7170
### **Java**
@@ -85,11 +84,11 @@ class Solution {
8584
}
8685
Collections.sort(mins);
8786
mins.add(mins.get(0) + 24 * 60);
88-
int res = 24 * 60;
87+
int ans = 1 << 30;
8988
for (int i = 1; i < mins.size(); ++i) {
90-
res = Math.min(res, mins.get(i) - mins.get(i - 1));
89+
ans = Math.min(ans, mins.get(i) - mins.get(i - 1));
9190
}
92-
return res;
91+
return ans;
9392
}
9493
}
9594
```
@@ -100,17 +99,20 @@ class Solution {
10099
class Solution {
101100
public:
102101
int findMinDifference(vector<string>& timePoints) {
103-
if (timePoints.size() > 24 * 60)
102+
if (timePoints.size() > 24 * 60) {
104103
return 0;
104+
}
105105
vector<int> mins;
106-
for (auto t : timePoints)
106+
for (auto& t : timePoints) {
107107
mins.push_back(stoi(t.substr(0, 2)) * 60 + stoi(t.substr(3)));
108+
}
108109
sort(mins.begin(), mins.end());
109110
mins.push_back(mins[0] + 24 * 60);
110-
int res = 24 * 60;
111-
for (int i = 1; i < mins.size(); ++i)
112-
res = min(res, mins[i] - mins[i - 1]);
113-
return res;
111+
int ans = 1 << 30;
112+
for (int i = 1; i < mins.size(); ++i) {
113+
ans = min(ans, mins[i] - mins[i - 1]);
114+
}
115+
return ans;
114116
}
115117
};
116118
```
@@ -131,11 +133,11 @@ func findMinDifference(timePoints []string) int {
131133
}
132134
sort.Ints(mins)
133135
mins = append(mins, mins[0]+24*60)
134-
res := 24 * 60
135-
for i := 1; i < len(mins); i++ {
136-
res = min(res, mins[i]-mins[i-1])
136+
ans := 1 << 30
137+
for i, x := range mins[1:] {
138+
ans = min(ans, x-mins[i])
137139
}
138-
return res
140+
return ans
139141
}
140142
141143
func min(a, b int) int {
@@ -146,6 +148,27 @@ func min(a, b int) int {
146148
}
147149
```
148150

151+
### **TypeScript**
152+
153+
```ts
154+
function findMinDifference(timePoints: string[]): number {
155+
if (timePoints.length > 24 * 60) {
156+
return 0;
157+
}
158+
const mins: number[] = timePoints.map(timePoint => {
159+
const [hour, minute] = timePoint.split(':').map(num => parseInt(num));
160+
return hour * 60 + minute;
161+
});
162+
mins.sort((a, b) => a - b);
163+
mins.push(mins[0] + 24 * 60);
164+
let ans = 1 << 30;
165+
for (let i = 1; i < mins.length; ++i) {
166+
ans = Math.min(ans, mins[i] - mins[i - 1]);
167+
}
168+
return ans;
169+
}
170+
```
171+
149172
### **...**
150173

151174
```
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
class Solution {
2-
public:
3-
int findMinDifference(vector<string>& timePoints) {
4-
if (timePoints.size() > 24 * 60)
5-
return 0;
6-
vector<int> mins;
7-
for (auto t : timePoints)
8-
mins.push_back(stoi(t.substr(0, 2)) * 60 + stoi(t.substr(3)));
9-
sort(mins.begin(), mins.end());
10-
mins.push_back(mins[0] + 24 * 60);
11-
int res = 24 * 60;
12-
for (int i = 1; i < mins.size(); ++i)
13-
res = min(res, mins[i] - mins[i - 1]);
14-
return res;
15-
}
1+
class Solution {
2+
public:
3+
int findMinDifference(vector<string>& timePoints) {
4+
if (timePoints.size() > 24 * 60) {
5+
return 0;
6+
}
7+
vector<int> mins;
8+
for (auto& t : timePoints) {
9+
mins.push_back(stoi(t.substr(0, 2)) * 60 + stoi(t.substr(3)));
10+
}
11+
sort(mins.begin(), mins.end());
12+
mins.push_back(mins[0] + 24 * 60);
13+
int ans = 1 << 30;
14+
for (int i = 1; i < mins.size(); ++i) {
15+
ans = min(ans, mins[i] - mins[i - 1]);
16+
}
17+
return ans;
18+
}
1619
};

lcof2/剑指 Offer II 035. 最小时间差/Solution.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ func findMinDifference(timePoints []string) int {
1111
}
1212
sort.Ints(mins)
1313
mins = append(mins, mins[0]+24*60)
14-
res := 24 * 60
15-
for i := 1; i < len(mins); i++ {
16-
res = min(res, mins[i]-mins[i-1])
14+
ans := 1 << 30
15+
for i, x := range mins[1:] {
16+
ans = min(ans, x-mins[i])
1717
}
18-
return res
18+
return ans
1919
}
2020

2121
func min(a, b int) int {
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
class Solution {
2-
public int findMinDifference(List<String> timePoints) {
3-
if (timePoints.size() > 24 * 60) {
4-
return 0;
5-
}
6-
List<Integer> mins = new ArrayList<>();
7-
for (String t : timePoints) {
8-
String[] time = t.split(":");
9-
mins.add(Integer.parseInt(time[0]) * 60 + Integer.parseInt(time[1]));
10-
}
11-
Collections.sort(mins);
12-
mins.add(mins.get(0) + 24 * 60);
13-
int res = 24 * 60;
14-
for (int i = 1; i < mins.size(); ++i) {
15-
res = Math.min(res, mins.get(i) - mins.get(i - 1));
16-
}
17-
return res;
18-
}
1+
class Solution {
2+
public int findMinDifference(List<String> timePoints) {
3+
if (timePoints.size() > 24 * 60) {
4+
return 0;
5+
}
6+
List<Integer> mins = new ArrayList<>();
7+
for (String t : timePoints) {
8+
String[] time = t.split(":");
9+
mins.add(Integer.parseInt(time[0]) * 60 + Integer.parseInt(time[1]));
10+
}
11+
Collections.sort(mins);
12+
mins.add(mins.get(0) + 24 * 60);
13+
int ans = 1 << 30;
14+
for (int i = 1; i < mins.size(); ++i) {
15+
ans = Math.min(ans, mins.get(i) - mins.get(i - 1));
16+
}
17+
return ans;
18+
}
1919
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
class Solution:
2-
def findMinDifference(self, timePoints: List[str]) -> int:
3-
if len(timePoints) > 24 * 60:
4-
return 0
5-
mins = sorted(int(t[:2]) * 60 + int(t[3:]) for t in timePoints)
6-
mins.append(mins[0] + 24 * 60)
7-
res = mins[-1]
8-
for i in range(1, len(mins)):
9-
res = min(res, mins[i] - mins[i - 1])
10-
return res
1+
class Solution:
2+
def findMinDifference(self, timePoints: List[str]) -> int:
3+
if len(timePoints) > 24 * 60:
4+
return 0
5+
mins = sorted(int(t[:2]) * 60 + int(t[3:]) for t in timePoints)
6+
mins.append(mins[0] + 24 * 60)
7+
return min(b - a for a, b in pairwise(mins))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function findMinDifference(timePoints: string[]): number {
2+
if (timePoints.length > 24 * 60) {
3+
return 0;
4+
}
5+
const mins: number[] = timePoints.map(timePoint => {
6+
const [hour, minute] = timePoint.split(':').map(num => parseInt(num));
7+
return hour * 60 + minute;
8+
});
9+
mins.sort((a, b) => a - b);
10+
mins.push(mins[0] + 24 * 60);
11+
let ans = 1 << 30;
12+
for (let i = 1; i < mins.length; ++i) {
13+
ans = Math.min(ans, mins[i] - mins[i - 1]);
14+
}
15+
return ans;
16+
}

0 commit comments

Comments
 (0)