> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/array_problem/2158.-amount-of-new-area-painted-each-day.md).

# 2158. Amount of New Area Painted Each Day

#### O(n\*m) Time Limit Exceeded&#x20;

````java
```java
class Solution {
    public int[] amountPainted(int[][] paint) {
        int[] result = new int[paint.length];
        boolean[] area = new boolean[100001];
        for (int i = 0; i < paint.length; i++) {
            int start = paint[i][0];
            int end = paint[i][1];
            int count = 0;
            for (int j = start; j < end; j++) {
                if (area[j]) {
                    count++;
                }
                area[j] = true;
            }
            result[i] = end - start - count;
        }
        return result;
    }
}
```
````

## line sweep , use TreeSet

```
T: O(m*n + nlogn), n is paint length, m is maxDay
```

```
S O(n)
```

````java
```java
class Solution {
    public int[] amountPainted(int[][] paint) {
        int maxDay = 0;
        // calculate the entire range
        for (int[] p : paint) { 
            maxDay = Math.max(p[0], maxDay);
            maxDay = Math.max(p[1], maxDay);
        }
        List<List<int[]>> lineData = new ArrayList<>();
        for (int i = 0; i <= maxDay; i++) {
            lineData.add(new ArrayList<>());
        }
        // record line data (start, end), record in a day line
        for (int i = 0; i < paint.length; i++) {
            int start = paint[i][0];
            int end = paint[i][1];
            lineData.get(start).add(new int[] {i, 1}); // record paint idx(paintWorkday), sign
            lineData.get(end).add(new int[]{i, -1});
        }

        int[] result = new int[paint.length];
        TreeSet<Integer> treeSet = new TreeSet<>();
        for (int i = 0; i <= maxDay; i++) { // start from 0 workday
            for (int[] data : lineData.get(i)) {
                int paintWorkday = data[0];
                int sign = data[1];
                if (sign == 1) {
                    treeSet.add(paintWorkday);
                } else {
                    treeSet.remove(paintWorkday);
                }
            }
            if (!treeSet.isEmpty()) { // be careful this!
                result[treeSet.first()]++;
            }
        }
        return result;
    }
}

/*
Input: paint = [[1,4],[5,8],[4,7]]
Output: [3,3,1]

1 2 3 4 5 6 7 8
- - - - - - - - -
0 0 0 
+.  -  
        1 1 1
        +.  - 
      2 2 2
      +.  - 
        when 5 -> in treeSet(1, 2), so we always use first one -> use 1st day as the workday
        treeSet, so small workday (paint idx), is always in the head


in each day, we get the day and see is it a start sign, if not remove it
if not remove it means still this idx's workday


T: O(m*n + nlogn), n is paint length, m is maxDay
S O(n)
  
*/
```
````

## Jump line&#x20;

```
T: O(n*m) , n paint length, m is max value range (ex: [0, 10000], [0, 10000])
```

```
S: O(n*m)
```

````java
```java
class Solution {
    public int[] amountPainted(int[][] paint) {
        int[] result = new int[paint.length];
        Map<Integer, Integer> map = new HashMap<>();

        int idx = 0;
        for (int[] p : paint) {
            int work = 0;
            int start = p[0];
            int end = p[1];
            while (start < end) {
                if (map.containsKey(start)) {
                    int preEnd = map.get(start);
                    map.put(start, Math.max(preEnd, end));
                    start = preEnd;
                } else {
                    map.put(start, end);
                    start++;
                    work++;
                }
            }
            result[idx++] = work;
        }
        return result;
    }
}

/*
T: O(n*m) , n paint length, m is max value range (ex: [0, 10000], [0, 10000])
S: O(n*m)

[[1,4],[4,7],[5,8]]


paint[0], s = 1 e = 4
map(1,4)
s = 2
w = 1

map(2,4)
s = 3
w = 2

map(3,4)
s = 4
w = 3
end while
result[0] = 3
---
paint[1], s = 4 e = 7
map(4,7)
s = 5
w = 1

map(5,7)
s = 6
w = 2

map(6,7)
s = 7
w = 3
end while
result[1] = 3

---------
paint[2], s = 5 e = 8
map(5) -> exists
                    int preEnd = map.get(5); => 7
                    map.put(start, Math.max(preEnd, end)); map(5, max(7, 8))
                    start = preEnd; start = 7 (jump!!)

map(7,8)
s = 8
w = 1
end while
result[2] = 1

so result[3,3,1]
*/
```ja
````


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/array_problem/2158.-amount-of-new-area-painted-each-day.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
