From 6a670afef0596c7d40f23e5a9df563e39defa533 Mon Sep 17 00:00:00 2001 From: Tomato <18185657+taoyq1988@users.noreply.github.com> Date: Wed, 16 Apr 2025 15:56:05 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1943 (#4354) No.1943.Describe the Painting --- .../1943.Describe the Painting/README.md | 32 +++++++++++++++++++ .../1943.Describe the Painting/README_EN.md | 32 +++++++++++++++++++ .../1943.Describe the Painting/Solution.go | 27 ++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 solution/1900-1999/1943.Describe the Painting/Solution.go diff --git a/solution/1900-1999/1943.Describe the Painting/README.md b/solution/1900-1999/1943.Describe the Painting/README.md index a03305e3e0d3e..b54927ec2aabd 100644 --- a/solution/1900-1999/1943.Describe the Painting/README.md +++ b/solution/1900-1999/1943.Describe the Painting/README.md @@ -181,6 +181,38 @@ public: }; ``` +#### Go + +```go +func splitPainting(segments [][]int) [][]int64 { + d := make(map[int]int64) + for _, seg := range segments { + d[seg[0]] += int64(seg[2]) + d[seg[1]] -= int64(seg[2]) + } + dList := make([]int, 0, len(d)) + for k := range d { + dList = append(dList, k) + } + sort.Ints(dList) + + var ans [][]int64 + + i := dList[0] + cur := d[i] + for j := 1; j < len(dList); j++ { + it := d[dList[j]] + if cur > 0 { + ans = append(ans, []int64{int64(i), int64(dList[j]), cur}) + } + cur += it + i = dList[j] + } + + return ans +} +``` + diff --git a/solution/1900-1999/1943.Describe the Painting/README_EN.md b/solution/1900-1999/1943.Describe the Painting/README_EN.md index be22156df45ea..a93f1ae91a32d 100644 --- a/solution/1900-1999/1943.Describe the Painting/README_EN.md +++ b/solution/1900-1999/1943.Describe the Painting/README_EN.md @@ -179,6 +179,38 @@ public: }; ``` +#### Go + +```go +func splitPainting(segments [][]int) [][]int64 { + d := make(map[int]int64) + for _, seg := range segments { + d[seg[0]] += int64(seg[2]) + d[seg[1]] -= int64(seg[2]) + } + dList := make([]int, 0, len(d)) + for k := range d { + dList = append(dList, k) + } + sort.Ints(dList) + + var ans [][]int64 + + i := dList[0] + cur := d[i] + for j := 1; j < len(dList); j++ { + it := d[dList[j]] + if cur > 0 { + ans = append(ans, []int64{int64(i), int64(dList[j]), cur}) + } + cur += it + i = dList[j] + } + + return ans +} +``` + diff --git a/solution/1900-1999/1943.Describe the Painting/Solution.go b/solution/1900-1999/1943.Describe the Painting/Solution.go new file mode 100644 index 0000000000000..c1768330647ff --- /dev/null +++ b/solution/1900-1999/1943.Describe the Painting/Solution.go @@ -0,0 +1,27 @@ +func splitPainting(segments [][]int) [][]int64 { + d := make(map[int]int64) + for _, seg := range segments { + d[seg[0]] += int64(seg[2]) + d[seg[1]] -= int64(seg[2]) + } + dList := make([]int, 0, len(d)) + for k := range d { + dList = append(dList, k) + } + sort.Ints(dList) + + var ans [][]int64 + + i := dList[0] + cur := d[i] + for j := 1; j < len(dList); j++ { + it := d[dList[j]] + if cur > 0 { + ans = append(ans, []int64{int64(i), int64(dList[j]), cur}) + } + cur += it + i = dList[j] + } + + return ans +}