{"id":8664,"date":"2021-11-03T23:48:43","date_gmt":"2021-11-04T06:48:43","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8664"},"modified":"2021-11-03T23:48:44","modified_gmt":"2021-11-04T06:48:44","slug":"leetcode-2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/list\/leetcode-2058-find-the-minimum-and-maximum-number-of-nodes-between-critical-points\/","title":{"rendered":"LeetCode 2058. Find the Minimum and Maximum Number of Nodes Between Critical Points"},"content":{"rendered":"\n<p>A&nbsp;<strong>critical point<\/strong>&nbsp;in a linked list is defined as&nbsp;<strong>either<\/strong>&nbsp;a&nbsp;<strong>local maxima<\/strong>&nbsp;or a&nbsp;<strong>local minima<\/strong>.<\/p>\n\n\n\n<p>A node is a&nbsp;<strong>local maxima<\/strong>&nbsp;if the current node has a value&nbsp;<strong>strictly greater<\/strong>&nbsp;than the previous node and the next node.<\/p>\n\n\n\n<p>A node is a&nbsp;<strong>local minima<\/strong>&nbsp;if the current node has a value&nbsp;<strong>strictly smaller<\/strong>&nbsp;than the previous node and the next node.<\/p>\n\n\n\n<p>Note that a node can only be a local maxima\/minima if there exists&nbsp;<strong>both<\/strong>&nbsp;a previous node and a next node.<\/p>\n\n\n\n<p>Given a linked list&nbsp;<code>head<\/code>, return&nbsp;<em>an array of length 2 containing&nbsp;<\/em><code>[minDistance, maxDistance]<\/code><em>&nbsp;where&nbsp;<\/em><code>minDistance<\/code><em>&nbsp;is the&nbsp;<strong>minimum distance<\/strong>&nbsp;between&nbsp;<strong>any&nbsp;two distinct<\/strong>&nbsp;critical points and&nbsp;<\/em><code>maxDistance<\/code><em>&nbsp;is the&nbsp;<strong>maximum distance<\/strong>&nbsp;between&nbsp;<strong>any&nbsp;two distinct<\/strong>&nbsp;critical points. If there are&nbsp;<strong>fewer<\/strong>&nbsp;than two critical points, return&nbsp;<\/em><code>[-1, -1]<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/13\/a1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [3,1]\n<strong>Output:<\/strong> [-1,-1]\n<strong>Explanation:<\/strong> There are no critical points in [3,1].\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/13\/a2.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [5,3,1,2,5,1,2]\n<strong>Output:<\/strong> [1,3]\n<strong>Explanation:<\/strong> There are three critical points:\n- [5,3,<strong><u>1<\/u><\/strong>,2,5,1,2]: The third node is a local minima because 1 is less than 3 and 2.\n- [5,3,1,2,<strong>5<\/strong>,1,2]: The fifth node is a local maxima because 5 is greater than 2 and 1.\n- [5,3,1,2,5,<strong>1<\/strong>,2]: The sixth node is a local minima because 1 is less than 5 and 2.\nThe minimum distance is between the fifth and the sixth node. minDistance = 6 - 5 = 1.\nThe maximum distance is between the third and the sixth node. maxDistance = 6 - 3 = 3.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/14\/a5.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [1,3,2,2,3,2,2,2,7]\n<strong>Output:<\/strong> [3,3]\n<strong>Explanation:<\/strong> There are two critical points:\n- [1,<strong>3<\/strong>,2,2,3,2,2,2,7]: The second node is a local maxima because 3 is greater than 1 and 2.\n- [1,3,2,2,<strong>3<\/strong>,2,2,2,7]: The fifth node is a local maxima because 3 is greater than 2 and 2.\nBoth the minimum and maximum distances are between the second and the fifth node.\nThus, minDistance and maxDistance is 5 - 2 = 3.\nNote that the last node is not considered a local maxima because it does not have a next node.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/13\/a4.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> head = [2,3,3,2]\n<strong>Output:<\/strong> [-1,-1]\n<strong>Explanation:<\/strong> There are no critical points in [2,3,3,2].\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The number of nodes in the list is in the range&nbsp;<code>[2, 10<sup>5<\/sup>]<\/code>.<\/li><li><code>1 &lt;= Node.val &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: One Pass<\/strong><\/h2>\n\n\n\n<p>Track the first and last critical points.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> nodesBetweenCriticalPoints(ListNode* head) {    \n    ListNode* p = nullptr;\n    int min_dist = INT_MAX;\n    int max_dist = INT_MIN;    \n    for (int i = 0, l = -1, r = -1; head; ++i, p = head, head = head->next) {\n      if (p && head->next) {\n        if ((head->val > p->val && head->val > head->next->val) \n            || (head->val < p->val && head->val < head->next->val)) {\n          if (r != -1) {\n            min_dist = min(min_dist, i - r);\n            max_dist = max(max_dist, i - l);\n          } else {\n            l = i;\n          }          \n          r = i;          \n        }\n      }\n    }\n    return { min_dist == INT_MAX ? -1 : min_dist, \n             max_dist == INT_MIN ? -1 : max_dist };\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A&nbsp;critical point&nbsp;in a linked list is defined as&nbsp;either&nbsp;a&nbsp;local maxima&nbsp;or a&nbsp;local minima. A node is a&nbsp;local maxima&nbsp;if the current node has a value&nbsp;strictly greater&nbsp;than the previous&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50],"tags":[],"class_list":["post-8664","post","type-post","status-publish","format-standard","hentry","category-list","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8664","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8664"}],"version-history":[{"count":1,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8664\/revisions"}],"predecessor-version":[{"id":8665,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8664\/revisions\/8665"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}