Skip to content

Commit f40d0a9

Browse files
committed
feat: add solutions to lc problem: No.1943 (#4354)
No.1943.Describe the Painting
1 parent 56f8cb5 commit f40d0a9

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

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

+32
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,38 @@ public:
181181
};
182182
```
183183
184+
#### Go
185+
186+
```go
187+
func splitPainting(segments [][]int) [][]int64 {
188+
colorMap := make(map[int]int64)
189+
for _, seg := range segments {
190+
colorMap[seg[0]] += int64(seg[2])
191+
colorMap[seg[1]] -= int64(seg[2])
192+
}
193+
points := make([]int, 0, len(colorMap))
194+
for k := range colorMap {
195+
points = append(points, k)
196+
}
197+
sort.Ints(points)
198+
199+
var result [][]int64
200+
201+
i := points[0]
202+
currentColor := colorMap[i]
203+
for p := 1; p < len(points); p++ {
204+
color := colorMap[points[p]]
205+
if currentColor > 0 {
206+
result = append(result, []int64{int64(i), int64(points[p]), currentColor})
207+
}
208+
currentColor += color
209+
i = points[p]
210+
}
211+
212+
return result
213+
}
214+
```
215+
184216
<!-- tabs:end -->
185217

186218
<!-- solution:end -->
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
func splitPainting(segments [][]int) [][]int64 {
2+
colorMap := make(map[int]int64)
3+
for _, seg := range segments {
4+
colorMap[seg[0]] += int64(seg[2])
5+
colorMap[seg[1]] -= int64(seg[2])
6+
}
7+
points := make([]int, 0, len(colorMap))
8+
for k := range colorMap {
9+
points = append(points, k)
10+
}
11+
sort.Ints(points)
12+
13+
var result [][]int64
14+
15+
i := points[0]
16+
currentColor := colorMap[i]
17+
for p := 1; p < len(points); p++ {
18+
color := colorMap[points[p]]
19+
if currentColor > 0 {
20+
result = append(result, []int64{int64(i), int64(points[p]), currentColor})
21+
}
22+
currentColor += color
23+
i = points[p]
24+
}
25+
26+
return result
27+
}

0 commit comments

Comments
 (0)