{"id":2024,"date":"2018-03-08T00:06:50","date_gmt":"2018-03-08T08:06:50","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2024"},"modified":"2018-03-22T22:33:59","modified_gmt":"2018-03-23T05:33:59","slug":"leetcode-495-teemo-attacking","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-495-teemo-attacking\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 495. Teemo Attacking"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u653b\u51fb\u7684\u65f6\u95f4\u5e8f\u5217\u4ee5\u53ca\u4e2d\u6bd2\u7684\u65f6\u957f\uff0c\u6c42\u603b\u5171\u7684\u4e2d\u6bd2\u65f6\u95f4\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/teemo-attacking\/description\/\">https:\/\/leetcode.com\/problems\/teemo-attacking\/description\/<\/a><\/p>\n<p>In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo&#8217;s attacking\u00a0<b>ascending<\/b>\u00a0time series towards Ashe and the poisoning time duration per Teemo&#8217;s attacking, you need to output the total time that Ashe is in poisoned condition.<\/p>\n<p>You may assume that Teemo attacks at the very beginning of a specific time point, and makes Ashe be in poisoned condition immediately.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"crayon:false\">Input: [1,4], 2\r\nOutput: 4\r\nExplanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned immediately. \r\nThis poisoned status will last 2 seconds until the end of time point 2. \r\nAnd at time point 4, Teemo attacks Ashe again, and causes Ashe to be in poisoned status for another 2 seconds. \r\nSo you finally need to output 4.\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"crayon:false \">Input: [1,2], 2\r\nOutput: 3\r\nExplanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned. \r\nThis poisoned status will last 2 seconds until the end of time point 2. \r\nHowever, at the beginning of time point 2, Teemo attacks Ashe again who is already in poisoned status. \r\nSince the poisoned status won't add up together, though the second poisoning attack will still work at time point 2, it will stop at the end of time point 3. \r\nSo you finally need to output 3.\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>You may assume the length of given time series array won&#8217;t exceed 10000.<\/li>\n<li>You may assume the numbers in the Teemo&#8217;s attacking time series and his poisoning time duration per attacking are non-negative integers, which won&#8217;t exceed 10,000,000.<\/li>\n<\/ol>\n<p><strong>Idea: Running Process<\/strong><\/p>\n<p>Compare the current attack time with the last one, if span is more than duration, add duration to total, otherwise add (curr &#8211; last).<\/p>\n<p><strong>C++<\/strong><\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 66 ms\r\nclass Solution {\r\npublic:\r\n  int findPoisonedDuration(vector&lt;int&gt;&amp; t, int duration) {\r\n    if (t.empty()) return 0;    \r\n    int total = 0;\r\n    for (int i = 1; i &lt; t.size(); ++ i)\r\n      total += (t[i] &gt; t[i - 1] + duration ? duration : t[i] - t[i - 1]);\r\n    return total + duration;\r\n  }\r\n};<\/pre>\n<p>Java<\/p>\n<pre class=\"lang:java decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 9 ms\r\nclass Solution {\r\n  public int findPoisonedDuration(int[] t, int duration) {\r\n    if (t.length == 0) return 0;    \r\n    int total = 0;\r\n    for (int i = 1; i &lt; t.length; ++ i)\r\n      total += (t[i] &gt; t[i - 1] + duration ? duration : t[i] - t[i - 1]);\r\n    return total + duration;\r\n  }\r\n}<\/pre>\n<p>Python3<\/p>\n<pre class=\"lang:python decode:true\">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 64 ms (beats 100%)\r\n\"\"\"\r\nclass Solution:\r\n  def findPoisonedDuration(self, timeSeries, duration):\r\n    if not timeSeries: return 0\r\n    l = timeSeries[0]\r\n    total = 0\r\n    for t in timeSeries:\r\n      total += duration if t - l &gt; duration else t - l\r\n      l = t\r\n    return total + duration<\/pre>\n<pre class=\"lang:python decode:true \">import numpy as np\r\nclass Solution:\r\n  def findPoisonedDuration(self, timeSeries, duration):    \r\n    if not timeSeries: return 0\r\n    d = np.diff(timeSeries)\r\n    d = np.clip(d, 0, duration)\r\n    return int(np.sum(d) + duration)<\/pre>\n<pre class=\"lang:python decode:true \">class Solution:\r\n  def findPoisonedDuration(self, t, duration):    \r\n    return sum([min(c - l, duration) for l, c in zip(t, t[1:] + [1e9])])<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u653b\u51fb\u7684\u65f6\u95f4\u5e8f\u5217\u4ee5\u53ca\u4e2d\u6bd2\u7684\u65f6\u957f\uff0c\u6c42\u603b\u5171\u7684\u4e2d\u6bd2\u65f6\u95f4\u3002 Problem: https:\/\/leetcode.com\/problems\/teemo-attacking\/description\/ In LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184,48],"tags":[177,75,179],"class_list":["post-2024","post","type-post","status-publish","format-standard","hentry","category-array","category-simulation","tag-medium","tag-online","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2024","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=2024"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2024\/revisions"}],"predecessor-version":[{"id":2314,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2024\/revisions\/2314"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}