-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy path1854-maximum-population-year.js
89 lines (83 loc) · 1.66 KB
/
1854-maximum-population-year.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* @param {number[][]} logs
* @return {number}
*/
const maximumPopulation = function(logs) {
const n = logs.length
const arr = Array(101).fill(0)
const base = 1950
for(let log of logs) {
const [start, end] = log
arr[start - base]++
arr[end - base]--
}
let res = 0, tmp = -Infinity
for(let i = 1; i < 101; i++) {
arr[i] += arr[i - 1]
}
for(let i = 0; i < 101; i++) {
if(arr[i] > tmp) {
res = i
tmp = arr[i]
}
}
return res + base
};
// another
/**
* @param {number[][]} logs
* @return {number}
*/
const maximumPopulation = function(logs) {
logs.sort((a, b) => {
if(a[0] === b[0]) return a[1] - b[1]
return a[0] - b[0]
})
const arr = Array(101).fill(0)
const bit = new FenwickTree(101)
for(let i = 0, len = logs.length; i < len; i++) {
const [start, end] = logs[i]
const idx = start - 1950
bit.update(idx + 1, 1)
}
for(let i = 0, len = logs.length; i < len; i++) {
const [start, end] = logs[i]
const idx = end - 1950
bit.update(idx + 1, -1)
}
let max = 0
for(let i = 1; i <= 101; i++) {
max = Math.max(max, bit.query(i))
}
let tmp
for(let i = 1; i <= 101; i++) {
if(bit.query(i) === max) {
tmp = i
break
}
}
return 1950 + tmp - 1
};
const lowBit = (x) => x & -x
class FenwickTree {
constructor(n) {
if (n < 1) return
this.sum = Array(n + 1).fill(0)
}
update(i, delta) {
if (i < 1) return
while (i < this.sum.length) {
this.sum[i] += delta
i += lowBit(i)
}
}
query(i) {
if (i < 1) return
let sum = 0
while (i > 0) {
sum += this.sum[i]
i -= lowBit(i)
}
return sum
}
}