From d721ebe5a93ca53eb0380ab9a6baedaa40d8febd Mon Sep 17 00:00:00 2001 From: YDZ Date: Mon, 5 Aug 2019 21:41:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20problem=20947?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ... Stones Removed with Same Row or Column.go | 22 ++++++++ ...es Removed with Same Row or Column_test.go | 51 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 Algorithms/0947. Most Stones Removed with Same Row or Column/947. Most Stones Removed with Same Row or Column.go create mode 100644 Algorithms/0947. Most Stones Removed with Same Row or Column/947. Most Stones Removed with Same Row or Column_test.go diff --git a/Algorithms/0947. Most Stones Removed with Same Row or Column/947. Most Stones Removed with Same Row or Column.go b/Algorithms/0947. Most Stones Removed with Same Row or Column/947. Most Stones Removed with Same Row or Column.go new file mode 100644 index 00000000..e33c2e00 --- /dev/null +++ b/Algorithms/0947. Most Stones Removed with Same Row or Column/947. Most Stones Removed with Same Row or Column.go @@ -0,0 +1,22 @@ +package leetcode + +func removeStones(stones [][]int) int { + if len(stones) <= 1 { + return 0 + } + uf, rowMap, colMap := UnionFind{}, map[int]int{}, map[int]int{} + uf.init(len(stones)) + for i := 0; i < len(stones); i++ { + if _, ok := rowMap[stones[i][0]]; ok { + uf.union(rowMap[stones[i][0]], i) + } else { + rowMap[stones[i][0]] = i + } + if _, ok := colMap[stones[i][1]]; ok { + uf.union(colMap[stones[i][1]], i) + } else { + colMap[stones[i][1]] = i + } + } + return len(stones) - uf.totalCount() +} diff --git a/Algorithms/0947. Most Stones Removed with Same Row or Column/947. Most Stones Removed with Same Row or Column_test.go b/Algorithms/0947. Most Stones Removed with Same Row or Column/947. Most Stones Removed with Same Row or Column_test.go new file mode 100644 index 00000000..54328dce --- /dev/null +++ b/Algorithms/0947. Most Stones Removed with Same Row or Column/947. Most Stones Removed with Same Row or Column_test.go @@ -0,0 +1,51 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question947 struct { + para947 + ans947 +} + +// para 是参数 +// one 代表第一个参数 +type para947 struct { + stones [][]int +} + +// ans 是答案 +// one 代表第一个答案 +type ans947 struct { + one int +} + +func Test_Problem947(t *testing.T) { + + qs := []question947{ + question947{ + para947{[][]int{[]int{0, 0}, []int{0, 1}, []int{1, 0}, []int{1, 2}, []int{2, 1}, []int{2, 2}}}, + ans947{5}, + }, + + question947{ + para947{[][]int{[]int{0, 0}, []int{0, 2}, []int{1, 1}, []int{2, 0}, []int{2, 2}}}, + ans947{3}, + }, + + question947{ + para947{[][]int{[]int{0, 0}}}, + ans947{0}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 947------------------------\n") + + for _, q := range qs { + _, p := q.ans947, q.para947 + fmt.Printf("【input】:%v 【output】:%v\n", p, removeStones(p.stones)) + } + fmt.Printf("\n\n\n") +}