{"id":9850,"date":"2022-09-26T10:18:29","date_gmt":"2022-09-26T17:18:29","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9850"},"modified":"2022-09-26T10:18:56","modified_gmt":"2022-09-26T17:18:56","slug":"leetcode-2420-find-all-good-indices","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-2420-find-all-good-indices\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2420. Find All Good Indices"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>&nbsp;of size&nbsp;<code>n<\/code>&nbsp;and a positive integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>We call an index&nbsp;<code>i<\/code>&nbsp;in the range&nbsp;<code>k &lt;= i &lt; n - k<\/code>&nbsp;<strong>good<\/strong>&nbsp;if the following conditions are satisfied:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The&nbsp;<code>k<\/code>&nbsp;elements that are just&nbsp;<strong>before<\/strong>&nbsp;the index&nbsp;<code>i<\/code>&nbsp;are in&nbsp;<strong>non-increasing<\/strong>&nbsp;order.<\/li><li>The&nbsp;<code>k<\/code>&nbsp;elements that are just&nbsp;<strong>after<\/strong>&nbsp;the index&nbsp;<code>i<\/code>&nbsp;are in&nbsp;<strong>non-decreasing<\/strong>&nbsp;order.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>an array of all good indices sorted in&nbsp;<strong>increasing<\/strong>&nbsp;order<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [2,1,1,1,3,4,1], k = 2\n<strong>Output:<\/strong> [2,3]\n<strong>Explanation:<\/strong> There are two good indices in the array:\n- Index 2. The subarray [2,1] is in non-increasing order, and the subarray [1,3] is in non-decreasing order.\n- Index 3. The subarray [1,1] is in non-increasing order, and the subarray [3,4] is in non-decreasing order.\nNote that the index 4 is not good because [4,1] is not non-decreasing.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [2,1,1,2], k = 2\n<strong>Output:<\/strong> []\n<strong>Explanation:<\/strong> There are no good indices in this array.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == nums.length<\/code><\/li><li><code>3 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10<sup>6<\/sup><\/code><\/li><li><code>1 &lt;= k &lt;= n \/ 2<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Prefix Sum<\/strong><\/h2>\n\n\n\n<p>Let before[i] = length of longest non-increasing subarray ends of nums[i].<br>Let after[i] = length of longest non-decreasing subarray ends of nums[i].<\/p>\n\n\n\n<p>An index is good if nums[i &#8211; 1] &gt;= k and nums[i + k] &gt;= k<\/p>\n\n\n\n<p>Time complexity: O(n + (n &#8211; 2*k))<br>Space complexity: O(n)<\/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> goodIndices(vector<int>& nums, int k) {\n    const int n = nums.size();\n    vector<int> before(n, 1);\n    vector<int> after(n, 1);\n    for (int i = 1; i < n; ++i) {\n      if (nums[i] <= nums[i - 1])\n        before[i] = before[i - 1] + 1;      \n      if (nums[i] >= nums[i - 1])\n        after[i] = after[i - 1] + 1;    \n    }\n    vector<int> ans;\n    for (int i = k; i + k < n; ++i) {\n      if (before[i - 1] >= k && after[i + k] >= k)\n        ans.push_back(i);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums&nbsp;of size&nbsp;n&nbsp;and a positive integer&nbsp;k. We call an index&nbsp;i&nbsp;in the range&nbsp;k &lt;= i &lt; n &#8211; k&nbsp;good&nbsp;if the following conditions are&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,177,764,200],"class_list":["post-9850","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-medium","tag-precompute","tag-prefix-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9850","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=9850"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9850\/revisions"}],"predecessor-version":[{"id":9852,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9850\/revisions\/9852"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}