1854:人口最多的年份
This commit is contained in:
parent
9538b7f4d2
commit
8cfc0007b8
64
src/main/java/leetcode/editor/cn/MaximumPopulationYear.java
Normal file
64
src/main/java/leetcode/editor/cn/MaximumPopulationYear.java
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
//给你一个二维整数数组 logs ,其中每个 logs[i] = [birthi, deathi] 表示第 i 个人的出生和死亡年份。
|
||||||
|
//
|
||||||
|
// 年份 x 的 人口 定义为这一年期间活着的人的数目。第 i 个人被计入年份 x 的人口需要满足:x 在闭区间 [birthi, deathi - 1] 内
|
||||||
|
//。注意,人不应当计入他们死亡当年的人口中。
|
||||||
|
//
|
||||||
|
// 返回 人口最多 且 最早 的年份。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
// 输入:logs = [[1993,1999],[2000,2010]]
|
||||||
|
//输出:1993
|
||||||
|
//解释:人口最多为 1 ,而 1993 是人口为 1 的最早年份。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
// 输入:logs = [[1950,1961],[1960,1971],[1970,1981]]
|
||||||
|
//输出:1960
|
||||||
|
//解释:
|
||||||
|
//人口最多为 2 ,分别出现在 1960 和 1970 。
|
||||||
|
//其中最早年份是 1960 。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= logs.length <= 100
|
||||||
|
// 1950 <= birthi < deathi <= 2050
|
||||||
|
//
|
||||||
|
// Related Topics 数组
|
||||||
|
// 👍 14 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
//1854:人口最多的年份
|
||||||
|
public class MaximumPopulationYear{
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new MaximumPopulationYear().new Solution();
|
||||||
|
}
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int maximumPopulation(int[][] logs) {
|
||||||
|
int[] year = new int[100];
|
||||||
|
for (int[] log : logs) {
|
||||||
|
for (int j = log[0]; j < log[1]; j++) {
|
||||||
|
year[j - 1950]++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int index = 0;
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
if (year[i] > year[index]) {
|
||||||
|
index = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return index + 1950;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
32
src/main/java/leetcode/editor/cn/MaximumPopulationYear.md
Normal file
32
src/main/java/leetcode/editor/cn/MaximumPopulationYear.md
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<p>给你一个二维整数数组 <code>logs</code> ,其中每个 <code>logs[i] = [birth<sub>i</sub>, death<sub>i</sub>]</code> 表示第 <code>i</code> 个人的出生和死亡年份。</p>
|
||||||
|
|
||||||
|
<p>年份 <code>x</code> 的 <strong>人口</strong> 定义为这一年期间活着的人的数目。第 <code>i</code> 个人被计入年份 <code>x</code> 的人口需要满足:<code>x</code> 在闭区间 <code>[birth<sub>i</sub>, death<sub>i</sub> - 1]</code> 内。注意,人不应当计入他们死亡当年的人口中。</p>
|
||||||
|
|
||||||
|
<p>返回 <strong>人口最多</strong> 且 <strong>最早</strong> 的年份。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>logs = [[1993,1999],[2000,2010]]
|
||||||
|
<strong>输出:</strong>1993
|
||||||
|
<strong>解释:</strong>人口最多为 1 ,而 1993 是人口为 1 的最早年份。
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre><strong>输入:</strong>logs = [[1950,1961],[1960,1971],[1970,1981]]
|
||||||
|
<strong>输出:</strong>1960
|
||||||
|
<strong>解释:</strong>
|
||||||
|
人口最多为 2 ,分别出现在 1960 和 1970 。
|
||||||
|
其中最早年份是 1960 。</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= logs.length <= 100</code></li>
|
||||||
|
<li><code>1950 <= birth<sub>i</sub> < death<sub>i</sub> <= 2050</code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>数组</li></div></div>\n<div><li>👍 14</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user