Skip to content

Commit e1ba11f

Browse files
committed
Mar-13
1 parent edc012b commit e1ba11f

10 files changed

+724
-10
lines changed

README.md

Lines changed: 22 additions & 10 deletions
Large diffs are not rendered by default.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Tag: Array, Counting, Prefix Sum, Line Sweep
2+
// Time: O(NlogN)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// You are given a 2D integer array logs where each logs[i] = [birthi, deathi] indicates the birth and death years of the ith person.
8+
// The population of some year x is the number of people alive during that year. The ith person is counted in year x's population if x is in the inclusive range [birthi, deathi - 1]. Note that the person is not counted in the year that they die.
9+
// Return the earliest year with the maximum population.
10+
//  
11+
// Example 1:
12+
//
13+
// Input: logs = [[1993,1999],[2000,2010]]
14+
// Output: 1993
15+
// Explanation: The maximum population is 1, and 1993 is the earliest year with this population.
16+
//
17+
// Example 2:
18+
//
19+
// Input: logs = [[1950,1961],[1960,1971],[1970,1981]]
20+
// Output: 1960
21+
// Explanation:
22+
// The maximum population is 2, and it had happened in years 1960 and 1970.
23+
// The earlier year between them is 1960.
24+
//  
25+
// Constraints:
26+
//
27+
// 1 <= logs.length <= 100
28+
// 1950 <= birthi < deathi <= 2050
29+
//
30+
//
31+
32+
class Solution {
33+
public:
34+
int maximumPopulation(vector<vector<int>>& logs) {
35+
map<int, int> lines;
36+
for (auto &log: logs) {
37+
lines[log[0]] += 1;
38+
lines[log[1]] -= 1;
39+
}
40+
41+
int count = 0;
42+
int max_count = 0;
43+
int res = 0;
44+
for (auto& i: lines) {
45+
count += i.second;
46+
if (count > max_count) {
47+
max_count = count;
48+
res = i.first;
49+
}
50+
}
51+
return res;
52+
}
53+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Tag: Array, Counting, Prefix Sum, Line Sweep
2+
# Time: O(NlogN)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
7+
# You are given a 2D integer array logs where each logs[i] = [birthi, deathi] indicates the birth and death years of the ith person.
8+
# The population of some year x is the number of people alive during that year. The ith person is counted in year x's population if x is in the inclusive range [birthi, deathi - 1]. Note that the person is not counted in the year that they die.
9+
# Return the earliest year with the maximum population.
10+
#  
11+
# Example 1:
12+
#
13+
# Input: logs = [[1993,1999],[2000,2010]]
14+
# Output: 1993
15+
# Explanation: The maximum population is 1, and 1993 is the earliest year with this population.
16+
#
17+
# Example 2:
18+
#
19+
# Input: logs = [[1950,1961],[1960,1971],[1970,1981]]
20+
# Output: 1960
21+
# Explanation:
22+
# The maximum population is 2, and it had happened in years 1960 and 1970.
23+
# The earlier year between them is 1960.
24+
#  
25+
# Constraints:
26+
#
27+
# 1 <= logs.length <= 100
28+
# 1950 <= birthi < deathi <= 2050
29+
#
30+
#
31+
32+
from collections import defaultdict
33+
class Solution:
34+
def maximumPopulation(self, logs: List[List[int]]) -> int:
35+
n = len(logs)
36+
line = defaultdict(int)
37+
38+
for s, e in logs:
39+
line[s] += 1
40+
line[e] -= 1
41+
42+
count = 0
43+
max_count = 0
44+
res = 0
45+
for year in sorted(line.keys()):
46+
count += line[year]
47+
if count > max_count:
48+
max_count = count
49+
res = year
50+
51+
return res
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Tag: Array, Prefix Sum, Line Sweep
2+
// Time: O(N + M)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// You are given an integer array nums of length n and a 2D array queries, where queries[i] = [li, ri].
8+
// For each queries[i]:
9+
//
10+
// Select a subset of indices within the range [li, ri] in nums.
11+
// Decrement the values at the selected indices by 1.
12+
//
13+
// A Zero Array is an array where all elements are equal to 0.
14+
// Return true if it is possible to transform nums into a Zero Array after processing all the queries sequentially, otherwise return false.
15+
//  
16+
// Example 1:
17+
//
18+
// Input: nums = [1,0,1], queries = [[0,2]]
19+
// Output: true
20+
// Explanation:
21+
//
22+
// For i = 0:
23+
//
24+
// Select the subset of indices as [0, 2] and decrement the values at these indices by 1.
25+
// The array will become [0, 0, 0], which is a Zero Array.
26+
//
27+
//
28+
//
29+
//
30+
// Example 2:
31+
//
32+
// Input: nums = [4,3,2,1], queries = [[1,3],[0,2]]
33+
// Output: false
34+
// Explanation:
35+
//
36+
// For i = 0:
37+
//
38+
// Select the subset of indices as [1, 2, 3] and decrement the values at these indices by 1.
39+
// The array will become [4, 2, 1, 0].
40+
//
41+
//
42+
// For i = 1:
43+
//
44+
// Select the subset of indices as [0, 1, 2] and decrement the values at these indices by 1.
45+
// The array will become [3, 1, 0, 0], which is not a Zero Array.
46+
//
47+
//
48+
//
49+
//
50+
//  
51+
// Constraints:
52+
//
53+
// 1 <= nums.length <= 105
54+
// 0 <= nums[i] <= 105
55+
// 1 <= queries.length <= 105
56+
// queries[i].length == 2
57+
// 0 <= li <= ri < nums.length
58+
//
59+
//
60+
61+
class Solution {
62+
public:
63+
bool isZeroArray(vector<int>& nums, vector<vector<int>>& queries) {
64+
int n = nums.size();
65+
vector<int> line(n + 1, 0);
66+
for (auto &q: queries) {
67+
line[q[0]] += 1;
68+
line[q[1] + 1] -= 1;
69+
}
70+
71+
int prefix = 0;
72+
for (int i = 0; i < n; i++) {
73+
prefix += line[i];
74+
if (nums[i] > prefix) {
75+
return false;
76+
}
77+
}
78+
return true;
79+
}
80+
};
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Tag: Array, Prefix Sum, Line Sweep
2+
# Time: O(N + M)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
7+
# You are given an integer array nums of length n and a 2D array queries, where queries[i] = [li, ri].
8+
# For each queries[i]:
9+
#
10+
# Select a subset of indices within the range [li, ri] in nums.
11+
# Decrement the values at the selected indices by 1.
12+
#
13+
# A Zero Array is an array where all elements are equal to 0.
14+
# Return true if it is possible to transform nums into a Zero Array after processing all the queries sequentially, otherwise return false.
15+
#  
16+
# Example 1:
17+
#
18+
# Input: nums = [1,0,1], queries = [[0,2]]
19+
# Output: true
20+
# Explanation:
21+
#
22+
# For i = 0:
23+
#
24+
# Select the subset of indices as [0, 2] and decrement the values at these indices by 1.
25+
# The array will become [0, 0, 0], which is a Zero Array.
26+
#
27+
#
28+
#
29+
#
30+
# Example 2:
31+
#
32+
# Input: nums = [4,3,2,1], queries = [[1,3],[0,2]]
33+
# Output: false
34+
# Explanation:
35+
#
36+
# For i = 0:
37+
#
38+
# Select the subset of indices as [1, 2, 3] and decrement the values at these indices by 1.
39+
# The array will become [4, 2, 1, 0].
40+
#
41+
#
42+
# For i = 1:
43+
#
44+
# Select the subset of indices as [0, 1, 2] and decrement the values at these indices by 1.
45+
# The array will become [3, 1, 0, 0], which is not a Zero Array.
46+
#
47+
#
48+
#
49+
#
50+
#  
51+
# Constraints:
52+
#
53+
# 1 <= nums.length <= 105
54+
# 0 <= nums[i] <= 105
55+
# 1 <= queries.length <= 105
56+
# queries[i].length == 2
57+
# 0 <= li <= ri < nums.length
58+
#
59+
#
60+
61+
class Solution:
62+
def isZeroArray(self, nums: List[int], queries: List[List[int]]) -> bool:
63+
n = len(nums)
64+
line = [0 for i in range(n + 1)]
65+
for l, r in queries:
66+
line[l] += 1
67+
line[r + 1] -=1
68+
69+
prefix = 0
70+
for i in range(n):
71+
prefix += line[i]
72+
if nums[i] > prefix:
73+
return False
74+
75+
return True

0 commit comments

Comments
 (0)