-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy path2158-amount-of-new-area-painted-each-day.js
More file actions
65 lines (56 loc) · 1.91 KB
/
2158-amount-of-new-area-painted-each-day.js
File metadata and controls
65 lines (56 loc) · 1.91 KB
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
/**
* 2158. Amount of New Area Painted Each Day
* https://leetcode.com/problems/amount-of-new-area-painted-each-day/
* Difficulty: Hard
*
* There is a long and thin painting that can be represented by a number line. You are given
* a 0-indexed 2D integer array paint of length n, where paint[i] = [starti, endi]. This means
* that on the ith day you need to paint the area between starti and endi.
*
* Painting the same area multiple times will create an uneven painting so you only want to
* paint each area of the painting at most once.
*
* Return an integer array worklog of length n, where worklog[i] is the amount of new area that
* you painted on the ith day.
*/
/**
* @param {number[][]} paint
* @return {number[]}
*/
var amountPainted = function(paint) {
const intervals = new Map();
const result = [];
for (const [start, end] of paint) {
let left = start;
let right = end;
let paintAmount = right - left;
const keys = Array.from(intervals.keys()).sort((a, b) => a - b);
const toRemove = [];
let merged = false;
for (const key of keys) {
const value = intervals.get(key);
if (key > right || value < left) continue;
if (!merged && key <= left && value >= left) {
left = Math.min(left, key);
right = Math.max(right, value);
paintAmount = Math.max(0, end - Math.max(start, value));
toRemove.push(key);
merged = true;
} else if (key >= left && key < right) {
paintAmount -= Math.min(right, value) - key;
right = Math.max(right, value);
toRemove.push(key);
} else if (value > left && value <= right) {
paintAmount -= value - Math.max(left, key);
left = Math.min(left, key);
toRemove.push(key);
}
}
for (const key of toRemove) {
intervals.delete(key);
}
intervals.set(left, right);
result.push(Math.max(0, paintAmount));
}
return result;
};