File tree 2 files changed +59
-0
lines changed
solution/1900-1999/1943.Describe the Painting
2 files changed +59
-0
lines changed Original file line number Diff line number Diff line change @@ -181,6 +181,38 @@ public:
181
181
};
182
182
```
183
183
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
+
184
216
<!-- tabs: end -->
185
217
186
218
<!-- solution: end -->
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments