From 4b39d60f56bfe3a4cd2c2a935f7f8f23a2319d7f Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sun, 23 Mar 2025 19:57:27 +0800 Subject: [PATCH 1/2] feat: add solutions to lc problem: No.3492 (#4289) No.3492.Maximum Containers on a Ship --- .../README.md | 33 ++++++++++++++++--- .../README_EN.md | 33 ++++++++++++++++--- .../Solution.cpp | 6 ++++ .../Solution.go | 3 ++ .../Solution.java | 5 +++ .../Solution.py | 3 ++ .../Solution.ts | 3 ++ 7 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 solution/3400-3499/3492.Maximum Containers on a Ship/Solution.cpp create mode 100644 solution/3400-3499/3492.Maximum Containers on a Ship/Solution.go create mode 100644 solution/3400-3499/3492.Maximum Containers on a Ship/Solution.java create mode 100644 solution/3400-3499/3492.Maximum Containers on a Ship/Solution.py create mode 100644 solution/3400-3499/3492.Maximum Containers on a Ship/Solution.ts diff --git a/solution/3400-3499/3492.Maximum Containers on a Ship/README.md b/solution/3400-3499/3492.Maximum Containers on a Ship/README.md index 5de14b25d0a6b..1340937500a00 100644 --- a/solution/3400-3499/3492.Maximum Containers on a Ship/README.md +++ b/solution/3400-3499/3492.Maximum Containers on a Ship/README.md @@ -62,32 +62,57 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3492.Ma -### 方法一 +### 方法一:数学 + +我们先计算出船上可以装载的最大重量,即 $n \times n \times w$,然后取其与 $\text{maxWeight}$ 的最小值,再除以 $w$ 即可。 + +时间复杂度 $O(1)$,空间复杂度 $O(1)$。 #### Python3 ```python - +class Solution: + def maxContainers(self, n: int, w: int, maxWeight: int) -> int: + return min(n * n * w, maxWeight) // w ``` #### Java ```java - +class Solution { + public int maxContainers(int n, int w, int maxWeight) { + return Math.min(n * n * w, maxWeight) / w; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int maxContainers(int n, int w, int maxWeight) { + return min(n * n * w, maxWeight) / w; + } +}; ``` #### Go ```go +func maxContainers(n int, w int, maxWeight int) int { + return min(n*n*w, maxWeight) / w +} +``` + +#### TypeScript +```ts +function maxContainers(n: number, w: number, maxWeight: number): number { + return (Math.min(n * n * w, maxWeight) / w) | 0; +} ``` diff --git a/solution/3400-3499/3492.Maximum Containers on a Ship/README_EN.md b/solution/3400-3499/3492.Maximum Containers on a Ship/README_EN.md index e835b5810361d..5f0a5e0d56753 100644 --- a/solution/3400-3499/3492.Maximum Containers on a Ship/README_EN.md +++ b/solution/3400-3499/3492.Maximum Containers on a Ship/README_EN.md @@ -60,32 +60,57 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3492.Ma -### Solution 1 +### Solution 1: Mathematics + +First, we calculate the maximum weight the boat can carry, which is $n \times n \times w$. Then, we take the minimum of this value and $\text{maxWeight}$, and divide it by $w$. + +The time complexity is $O(1)$, and the space complexity is $O(1)$. #### Python3 ```python - +class Solution: + def maxContainers(self, n: int, w: int, maxWeight: int) -> int: + return min(n * n * w, maxWeight) // w ``` #### Java ```java - +class Solution { + public int maxContainers(int n, int w, int maxWeight) { + return Math.min(n * n * w, maxWeight) / w; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int maxContainers(int n, int w, int maxWeight) { + return min(n * n * w, maxWeight) / w; + } +}; ``` #### Go ```go +func maxContainers(n int, w int, maxWeight int) int { + return min(n*n*w, maxWeight) / w +} +``` + +#### TypeScript +```ts +function maxContainers(n: number, w: number, maxWeight: number): number { + return (Math.min(n * n * w, maxWeight) / w) | 0; +} ``` diff --git a/solution/3400-3499/3492.Maximum Containers on a Ship/Solution.cpp b/solution/3400-3499/3492.Maximum Containers on a Ship/Solution.cpp new file mode 100644 index 0000000000000..05840e423cf7c --- /dev/null +++ b/solution/3400-3499/3492.Maximum Containers on a Ship/Solution.cpp @@ -0,0 +1,6 @@ +class Solution { +public: + int maxContainers(int n, int w, int maxWeight) { + return min(n * n * w, maxWeight) / w; + } +}; diff --git a/solution/3400-3499/3492.Maximum Containers on a Ship/Solution.go b/solution/3400-3499/3492.Maximum Containers on a Ship/Solution.go new file mode 100644 index 0000000000000..709c3430f7b00 --- /dev/null +++ b/solution/3400-3499/3492.Maximum Containers on a Ship/Solution.go @@ -0,0 +1,3 @@ +func maxContainers(n int, w int, maxWeight int) int { + return min(n*n*w, maxWeight) / w +} diff --git a/solution/3400-3499/3492.Maximum Containers on a Ship/Solution.java b/solution/3400-3499/3492.Maximum Containers on a Ship/Solution.java new file mode 100644 index 0000000000000..884a332034ac1 --- /dev/null +++ b/solution/3400-3499/3492.Maximum Containers on a Ship/Solution.java @@ -0,0 +1,5 @@ +class Solution { + public int maxContainers(int n, int w, int maxWeight) { + return Math.min(n * n * w, maxWeight) / w; + } +} diff --git a/solution/3400-3499/3492.Maximum Containers on a Ship/Solution.py b/solution/3400-3499/3492.Maximum Containers on a Ship/Solution.py new file mode 100644 index 0000000000000..1aeac46ab7eae --- /dev/null +++ b/solution/3400-3499/3492.Maximum Containers on a Ship/Solution.py @@ -0,0 +1,3 @@ +class Solution: + def maxContainers(self, n: int, w: int, maxWeight: int) -> int: + return min(n * n * w, maxWeight) // w diff --git a/solution/3400-3499/3492.Maximum Containers on a Ship/Solution.ts b/solution/3400-3499/3492.Maximum Containers on a Ship/Solution.ts new file mode 100644 index 0000000000000..c6dba24919781 --- /dev/null +++ b/solution/3400-3499/3492.Maximum Containers on a Ship/Solution.ts @@ -0,0 +1,3 @@ +function maxContainers(n: number, w: number, maxWeight: number): number { + return (Math.min(n * n * w, maxWeight) / w) | 0; +} From 34f5f0cc03a3b7d38c764eb958ad6130e94b7ed6 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sun, 23 Mar 2025 21:14:04 +0800 Subject: [PATCH 2/2] feat: add solutions to lc problem: No.3493 (#4290) No.3493.Properties Graph --- .../3400-3499/3493.Properties Graph/README.md | 229 +++++++++++++++++- .../3493.Properties Graph/README_EN.md | 229 +++++++++++++++++- .../3493.Properties Graph/Solution.cpp | 46 ++++ .../3493.Properties Graph/Solution.go | 46 ++++ .../3493.Properties Graph/Solution.java | 50 ++++ .../3493.Properties Graph/Solution.py | 24 ++ .../3493.Properties Graph/Solution.ts | 46 ++++ 7 files changed, 662 insertions(+), 8 deletions(-) create mode 100644 solution/3400-3499/3493.Properties Graph/Solution.cpp create mode 100644 solution/3400-3499/3493.Properties Graph/Solution.go create mode 100644 solution/3400-3499/3493.Properties Graph/Solution.java create mode 100644 solution/3400-3499/3493.Properties Graph/Solution.py create mode 100644 solution/3400-3499/3493.Properties Graph/Solution.ts diff --git a/solution/3400-3499/3493.Properties Graph/README.md b/solution/3400-3499/3493.Properties Graph/README.md index 3072218d938e6..f43bf359013eb 100644 --- a/solution/3400-3499/3493.Properties Graph/README.md +++ b/solution/3400-3499/3493.Properties Graph/README.md @@ -81,32 +81,253 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3493.Pr -### 方法一 +### 方法一:哈希表 + DFS + +我们先将每个属性数组转换为一个哈希表,存储在哈希表数组 $\textit{ss}$ 中。定义一个图 $\textit{g}$,其中 $\textit{g}[i]$ 存储了与属性数组 $\textit{properties}[i]$ 有边相连的属性数组的索引。 + +然后我们遍历所有的属性哈希表,对于每一对属性哈希表 $(i, j)$,其中 $j < i$,我们检查这两个属性哈希表中的交集元素个数是否大于等于 $k$,如果是,则在图 $\textit{g}$ 中添加一条从 $i$ 到 $j$ 的边,同时在图 $\textit{g}$ 中添加一条从 $j$ 到 $i$ 的边。 + +最后,我们使用深度优先搜索计算图 $\textit{g}$ 的连通分量的数量。 + +时间复杂度 $O(n^2 \times m)$,空间复杂度 $O(n \times m)$。其中 $n$ 是属性数组的长度,而 $m$ 是属性数组中的元素个数。 #### Python3 ```python - +class Solution: + def numberOfComponents(self, properties: List[List[int]], k: int) -> int: + def dfs(i: int) -> None: + vis[i] = True + for j in g[i]: + if not vis[j]: + dfs(j) + + n = len(properties) + ss = list(map(set, properties)) + g = [[] for _ in range(n)] + for i, s1 in enumerate(ss): + for j in range(i): + s2 = ss[j] + if len(s1 & s2) >= k: + g[i].append(j) + g[j].append(i) + ans = 0 + vis = [False] * n + for i in range(n): + if not vis[i]: + dfs(i) + ans += 1 + return ans ``` #### Java ```java - +class Solution { + private List[] g; + private boolean[] vis; + + public int numberOfComponents(int[][] properties, int k) { + int n = properties.length; + g = new List[n]; + Set[] ss = new Set[n]; + Arrays.setAll(g, i -> new ArrayList<>()); + Arrays.setAll(ss, i -> new HashSet<>()); + for (int i = 0; i < n; ++i) { + for (int x : properties[i]) { + ss[i].add(x); + } + } + for (int i = 0; i < n; ++i) { + for (int j = 0; j < i; ++j) { + int cnt = 0; + for (int x : ss[i]) { + if (ss[j].contains(x)) { + ++cnt; + } + } + if (cnt >= k) { + g[i].add(j); + g[j].add(i); + } + } + } + + int ans = 0; + vis = new boolean[n]; + for (int i = 0; i < n; ++i) { + if (!vis[i]) { + dfs(i); + ++ans; + } + } + return ans; + } + + private void dfs(int i) { + vis[i] = true; + for (int j : g[i]) { + if (!vis[j]) { + dfs(j); + } + } + } +} ``` #### C++ ```cpp - +class Solution { +public: + int numberOfComponents(vector>& properties, int k) { + int n = properties.size(); + unordered_set ss[n]; + vector g[n]; + for (int i = 0; i < n; ++i) { + for (int x : properties[i]) { + ss[i].insert(x); + } + } + for (int i = 0; i < n; ++i) { + auto& s1 = ss[i]; + for (int j = 0; j < i; ++j) { + auto& s2 = ss[j]; + int cnt = 0; + for (int x : s1) { + if (s2.contains(x)) { + ++cnt; + } + } + if (cnt >= k) { + g[i].push_back(j); + g[j].push_back(i); + } + } + } + int ans = 0; + vector vis(n); + auto dfs = [&](this auto&& dfs, int i) -> void { + vis[i] = true; + for (int j : g[i]) { + if (!vis[j]) { + dfs(j); + } + } + }; + for (int i = 0; i < n; ++i) { + if (!vis[i]) { + dfs(i); + ++ans; + } + } + return ans; + } +}; ``` #### Go ```go +func numberOfComponents(properties [][]int, k int) (ans int) { + n := len(properties) + ss := make([]map[int]struct{}, n) + g := make([][]int, n) + + for i := 0; i < n; i++ { + ss[i] = make(map[int]struct{}) + for _, x := range properties[i] { + ss[i][x] = struct{}{} + } + } + + for i := 0; i < n; i++ { + for j := 0; j < i; j++ { + cnt := 0 + for x := range ss[i] { + if _, ok := ss[j][x]; ok { + cnt++ + } + } + if cnt >= k { + g[i] = append(g[i], j) + g[j] = append(g[j], i) + } + } + } + + vis := make([]bool, n) + var dfs func(int) + dfs = func(i int) { + vis[i] = true + for _, j := range g[i] { + if !vis[j] { + dfs(j) + } + } + } + + for i := 0; i < n; i++ { + if !vis[i] { + dfs(i) + ans++ + } + } + return +} +``` +#### TypeScript + +```ts +function numberOfComponents(properties: number[][], k: number): number { + const n = properties.length; + const ss: Set[] = Array.from({ length: n }, () => new Set()); + const g: number[][] = Array.from({ length: n }, () => []); + + for (let i = 0; i < n; i++) { + for (const x of properties[i]) { + ss[i].add(x); + } + } + + for (let i = 0; i < n; i++) { + for (let j = 0; j < i; j++) { + let cnt = 0; + for (const x of ss[i]) { + if (ss[j].has(x)) { + cnt++; + } + } + if (cnt >= k) { + g[i].push(j); + g[j].push(i); + } + } + } + + let ans = 0; + const vis: boolean[] = Array(n).fill(false); + + const dfs = (i: number) => { + vis[i] = true; + for (const j of g[i]) { + if (!vis[j]) { + dfs(j); + } + } + }; + + for (let i = 0; i < n; i++) { + if (!vis[i]) { + dfs(i); + ans++; + } + } + return ans; +} ``` diff --git a/solution/3400-3499/3493.Properties Graph/README_EN.md b/solution/3400-3499/3493.Properties Graph/README_EN.md index 9b92ddfb2dea4..3ca55fe7826bd 100644 --- a/solution/3400-3499/3493.Properties Graph/README_EN.md +++ b/solution/3400-3499/3493.Properties Graph/README_EN.md @@ -79,32 +79,253 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3493.Pr -### Solution 1 +### Solution 1: Hash Table + DFS + +We first convert each attribute array into a hash table and store them in a hash table array $\textit{ss}$. We define a graph $\textit{g}$, where $\textit{g}[i]$ stores the indices of attribute arrays that are connected to $\textit{properties}[i]$. + +Then, we iterate through all attribute hash tables. For each pair of attribute hash tables $(i, j)$ where $j < i$, we check whether the number of common elements between them is at least $k$. If so, we add an edge from $i$ to $j$ in the graph $\textit{g}$, as well as an edge from $j$ to $i$. + +Finally, we use Depth-First Search (DFS) to compute the number of connected components in the graph $\textit{g}$. + +The time complexity is $O(n^2 \times m)$, and the space complexity is $O(n \times m)$, where $n$ is the length of the attribute arrays and $m$ is the number of elements in an attribute array. #### Python3 ```python - +class Solution: + def numberOfComponents(self, properties: List[List[int]], k: int) -> int: + def dfs(i: int) -> None: + vis[i] = True + for j in g[i]: + if not vis[j]: + dfs(j) + + n = len(properties) + ss = list(map(set, properties)) + g = [[] for _ in range(n)] + for i, s1 in enumerate(ss): + for j in range(i): + s2 = ss[j] + if len(s1 & s2) >= k: + g[i].append(j) + g[j].append(i) + ans = 0 + vis = [False] * n + for i in range(n): + if not vis[i]: + dfs(i) + ans += 1 + return ans ``` #### Java ```java - +class Solution { + private List[] g; + private boolean[] vis; + + public int numberOfComponents(int[][] properties, int k) { + int n = properties.length; + g = new List[n]; + Set[] ss = new Set[n]; + Arrays.setAll(g, i -> new ArrayList<>()); + Arrays.setAll(ss, i -> new HashSet<>()); + for (int i = 0; i < n; ++i) { + for (int x : properties[i]) { + ss[i].add(x); + } + } + for (int i = 0; i < n; ++i) { + for (int j = 0; j < i; ++j) { + int cnt = 0; + for (int x : ss[i]) { + if (ss[j].contains(x)) { + ++cnt; + } + } + if (cnt >= k) { + g[i].add(j); + g[j].add(i); + } + } + } + + int ans = 0; + vis = new boolean[n]; + for (int i = 0; i < n; ++i) { + if (!vis[i]) { + dfs(i); + ++ans; + } + } + return ans; + } + + private void dfs(int i) { + vis[i] = true; + for (int j : g[i]) { + if (!vis[j]) { + dfs(j); + } + } + } +} ``` #### C++ ```cpp - +class Solution { +public: + int numberOfComponents(vector>& properties, int k) { + int n = properties.size(); + unordered_set ss[n]; + vector g[n]; + for (int i = 0; i < n; ++i) { + for (int x : properties[i]) { + ss[i].insert(x); + } + } + for (int i = 0; i < n; ++i) { + auto& s1 = ss[i]; + for (int j = 0; j < i; ++j) { + auto& s2 = ss[j]; + int cnt = 0; + for (int x : s1) { + if (s2.contains(x)) { + ++cnt; + } + } + if (cnt >= k) { + g[i].push_back(j); + g[j].push_back(i); + } + } + } + int ans = 0; + vector vis(n); + auto dfs = [&](this auto&& dfs, int i) -> void { + vis[i] = true; + for (int j : g[i]) { + if (!vis[j]) { + dfs(j); + } + } + }; + for (int i = 0; i < n; ++i) { + if (!vis[i]) { + dfs(i); + ++ans; + } + } + return ans; + } +}; ``` #### Go ```go +func numberOfComponents(properties [][]int, k int) (ans int) { + n := len(properties) + ss := make([]map[int]struct{}, n) + g := make([][]int, n) + + for i := 0; i < n; i++ { + ss[i] = make(map[int]struct{}) + for _, x := range properties[i] { + ss[i][x] = struct{}{} + } + } + + for i := 0; i < n; i++ { + for j := 0; j < i; j++ { + cnt := 0 + for x := range ss[i] { + if _, ok := ss[j][x]; ok { + cnt++ + } + } + if cnt >= k { + g[i] = append(g[i], j) + g[j] = append(g[j], i) + } + } + } + + vis := make([]bool, n) + var dfs func(int) + dfs = func(i int) { + vis[i] = true + for _, j := range g[i] { + if !vis[j] { + dfs(j) + } + } + } + + for i := 0; i < n; i++ { + if !vis[i] { + dfs(i) + ans++ + } + } + return +} +``` +#### TypeScript + +```ts +function numberOfComponents(properties: number[][], k: number): number { + const n = properties.length; + const ss: Set[] = Array.from({ length: n }, () => new Set()); + const g: number[][] = Array.from({ length: n }, () => []); + + for (let i = 0; i < n; i++) { + for (const x of properties[i]) { + ss[i].add(x); + } + } + + for (let i = 0; i < n; i++) { + for (let j = 0; j < i; j++) { + let cnt = 0; + for (const x of ss[i]) { + if (ss[j].has(x)) { + cnt++; + } + } + if (cnt >= k) { + g[i].push(j); + g[j].push(i); + } + } + } + + let ans = 0; + const vis: boolean[] = Array(n).fill(false); + + const dfs = (i: number) => { + vis[i] = true; + for (const j of g[i]) { + if (!vis[j]) { + dfs(j); + } + } + }; + + for (let i = 0; i < n; i++) { + if (!vis[i]) { + dfs(i); + ans++; + } + } + return ans; +} ``` diff --git a/solution/3400-3499/3493.Properties Graph/Solution.cpp b/solution/3400-3499/3493.Properties Graph/Solution.cpp new file mode 100644 index 0000000000000..fc6ede03481d1 --- /dev/null +++ b/solution/3400-3499/3493.Properties Graph/Solution.cpp @@ -0,0 +1,46 @@ +class Solution { +public: + int numberOfComponents(vector>& properties, int k) { + int n = properties.size(); + unordered_set ss[n]; + vector g[n]; + for (int i = 0; i < n; ++i) { + for (int x : properties[i]) { + ss[i].insert(x); + } + } + for (int i = 0; i < n; ++i) { + auto& s1 = ss[i]; + for (int j = 0; j < i; ++j) { + auto& s2 = ss[j]; + int cnt = 0; + for (int x : s1) { + if (s2.contains(x)) { + ++cnt; + } + } + if (cnt >= k) { + g[i].push_back(j); + g[j].push_back(i); + } + } + } + int ans = 0; + vector vis(n); + auto dfs = [&](this auto&& dfs, int i) -> void { + vis[i] = true; + for (int j : g[i]) { + if (!vis[j]) { + dfs(j); + } + } + }; + for (int i = 0; i < n; ++i) { + if (!vis[i]) { + dfs(i); + ++ans; + } + } + return ans; + } +}; diff --git a/solution/3400-3499/3493.Properties Graph/Solution.go b/solution/3400-3499/3493.Properties Graph/Solution.go new file mode 100644 index 0000000000000..959957c6ff87a --- /dev/null +++ b/solution/3400-3499/3493.Properties Graph/Solution.go @@ -0,0 +1,46 @@ +func numberOfComponents(properties [][]int, k int) (ans int) { + n := len(properties) + ss := make([]map[int]struct{}, n) + g := make([][]int, n) + + for i := 0; i < n; i++ { + ss[i] = make(map[int]struct{}) + for _, x := range properties[i] { + ss[i][x] = struct{}{} + } + } + + for i := 0; i < n; i++ { + for j := 0; j < i; j++ { + cnt := 0 + for x := range ss[i] { + if _, ok := ss[j][x]; ok { + cnt++ + } + } + if cnt >= k { + g[i] = append(g[i], j) + g[j] = append(g[j], i) + } + } + } + + vis := make([]bool, n) + var dfs func(int) + dfs = func(i int) { + vis[i] = true + for _, j := range g[i] { + if !vis[j] { + dfs(j) + } + } + } + + for i := 0; i < n; i++ { + if !vis[i] { + dfs(i) + ans++ + } + } + return +} diff --git a/solution/3400-3499/3493.Properties Graph/Solution.java b/solution/3400-3499/3493.Properties Graph/Solution.java new file mode 100644 index 0000000000000..eeca61f158f70 --- /dev/null +++ b/solution/3400-3499/3493.Properties Graph/Solution.java @@ -0,0 +1,50 @@ +class Solution { + private List[] g; + private boolean[] vis; + + public int numberOfComponents(int[][] properties, int k) { + int n = properties.length; + g = new List[n]; + Set[] ss = new Set[n]; + Arrays.setAll(g, i -> new ArrayList<>()); + Arrays.setAll(ss, i -> new HashSet<>()); + for (int i = 0; i < n; ++i) { + for (int x : properties[i]) { + ss[i].add(x); + } + } + for (int i = 0; i < n; ++i) { + for (int j = 0; j < i; ++j) { + int cnt = 0; + for (int x : ss[i]) { + if (ss[j].contains(x)) { + ++cnt; + } + } + if (cnt >= k) { + g[i].add(j); + g[j].add(i); + } + } + } + + int ans = 0; + vis = new boolean[n]; + for (int i = 0; i < n; ++i) { + if (!vis[i]) { + dfs(i); + ++ans; + } + } + return ans; + } + + private void dfs(int i) { + vis[i] = true; + for (int j : g[i]) { + if (!vis[j]) { + dfs(j); + } + } + } +} diff --git a/solution/3400-3499/3493.Properties Graph/Solution.py b/solution/3400-3499/3493.Properties Graph/Solution.py new file mode 100644 index 0000000000000..b29ceaac51c7a --- /dev/null +++ b/solution/3400-3499/3493.Properties Graph/Solution.py @@ -0,0 +1,24 @@ +class Solution: + def numberOfComponents(self, properties: List[List[int]], k: int) -> int: + def dfs(i: int) -> None: + vis[i] = True + for j in g[i]: + if not vis[j]: + dfs(j) + + n = len(properties) + ss = list(map(set, properties)) + g = [[] for _ in range(n)] + for i, s1 in enumerate(ss): + for j in range(i): + s2 = ss[j] + if len(s1 & s2) >= k: + g[i].append(j) + g[j].append(i) + ans = 0 + vis = [False] * n + for i in range(n): + if not vis[i]: + dfs(i) + ans += 1 + return ans diff --git a/solution/3400-3499/3493.Properties Graph/Solution.ts b/solution/3400-3499/3493.Properties Graph/Solution.ts new file mode 100644 index 0000000000000..4abf2a9836f43 --- /dev/null +++ b/solution/3400-3499/3493.Properties Graph/Solution.ts @@ -0,0 +1,46 @@ +function numberOfComponents(properties: number[][], k: number): number { + const n = properties.length; + const ss: Set[] = Array.from({ length: n }, () => new Set()); + const g: number[][] = Array.from({ length: n }, () => []); + + for (let i = 0; i < n; i++) { + for (const x of properties[i]) { + ss[i].add(x); + } + } + + for (let i = 0; i < n; i++) { + for (let j = 0; j < i; j++) { + let cnt = 0; + for (const x of ss[i]) { + if (ss[j].has(x)) { + cnt++; + } + } + if (cnt >= k) { + g[i].push(j); + g[j].push(i); + } + } + } + + let ans = 0; + const vis: boolean[] = Array(n).fill(false); + + const dfs = (i: number) => { + vis[i] = true; + for (const j of g[i]) { + if (!vis[j]) { + dfs(j); + } + } + }; + + for (let i = 0; i < n; i++) { + if (!vis[i]) { + dfs(i); + ans++; + } + } + return ans; +}