aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2024-08-28 19:03:02 -0600
committerJack Sangdahl <[email protected]>2024-08-28 19:03:02 -0600
commit7dc61c9997c53be6c50726f2a618df397f9ac09f (patch)
tree2eb12e54b80ba3643977e9867b43ed4ab5798d2e
parent35132e9f44831f0cf18d329b15482744bd5548de (diff)
downloadleetcode-7dc61c9997c53be6c50726f2a618df397f9ac09f.tar.gz
leetcode-7dc61c9997c53be6c50726f2a618df397f9ac09f.zip
947. most stones removed with same row or column
-rw-r--r--most_stones_remove.c73
-rw-r--r--most_stones_remove.cpp18
-rw-r--r--most_stones_remove.py34
-rw-r--r--most_stones_remove.rs44
4 files changed, 154 insertions, 15 deletions
diff --git a/most_stones_remove.c b/most_stones_remove.c
new file mode 100644
index 0000000..18f368c
--- /dev/null
+++ b/most_stones_remove.c
@@ -0,0 +1,73 @@
+// 947. Most Stones Removed with Same Row or Column
+#include "leetcode.h"
+
+/*
+ * on a 2d plane, we place 'n' stones at some integer coordinate points. each
+ * coordinate point may have at most one stone. a stone can be removed if it
+ * shares either the same row or the same column as another stone that has not
+ * been removed. given an array 'stones' of length 'n' where 'stones[i] = [x_i,
+ * y_i]' represents the location of the i'th stone, return the largest possible
+ * number of stones that can be removed
+ */
+
+struct hash {
+ struct hash *parent;
+ UT_hash_handle set_hh;
+ UT_hash_handle by_row_hh;
+ UT_hash_handle by_col_hh;
+};
+
+struct hash *find_parent(struct hash *stone) {
+ while (stone->parent)
+ stone = stone->parent;
+ return stone;
+}
+
+int removeStones(int **stones, int stonesSize, int *stonesColSize) {
+ struct hash *set = NULL, *by_row = NULL, *by_col = NULL;
+ struct hash *const entries = (struct hash *)malloc(sizeof(struct hash));
+ for (int i = 0; i < stonesSize; i++) {
+ struct hash *entry = &entries[i], *row_entry, *col_entry;
+ HASH_FIND(by_row_hh, by_row, &stones[i][0], sizeof(stones[i][0]),
+ row_entry);
+ HASH_FIND(by_col_hh, by_col, &stones[i][1], sizeof(stones[i][1]),
+ col_entry);
+ if (row_entry && col_entry) {
+ struct hash *row_parent = find_parent(row_entry);
+ struct hash *col_parent = find_parent(col_entry);
+ if (row_parent != col_parent) {
+ col_parent->parent = row_parent;
+ HASH_DELETE(set_hh, set, col_parent);
+ }
+ entry->parent = row_parent;
+ } else if (row_entry) {
+ entry->parent = row_entry;
+ HASH_ADD_KEYPTR(by_col_hh, by_col, &stones[i][1], sizeof(stones[i][1]),
+ entry);
+ } else if (col_entry) {
+ entry->parent = col_entry;
+ HASH_ADD_KEYPTR(by_row_hh, by_row, &stones[i][0], sizeof(stones[i][0]),
+ entry);
+ } else {
+ entry->parent = NULL;
+ HASH_ADD_KEYPTR(set_hh, set, entry, sizeof(entry), entry);
+ HASH_ADD_KEYPTR(by_row_hh, by_row, &stones[i][0], sizeof(stones[i][0]),
+ entry);
+ HASH_ADD_KEYPTR(by_col_hh, by_col, &stones[i][1], sizeof(stones[i][1]),
+ entry);
+ }
+ }
+ int ans = stonesSize - HASH_CNT(set_hh, set);
+ HASH_CLEAR(set_hh, set);
+ HASH_CLEAR(by_row_hh, by_row);
+ HASH_CLEAR(by_col_hh, by_col);
+ free(entries);
+ return ans;
+}
+
+int main() {
+ int s1[6][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 2}, {2, 1}, {2, 2}},
+ s2[5][2] = {{0, 1}, {0, 2}, {1, 1}, {2, 0}, {2, 2}}, scs1 = 6, scs2 = 5;
+ printf("%d\n", removeStones((int **)s1, ARRAY_SIZE(s1), &scs1)); // expect: 5
+ printf("%d\n", removeStones((int **)s2, ARRAY_SIZE(s2), &scs2)); // expect: 3
+}
diff --git a/most_stones_remove.cpp b/most_stones_remove.cpp
index 5283383..632a7fc 100644
--- a/most_stones_remove.cpp
+++ b/most_stones_remove.cpp
@@ -1,8 +1,18 @@
+// 947. Most Stones Removed with Same Row or Column
#include "leetcode.h"
+/*
+ * on a 2d plane, we place 'n' stones at some integer coordinate points. each
+ * coordinate point may have at most one stone. a stone can be removed if it
+ * shares either the same row or the same column as another stone that has not
+ * been removed. given an array 'stones' of length 'n' where 'stones[i] = [x_i,
+ * y_i]' represents the location of the i'th stone, return the largest possible
+ * number of stones that can be removed
+ */
+
class Solution {
public:
- int removeStones(vvd(int) & stones) {
+ int removeStones(vector<vector<int>> &stones) {
int n = stones.size(), c = 0;
unordered_map<int, vector<int>> rows, cols;
for (int i = 0; i < n; ++i) {
@@ -30,6 +40,8 @@ public:
int main() {
Solution obj;
- vvd(int) stones = {{0, 0}, {0, 1}, {1, 0}, {1, 2}, {2, 1}, {2, 2}};
- cout << obj.removeStones(stones);
+ vvd(int) s1 = {{0, 0}, {0, 1}, {1, 0}, {1, 2}, {2, 1}, {2, 2}},
+ s2 = {{0, 1}, {0, 2}, {1, 1}, {2, 0}, {2, 2}};
+ printf("%d\n", obj.removeStones(s1)); // expect: 5
+ printf("%d\n", obj.removeStones(s2)); // expect: 3
}
diff --git a/most_stones_remove.py b/most_stones_remove.py
new file mode 100644
index 0000000..1f12d3e
--- /dev/null
+++ b/most_stones_remove.py
@@ -0,0 +1,34 @@
+# 947. Most Stones Removed with Same Row or Column
+
+"""
+on a 2d plane, we place 'n' stones at some integer coordinate stones. each
+coordinate point may have at most one stone. a stone can be removed if it
+shares either the same row or the same column as another stone that has not
+been removed. given an array 'stones' of length 'n' where 'stones[i] = [x_i,
+y_i]' represents the location of the i'th stone, return the largest possible
+number of stones that can be removed
+"""
+
+
+class Solution(object):
+ def removeStones(self, stones):
+ """
+ :type stones: List[List[int]]
+ :rtype: int
+ """
+ uf = {}
+
+ def find(x):
+ if x != uf.setdefault(x, x):
+ uf[x] = find(uf[x])
+ return uf[x]
+
+ for i, j in stones:
+ uf[find(i)] = find(~j)
+ return len(stones) - len({find(x) for x in uf})
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.removeStones(stones=[[0, 0], [0, 1], [1, 0], [1, 2], [2, 1], [2, 2]]))
+ print(obj.removeStones(stones=[[0, 0], [0, 2], [1, 1], [2, 0], [2, 2]]))
diff --git a/most_stones_remove.rs b/most_stones_remove.rs
index 8dc890d..d32137f 100644
--- a/most_stones_remove.rs
+++ b/most_stones_remove.rs
@@ -1,3 +1,14 @@
+// 947. Most Stones Removed with Same Row or Column
+
+/*
+ * on a 2d plane, we place 'n' stones at some integer coordinate points. each
+ * coordinate point may have at most one stone. a stone can be removed if it
+ * shares either the same row or the same column as another stone that has not
+ * been removed. given an array 'stones' of length 'n' where 'stones[i] = [x_i,
+ * y_i]' represents the location of the i'th stone, return the largest possible
+ * number of stones that can be removed
+ */
+
use std::collections::HashMap;
struct Solution;
@@ -9,16 +20,24 @@ impl Solution {
let mut cols: HashMap<i32, usize> = HashMap::new();
for (i, v) in stones.iter().enumerate() {
match rows.get(&v[0]) {
- Some(&j) => if Self::union_stone(i, j, &mut parent) {
- ans += 1;
+ Some(&j) => {
+ if Self::union_stone(i, j, &mut parent) {
+ ans += 1;
+ }
+ }
+ None => {
+ rows.insert(v[0], i);
}
- None => { rows.insert(v[0], i); }
}
match cols.get(&v[1]) {
- Some(&j) => if Self::union_stone(i, j, &mut parent) {
- ans += 1;
+ Some(&j) => {
+ if Self::union_stone(i, j, &mut parent) {
+ ans += 1;
+ }
+ }
+ None => {
+ cols.insert(v[1], i);
}
- None => { cols.insert(v[1], i); }
}
}
ans
@@ -39,11 +58,12 @@ impl Solution {
fn main() {
let stones = vec![
- vec![0,0],
- vec![0,1],
- vec![1,0],
- vec![1,2],
- vec![2,1],
- vec![2,2]];
+ vec![0, 0],
+ vec![0, 1],
+ vec![1, 0],
+ vec![1, 2],
+ vec![2, 1],
+ vec![2, 2],
+ ];
print!("{}", Solution::remove_stones(stones));
}