{"id":8444,"date":"2021-05-08T23:24:28","date_gmt":"2021-05-09T06:24:28","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8444"},"modified":"2021-05-08T23:43:18","modified_gmt":"2021-05-09T06:43:18","slug":"leetcode-1854-maximum-population-year","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1854-maximum-population-year\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1854. Maximum Population Year"},"content":{"rendered":"\n<p>You are given a 2D integer array&nbsp;<code>logs<\/code>&nbsp;where each&nbsp;<code>logs[i] = [birth<sub>i<\/sub>, death<sub>i<\/sub>]<\/code>&nbsp;indicates the birth and death years of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;person.<\/p>\n\n\n\n<p>The&nbsp;<strong>population<\/strong>&nbsp;of some year&nbsp;<code>x<\/code>&nbsp;is the number of people alive during that year. The&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;person is counted in year&nbsp;<code>x<\/code>&#8216;s population if&nbsp;<code>x<\/code>&nbsp;is in the&nbsp;<strong>inclusive<\/strong>&nbsp;range&nbsp;<code>[birth<sub>i<\/sub>, death<sub>i<\/sub>&nbsp;- 1]<\/code>. Note that the person is&nbsp;<strong>not<\/strong>&nbsp;counted in the year that they die.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>earliest<\/strong>&nbsp;year with the&nbsp;<strong>maximum population<\/strong><\/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> logs = [[1993,1999],[2000,2010]]\n<strong>Output:<\/strong> 1993\n<strong>Explanation:<\/strong> The maximum population is 1, and 1993 is the earliest year with this population.\n<\/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> logs = [[1950,1961],[1960,1971],[1970,1981]]\n<strong>Output:<\/strong> 1960\n<strong>Explanation:<\/strong> \nThe maximum population is 2, and it had happened in years 1960 and 1970.\nThe earlier year between them is 1960.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= logs.length &lt;= 100<\/code><\/li><li><code>1950 &lt;= birth<sub>i<\/sub>&nbsp;&lt; death<sub>i<\/sub>&nbsp;&lt;= 2050<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n*y)<br>Space complexity: O(y)<\/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 maximumPopulation(vector<vector<int>>& logs) {\n    vector<int> pop(2051);\n    for (const auto& log : logs) {\n      for (int y = log[0]; y < log[1]; ++y)\n        ++pop[y];\n    }\n    int ans = -1;\n    int max_pop = 0;\n    for (int y = 1950; y <= 2050; ++y) {\n      if (pop[y] > max_pop) {\n        max_pop = pop[y];\n        ans = y;\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a 2D integer array&nbsp;logs&nbsp;where each&nbsp;logs[i] = [birthi, deathi]&nbsp;indicates the birth and death years of the&nbsp;ith&nbsp;person. The&nbsp;population&nbsp;of some year&nbsp;x&nbsp;is the number of people&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[222,179],"class_list":["post-8444","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-easy","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8444","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=8444"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8444\/revisions"}],"predecessor-version":[{"id":8446,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8444\/revisions\/8446"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}