A critical point in a linked list is defined as either a local maxima or a local minima.
A node is a local maxima if the current node has a value strictly greater than the previous node and the next node.
A node is a local minima if the current node has a value strictly smaller than the previous node and the next node.
Note that a node can only be a local maxima/minima if there exists both a previous node and a next node.
Given a linked list head, return an array of length 2
containing [minDistance, maxDistance]
where minDistance is the minimum distance between any two distinct critical points and maxDistance is
the maximum distance between any two distinct critical points. If there are fewer than two critical points, return [-1, -1]
.
Example 1:
Input: head = [3,1]
Output: [-1,-1]
Explanation: There are no critical points in [3,1].
Example 2:
Input: head = [5,3,1,2,5,1,2]
Output: [1,3]
Explanation: There are three critical points:
- [5,3,1,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2.
- [5,3,1,2,5,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1.
- [5,3,1,2,5,1,2]: The sixth node is a local minima because 1 is less than 5 and 2.
The minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1.
The maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3.
Example 3:
Input: head = [1,3,2,2,3,2,2,2,7]
Output: [3,3]
Explanation: There are two critical points:
- [1,3,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2.
- [1,3,2,2,3,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2.
Both the minimum and maximum distances are between the second and the fifth node.
Thus, minDistance and maxDistance is 5 - 2 = 3.
Note that the last node is not considered a local maxima because it does not have a next node.
Constraints:
- The number of nodes in the list is in the range
[2, 105]
. 1 <= Node.val <= 10^5
Given a linked list head, we need to return an array of length 2 containing [minDistance, maxDistance]
where minDistance
is the minimum distance between any two distinct critical points and maxDistance
is the maximum distance between any two distinct critical points. If there are fewer than two critical points, return [-1, -1]
.
For calculating maxDistance
, we need to find the first and last critical point in the linked list and get the distance between them.
For calculating minDistance
, we need to find the minimum distance between any two adjacent critical points.
- Initialize a
result
vector with[-1,-1]
. - if there is only 3 node in the linked list, it is not possible to get the critical point, hence, return
result
. - Initialize
firstCriticalPoint
andlastFoundCriticalPoint
with-1
,cur
with1
. - While
head->next->next
is not null:-
left
:head->val
-
mid
:head->next->val
-
right
:head->next->next->val
-
If
mid
is a critical point-
If critical point is found first time
firstCriticalPoint
=cur
-
else,
result[1]
=cur - firstCriticalPoint
// calculate maximum distance -
If critical point is found first time
lastFoundCriticalPoint
=cur
-
else
- if
result[0]
=-1
result[0]
=cur - lastFoundCriticalPoint
- else
result[0]
=min(result[0], cur - lastFoundCriticalPoint)
lastFoundCriticalPoint
=cur
- if
-
-
Increament
cur
by 1. -
Move
head
to next node.
-
- Return
result
-
Time Complexity:
O(size_of_linked_list)
-
Space Complexity:
O(1)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
vector<int> nodesBetweenCriticalPoints(ListNode* head) {
vector<int> result(2,-1);
if(head == NULL || head->next == NULL || head->next->next == NULL)
{
return result;
}
int firstCriticalPoint = -1;
int lastFoundCriticalPoint = -1;
int cur = 1;
while(head->next->next != NULL) {
int left = head->val;
int mid = head->next->val;
int right = head->next->next->val;
if((left < mid && mid > right) || (left > mid && mid < right)) {
if(firstCriticalPoint == -1) {
firstCriticalPoint = cur;
} else {
result[1] = cur - firstCriticalPoint;
}
if(lastFoundCriticalPoint == -1) {
lastFoundCriticalPoint = cur;
} else {
if(result[0] == -1) {
result[0] = cur - lastFoundCriticalPoint;
} else {
result[0] = min(result[0], cur - lastFoundCriticalPoint);
}
lastFoundCriticalPoint = cur;
}
}
cur++;
head = head->next;
}
return result;
}
};
Tags: C++, cpp, leetcode, leetcode 2058, adhoc, linked list