Skip to content

Commit e3b911b

Browse files
committed
feat: add python and java solutions to lcci problem: No.16.10
1 parent d89f616 commit e3b911b

File tree

4 files changed

+111
-4
lines changed

4 files changed

+111
-4
lines changed

lcci/16.10.Living People/README.md

+37-2
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,50 @@ death = {1948, 1951, 2000}
3131
<!-- 这里可写当前语言的特殊实现逻辑 -->
3232

3333
```python
34-
34+
class Solution:
35+
def maxAliveYear(self, birth: List[int], death: List[int]) -> int:
36+
years = [0] * 101
37+
for i in range(len(birth)):
38+
start = birth[i] - 1900
39+
end = death[i] - 1900
40+
for j in range(start, end + 1):
41+
years[j] += 1
42+
max_v = years[0]
43+
res = 0
44+
for i in range(1, 101):
45+
if years[i] > max_v:
46+
max_v = years[i]
47+
res = i
48+
return 1900 + res
3549
```
3650

3751
### **Java**
3852

3953
<!-- 这里可写当前语言的特殊实现逻辑 -->
4054

4155
```java
42-
56+
class Solution {
57+
public int maxAliveYear(int[] birth, int[] death) {
58+
int[] years = new int[101];
59+
int n = birth.length;
60+
for (int i = 0; i < n; ++i) {
61+
int start = birth[i] - 1900;
62+
int end = death[i] - 1900;
63+
for (int j = start; j <= end; ++j) {
64+
++years[j];
65+
}
66+
}
67+
int max = years[0];
68+
int res = 0;
69+
for (int i = 1; i < 101; ++i) {
70+
if (years[i] > max) {
71+
max = years[i];
72+
res = i;
73+
}
74+
}
75+
return 1900 + res;
76+
}
77+
}
4378
```
4479

4580
### **...**

lcci/16.10.Living People/README_EN.md

+37-2
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,48 @@ death = {1948, 1951, 2000}
3636
### **Python3**
3737

3838
```python
39-
39+
class Solution:
40+
def maxAliveYear(self, birth: List[int], death: List[int]) -> int:
41+
years = [0] * 101
42+
for i in range(len(birth)):
43+
start = birth[i] - 1900
44+
end = death[i] - 1900
45+
for j in range(start, end + 1):
46+
years[j] += 1
47+
max_v = years[0]
48+
res = 0
49+
for i in range(1, 101):
50+
if years[i] > max_v:
51+
max_v = years[i]
52+
res = i
53+
return 1900 + res
4054
```
4155

4256
### **Java**
4357

4458
```java
45-
59+
class Solution {
60+
public int maxAliveYear(int[] birth, int[] death) {
61+
int[] years = new int[101];
62+
int n = birth.length;
63+
for (int i = 0; i < n; ++i) {
64+
int start = birth[i] - 1900;
65+
int end = death[i] - 1900;
66+
for (int j = start; j <= end; ++j) {
67+
++years[j];
68+
}
69+
}
70+
int max = years[0];
71+
int res = 0;
72+
for (int i = 1; i < 101; ++i) {
73+
if (years[i] > max) {
74+
max = years[i];
75+
res = i;
76+
}
77+
}
78+
return 1900 + res;
79+
}
80+
}
4681
```
4782

4883
### **...**
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int maxAliveYear(int[] birth, int[] death) {
3+
int[] years = new int[101];
4+
int n = birth.length;
5+
for (int i = 0; i < n; ++i) {
6+
int start = birth[i] - 1900;
7+
int end = death[i] - 1900;
8+
for (int j = start; j <= end; ++j) {
9+
++years[j];
10+
}
11+
}
12+
int max = years[0];
13+
int res = 0;
14+
for (int i = 1; i < 101; ++i) {
15+
if (years[i] > max) {
16+
max = years[i];
17+
res = i;
18+
}
19+
}
20+
return 1900 + res;
21+
}
22+
}

lcci/16.10.Living People/Solution.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def maxAliveYear(self, birth: List[int], death: List[int]) -> int:
3+
years = [0] * 101
4+
for i in range(len(birth)):
5+
start = birth[i] - 1900
6+
end = death[i] - 1900
7+
for j in range(start, end + 1):
8+
years[j] += 1
9+
max_v = years[0]
10+
res = 0
11+
for i in range(1, 101):
12+
if years[i] > max_v:
13+
max_v = years[i]
14+
res = i
15+
return 1900 + res

0 commit comments

Comments
 (0)