2058. 找出临界点之间的最小和最大距离
为保证权益,题目请参考 2058. 找出临界点之间的最小和最大距离(From LeetCode).
解决方案1
Python
python
# 2058. 找出临界点之间的最小和最大距离
# https://leetcode-cn.com/problems/find-the-minimum-and-maximum-number-of-nodes-between-critical-points/
################################################################################
from typing import List, Optional
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def nodesBetweenCriticalPoints(self, head: Optional[ListNode]) -> List[int]:
if head is None or head.next is None or head.next.next is None:
return [-1, -1]
nodeBef = head
nodeCur = head.next
nodeNex = head.next.next
minBef = -1
minCur = 1
minFir = -1
ans = [1000000000, -1000000000]
while nodeNex:
if (nodeBef.val > nodeCur.val < nodeNex.val) or (
nodeBef.val < nodeCur.val > nodeNex.val
):
if minFir == -1 or minBef == -1:
minBef = minFir = minCur
else:
ans[0] = min(ans[0], minCur - minBef)
ans[1] = max(ans[1], minCur - minFir)
minBef = minCur
minCur += 1
nodeBef, nodeCur, nodeNex = nodeCur, nodeNex, nodeNex.next
if ans[0] == 1000000000:
return [-1, -1]
else:
return ans
################################################################################
if __name__ == "__main__":
solution = Solution()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52