{"id":5292,"date":"2019-07-14T12:33:44","date_gmt":"2019-07-14T19:33:44","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5292"},"modified":"2019-07-15T20:17:52","modified_gmt":"2019-07-16T03:17:52","slug":"leetcode-1124-longest-well-performing-interval","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/uncategorized\/leetcode-1124-longest-well-performing-interval\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1124. Longest Well-Performing Interval"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1124. Longest Well-Performing Interval - \u5237\u9898\u627e\u5de5\u4f5c EP256\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/H76XMJmBfP0?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>We are given&nbsp;<code>hours<\/code>, a list of the number of hours&nbsp;worked per day for a given employee.<\/p>\n\n\n\n<p>A day is considered to be a&nbsp;<em>tiring day<\/em>&nbsp;if and only if the number of hours worked is (strictly) greater than&nbsp;<code>8<\/code>.<\/p>\n\n\n\n<p>A&nbsp;<em>well-performing interval<\/em>&nbsp;is an interval of days for which the number of tiring days is strictly larger than the number of non-tiring days.<\/p>\n\n\n\n<p>Return the length of the longest well-performing interval.<\/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> hours = [9,9,6,0,6,6,9]\n<strong>Output:<\/strong> 3\n<strong>Explanation: <\/strong>The longest well-performing interval is [9,9,6].<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= hours.length &lt;= 10000<\/code><\/li><li><code>0 &lt;= hours[i] &lt;= 16<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Target sum == 1<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/07\/1124-ep256.png\" alt=\"\" class=\"wp-image-5297\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/07\/1124-ep256.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/07\/1124-ep256-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/07\/1124-ep256-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>This problem can be reduced to find the longest subarray with sum of 1, or the longest subarray starting from left-most that has a sum greater than 0.<\/p>\n\n\n\n<p> e.g. [6, 6, (6, 9, 9), 6, 6] =&gt; sum = 1 <br>e.g. [9, 9, 9] =&gt; sum = 3<br><\/p>\n\n\n\n<p>Time complexity: O(n)<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  int longestWPI(vector<int>& hours) {\n    const int n = hours.size();\n    for (int& v : hours) v = v > 8 ? 1 : -1;\n    int s = 0;\n    unordered_map<int, int> idx;    \n    int ans = 0;\n    for (int i = 0; i < hours.size(); ++i) {\n      s += hours[i];\n      if (s > 0) \n        ans = i + 1;\n      if (!idx.count(s)) \n        idx[s] = i;\n      if (idx.count(s - 1))\n        ans = max(ans, i - idx[s - 1]);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>We are given&nbsp;hours, a list of the number of hours&nbsp;worked per day for a given employee. A day is considered to be a&nbsp;tiring day&nbsp;if and&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5292","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5292","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=5292"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5292\/revisions"}],"predecessor-version":[{"id":5306,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5292\/revisions\/5306"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}