{"id":9238,"date":"2021-12-25T18:41:45","date_gmt":"2021-12-26T02:41:45","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9238"},"modified":"2021-12-25T18:42:59","modified_gmt":"2021-12-26T02:42:59","slug":"leetcode-1921-eliminate-maximum-number-of-monsters","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1921-eliminate-maximum-number-of-monsters\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1921. Eliminate Maximum Number of Monsters"},"content":{"rendered":"\n<p>You are playing a video game where you are defending your city from a group of&nbsp;<code>n<\/code>&nbsp;monsters. You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>dist<\/code>&nbsp;of size&nbsp;<code>n<\/code>, where&nbsp;<code>dist[i]<\/code>&nbsp;is the&nbsp;<strong>initial distance<\/strong>&nbsp;in kilometers of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;monster from the city.<\/p>\n\n\n\n<p>The monsters walk toward the city at a&nbsp;<strong>constant<\/strong>&nbsp;speed. The speed of each monster is given to you in an integer array&nbsp;<code>speed<\/code>&nbsp;of size&nbsp;<code>n<\/code>, where&nbsp;<code>speed[i]<\/code>&nbsp;is the speed of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;monster in kilometers per minute.<\/p>\n\n\n\n<p>You have a weapon that, once fully charged, can eliminate a&nbsp;<strong>single<\/strong>&nbsp;monster. However, the weapon takes&nbsp;<strong>one minute<\/strong>&nbsp;to charge.The weapon is fully charged at the very start.<\/p>\n\n\n\n<p>You lose when any monster reaches your city. If a monster reaches the city at the exact moment the weapon is fully charged, it counts as a&nbsp;<strong>loss<\/strong>, and the game ends before you can use your weapon.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;number of monsters that you can eliminate before you lose, or&nbsp;<\/em><code>n<\/code><em>&nbsp;if you can eliminate all the monsters before they reach the city.<\/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> dist = [1,3,4], speed = [1,1,1]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong>\nIn the beginning, the distances of the monsters are [1,3,4]. You eliminate the first monster.\nAfter a minute, the distances of the monsters are [X,2,3]. You eliminate the second monster.\nAfter a minute, the distances of the monsters are [X,X,2]. You eliminate the thrid monster.\nAll 3 monsters can be eliminated.<\/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> dist = [1,1,2,3], speed = [1,1,1,1]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong>\nIn the beginning, the distances of the monsters are [1,1,2,3]. You eliminate the first monster.\nAfter a minute, the distances of the monsters are [X,0,1,2], so you lose.\nYou can only eliminate 1 monster.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> dist = [3,2,4], speed = [5,3,2]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong>\nIn the beginning, the distances of the monsters are [3,2,4]. You eliminate the first monster.\nAfter a minute, the distances of the monsters are [X,0,2], so you lose.\nYou can only eliminate 1 monster.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == dist.length == speed.length<\/code><\/li><li><code>1 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= dist[i], speed[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Sort by arrival time, and see how many we can eliminate.<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<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 eliminateMaximum(vector<int>& dist, vector<int>& speed) {\n    const int n = dist.size();\n    vector<int> t(n);\n    for (int i = 0; i < n; ++i)\n      t[i] = (dist[i] + speed[i] - 1) \/ speed[i];\n    sort(begin(t), end(t));    \n    for (int i = 0; i < n; ++i)\n      if (t[i] <= i) return i;\n    return n;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are playing a video game where you are defending your city from a group of&nbsp;n&nbsp;monsters. You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;dist&nbsp;of size&nbsp;n, where&nbsp;dist[i]&nbsp;is the&nbsp;initial distance&nbsp;in&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,177],"class_list":["post-9238","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9238","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=9238"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9238\/revisions"}],"predecessor-version":[{"id":9240,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9238\/revisions\/9240"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}