Skip to content

Commit 77369a4

Browse files
committed
feat: add solutions to lc problem: No.1943
No.1943.Describe the Painting
1 parent fdc1927 commit 77369a4

File tree

5 files changed

+211
-2
lines changed

5 files changed

+211
-2
lines changed

solution/1900-1999/1943.Describe the Painting/README.md

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,94 @@
7979

8080
<!-- 这里可写通用的实现逻辑 -->
8181

82+
差分数组。
83+
8284
<!-- tabs:start -->
8385

8486
### **Python3**
8587

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

8890
```python
89-
91+
class Solution:
92+
def splitPainting(self, segments: List[List[int]]) -> List[List[int]]:
93+
d = defaultdict(int)
94+
for l, r, c in segments:
95+
d[l] += c
96+
d[r] -= c
97+
s = sorted([[k, v] for k, v in d.items()])
98+
n = len(s)
99+
for i in range(1, n):
100+
s[i][1] += s[i - 1][1]
101+
ans = []
102+
for i in range(n - 1):
103+
if s[i][1]:
104+
ans.append([s[i][0], s[i + 1][0], s[i][1]])
105+
return ans
90106
```
91107

92108
### **Java**
93109

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

96112
```java
113+
class Solution {
114+
public List<List<Long>> splitPainting(int[][] segments) {
115+
TreeMap<Integer, Long> d = new TreeMap<>();
116+
for (int[] e : segments) {
117+
int l = e[0], r = e[1], c = e[2];
118+
d.put(l, d.getOrDefault(l, 0L) + c);
119+
d.put(r, d.getOrDefault(r, 0L) - c);
120+
}
121+
List<List<Long>> ans = new ArrayList<>();
122+
long i = 0, j = 0;
123+
long cur = 0;
124+
for (Map.Entry<Integer, Long> e : d.entrySet()) {
125+
if (Objects.equals(e.getKey(), d.firstKey())) {
126+
i = e.getKey();
127+
} else {
128+
j = e.getKey();
129+
if (cur > 0) {
130+
ans.add(Arrays.asList(i, j, cur));
131+
}
132+
i = j;
133+
}
134+
cur += e.getValue();
135+
}
136+
return ans;
137+
}
138+
}
139+
```
97140

141+
### **C++**
142+
143+
```cpp
144+
class Solution {
145+
public:
146+
vector<vector<long long>> splitPainting(vector<vector<int>>& segments) {
147+
map<int, long long> d;
148+
for (auto& e : segments)
149+
{
150+
int l = e[0], r = e[1], c = e[2];
151+
d[l] += c;
152+
d[r] -= c;
153+
}
154+
vector<vector<long long>> ans;
155+
long long i, j, cur = 0;
156+
for (auto& it : d)
157+
{
158+
if (it == *d.begin()) i = it.first;
159+
else
160+
{
161+
j = it.first;
162+
if (cur > 0) ans.push_back({i, j, cur});
163+
i = j;
164+
}
165+
cur += it.second;
166+
}
167+
return ans;
168+
}
169+
};
98170
```
99171
100172
### **...**

solution/1900-1999/1943.Describe the Painting/README_EN.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,83 @@ Note that returning a single segment [1,7) is incorrect because the mixed color
8181
### **Python3**
8282

8383
```python
84-
84+
class Solution:
85+
def splitPainting(self, segments: List[List[int]]) -> List[List[int]]:
86+
d = defaultdict(int)
87+
for l, r, c in segments:
88+
d[l] += c
89+
d[r] -= c
90+
s = sorted([[k, v] for k, v in d.items()])
91+
n = len(s)
92+
for i in range(1, n):
93+
s[i][1] += s[i - 1][1]
94+
ans = []
95+
for i in range(n - 1):
96+
if s[i][1]:
97+
ans.append([s[i][0], s[i + 1][0], s[i][1]])
98+
return ans
8599
```
86100

87101
### **Java**
88102

89103
```java
104+
class Solution {
105+
public List<List<Long>> splitPainting(int[][] segments) {
106+
TreeMap<Integer, Long> d = new TreeMap<>();
107+
for (int[] e : segments) {
108+
int l = e[0], r = e[1], c = e[2];
109+
d.put(l, d.getOrDefault(l, 0L) + c);
110+
d.put(r, d.getOrDefault(r, 0L) - c);
111+
}
112+
List<List<Long>> ans = new ArrayList<>();
113+
long i = 0, j = 0;
114+
long cur = 0;
115+
for (Map.Entry<Integer, Long> e : d.entrySet()) {
116+
if (Objects.equals(e.getKey(), d.firstKey())) {
117+
i = e.getKey();
118+
} else {
119+
j = e.getKey();
120+
if (cur > 0) {
121+
ans.add(Arrays.asList(i, j, cur));
122+
}
123+
i = j;
124+
}
125+
cur += e.getValue();
126+
}
127+
return ans;
128+
}
129+
}
130+
```
90131

132+
### **C++**
133+
134+
```cpp
135+
class Solution {
136+
public:
137+
vector<vector<long long>> splitPainting(vector<vector<int>>& segments) {
138+
map<int, long long> d;
139+
for (auto& e : segments)
140+
{
141+
int l = e[0], r = e[1], c = e[2];
142+
d[l] += c;
143+
d[r] -= c;
144+
}
145+
vector<vector<long long>> ans;
146+
long long i, j, cur = 0;
147+
for (auto& it : d)
148+
{
149+
if (it == *d.begin()) i = it.first;
150+
else
151+
{
152+
j = it.first;
153+
if (cur > 0) ans.push_back({i, j, cur});
154+
i = j;
155+
}
156+
cur += it.second;
157+
}
158+
return ans;
159+
}
160+
};
91161
```
92162
93163
### **...**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
vector<vector<long long>> splitPainting(vector<vector<int>>& segments) {
4+
map<int, long long> d;
5+
for (auto& e : segments)
6+
{
7+
int l = e[0], r = e[1], c = e[2];
8+
d[l] += c;
9+
d[r] -= c;
10+
}
11+
vector<vector<long long>> ans;
12+
long long i, j, cur = 0;
13+
for (auto& it : d)
14+
{
15+
if (it == *d.begin()) i = it.first;
16+
else
17+
{
18+
j = it.first;
19+
if (cur > 0) ans.push_back({i, j, cur});
20+
i = j;
21+
}
22+
cur += it.second;
23+
}
24+
return ans;
25+
}
26+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public List<List<Long>> splitPainting(int[][] segments) {
3+
TreeMap<Integer, Long> d = new TreeMap<>();
4+
for (int[] e : segments) {
5+
int l = e[0], r = e[1], c = e[2];
6+
d.put(l, d.getOrDefault(l, 0L) + c);
7+
d.put(r, d.getOrDefault(r, 0L) - c);
8+
}
9+
List<List<Long>> ans = new ArrayList<>();
10+
long i = 0, j = 0;
11+
long cur = 0;
12+
for (Map.Entry<Integer, Long> e : d.entrySet()) {
13+
if (Objects.equals(e.getKey(), d.firstKey())) {
14+
i = e.getKey();
15+
} else {
16+
j = e.getKey();
17+
if (cur > 0) {
18+
ans.add(Arrays.asList(i, j, cur));
19+
}
20+
i = j;
21+
}
22+
cur += e.getValue();
23+
}
24+
return ans;
25+
}
26+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def splitPainting(self, segments: List[List[int]]) -> List[List[int]]:
3+
d = defaultdict(int)
4+
for l, r, c in segments:
5+
d[l] += c
6+
d[r] -= c
7+
s = sorted([[k, v] for k, v in d.items()])
8+
n = len(s)
9+
for i in range(1, n):
10+
s[i][1] += s[i - 1][1]
11+
ans = []
12+
for i in range(n - 1):
13+
if s[i][1]:
14+
ans.append([s[i][0], s[i + 1][0], s[i][1]])
15+
return ans

0 commit comments

Comments
 (0)