From 64bec1db88ac710cf1f652dc913899eaa9e198ba Mon Sep 17 00:00:00 2001 From: yanglbme Date: Tue, 1 Oct 2024 11:28:42 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1583 No.1583.Count Unhappy Friends --- .../1583.Count Unhappy Friends/README.md | 79 +++++++++++++----- .../1583.Count Unhappy Friends/README_EN.md | 83 +++++++++++++++---- .../1583.Count Unhappy Friends/Solution.cpp | 13 ++- .../1583.Count Unhappy Friends/Solution.go | 17 ++-- .../1583.Count Unhappy Friends/Solution.java | 9 +- .../1583.Count Unhappy Friends/Solution.py | 9 +- .../1583.Count Unhappy Friends/Solution.ts | 26 ++++++ 7 files changed, 178 insertions(+), 58 deletions(-) create mode 100644 solution/1500-1599/1583.Count Unhappy Friends/Solution.ts diff --git a/solution/1500-1599/1583.Count Unhappy Friends/README.md b/solution/1500-1599/1583.Count Unhappy Friends/README.md index 471b49210b8f6..a65acfbd9bc2e 100644 --- a/solution/1500-1599/1583.Count Unhappy Friends/README.md +++ b/solution/1500-1599/1583.Count Unhappy Friends/README.md @@ -91,11 +91,11 @@ tags: -### 方法一:数组 + 枚举 +### 方法一:枚举 -我们用数组 $d$ 记录每个朋友与其它朋友的亲近程度,其中 $d[i][j]$ 表示朋友 $i$ 对 $j$ 的亲近程度(值越小,越亲近),另外,用数组 $p$ 记录每个朋友的配对朋友。 +我们用数组 $\textit{d}$ 记录每个朋友与其它朋友的亲近程度,其中 $\textit{d}[i][j]$ 表示朋友 $i$ 对 $j$ 的亲近程度(值越小,越亲近),另外,用数组 $\textit{p}$ 记录每个朋友的配对朋友。 -我们枚举每个朋友 $x$,对于 $x$ 的配对朋友 $y$,我们找到 $x$ 对 $y$ 的亲近程度 $d[x][y]$,然后枚举比 $d[x][y]$ 更亲近的其它朋友 $u$,如果存在 $u$ 对 $x$ 的亲近程度 $d[u][x]$ 比 $d[u][y]$ 更高,那么 $x$ 就是不开心的朋友,将结果加一即可。 +我们枚举每个朋友 $x$,对于 $x$ 的配对朋友 $y$,我们找到 $x$ 对 $y$ 的亲近程度 $\textit{d}[x][y]$,然后枚举比 $\textit{d}[x][y]$ 更亲近的其它朋友 $u$,如果存在 $u$ 对 $x$ 的亲近程度 $\textit{d}[u][x]$ 比 $\textit{d}[u][y]$ 更高,那么 $x$ 就是不开心的朋友,将结果加一即可。 枚举结束后,即可得到不开心的朋友的数目。 @@ -110,7 +110,7 @@ class Solution: def unhappyFriends( self, n: int, preferences: List[List[int]], pairs: List[List[int]] ) -> int: - d = [{p: i for i, p in enumerate(v)} for v in preferences] + d = [{x: j for j, x in enumerate(p)} for p in preferences] p = {} for x, y in pairs: p[x] = y @@ -118,7 +118,12 @@ class Solution: ans = 0 for x in range(n): y = p[x] - ans += any(d[u][x] < d[u][p[u]] for u in preferences[x][: d[x][y]]) + for i in range(d[x][y]): + u = preferences[x][i] + v = p[u] + if d[u][x] < d[u][v]: + ans += 1 + break return ans ``` @@ -142,15 +147,14 @@ class Solution { int ans = 0; for (int x = 0; x < n; ++x) { int y = p[x]; - int find = 0; for (int i = 0; i < d[x][y]; ++i) { int u = preferences[x][i]; - if (d[u][x] < d[u][p[u]]) { - find = 1; + int v = p[u]; + if (d[u][x] < d[u][v]) { + ++ans; break; } } - ans += find; } return ans; } @@ -163,13 +167,13 @@ class Solution { class Solution { public: int unhappyFriends(int n, vector>& preferences, vector>& pairs) { - int d[n][n]; - int p[n]; + vector> d(n, vector(n)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n - 1; ++j) { d[i][preferences[i][j]] = j; } } + vector p(n, 0); for (auto& e : pairs) { int x = e[0], y = e[1]; p[x] = y; @@ -178,15 +182,14 @@ public: int ans = 0; for (int x = 0; x < n; ++x) { int y = p[x]; - int find = 0; for (int i = 0; i < d[x][y]; ++i) { int u = preferences[x][i]; - if (d[u][x] < d[u][p[u]]) { - find = 1; + int v = p[u]; + if (d[u][x] < d[u][v]) { + ++ans; break; } } - ans += find; } return ans; } @@ -198,34 +201,70 @@ public: ```go func unhappyFriends(n int, preferences [][]int, pairs [][]int) (ans int) { d := make([][]int, n) - p := make([]int, n) for i := range d { d[i] = make([]int, n) + } + + for i := 0; i < n; i++ { for j := 0; j < n-1; j++ { d[i][preferences[i][j]] = j } } + + p := make([]int, n) for _, e := range pairs { x, y := e[0], e[1] p[x] = y p[y] = x } + for x := 0; x < n; x++ { y := p[x] - find := 0 for i := 0; i < d[x][y]; i++ { u := preferences[x][i] - if d[u][x] < d[u][p[u]] { - find = 1 + v := p[u] + if d[u][x] < d[u][v] { + ans++ break } } - ans += find } + return } ``` +#### TypeScript + +```ts +function unhappyFriends(n: number, preferences: number[][], pairs: number[][]): number { + const d: number[][] = Array.from({ length: n }, () => Array(n).fill(0)); + for (let i = 0; i < n; ++i) { + for (let j = 0; j < n - 1; ++j) { + d[i][preferences[i][j]] = j; + } + } + const p: number[] = Array(n).fill(0); + for (const [x, y] of pairs) { + p[x] = y; + p[y] = x; + } + let ans = 0; + for (let x = 0; x < n; ++x) { + const y = p[x]; + for (let i = 0; i < d[x][y]; ++i) { + const u = preferences[x][i]; + const v = p[u]; + if (d[u][x] < d[u][v]) { + ++ans; + break; + } + } + } + return ans; +} +``` + diff --git a/solution/1500-1599/1583.Count Unhappy Friends/README_EN.md b/solution/1500-1599/1583.Count Unhappy Friends/README_EN.md index d109b03c35181..7e9f73b901c05 100644 --- a/solution/1500-1599/1583.Count Unhappy Friends/README_EN.md +++ b/solution/1500-1599/1583.Count Unhappy Friends/README_EN.md @@ -89,7 +89,15 @@ Friends 0 and 2 are happy. -### Solution 1 +### Solution 1: Enumeration + +We use an array $\textit{d}$ to record the closeness between each pair of friends, where $\textit{d}[i][j]$ represents the closeness of friend $i$ to friend $j$ (the smaller the value, the closer they are). Additionally, we use an array $\textit{p}$ to record the paired friend for each friend. + +We enumerate each friend $x$. For $x$'s paired friend $y$, we find the closeness $\textit{d}[x][y]$ of $x$ to $y$. Then, we enumerate other friends $u$ who are closer than $\textit{d}[x][y]$. If there exists a friend $u$ such that the closeness $\textit{d}[u][x]$ of $u$ to $x$ is higher than $\textit{d}[u][y]$, then $x$ is an unhappy friend, and we increment the result by one. + +After the enumeration, we obtain the number of unhappy friends. + +The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Here, $n$ is the number of friends. @@ -100,7 +108,7 @@ class Solution: def unhappyFriends( self, n: int, preferences: List[List[int]], pairs: List[List[int]] ) -> int: - d = [{p: i for i, p in enumerate(v)} for v in preferences] + d = [{x: j for j, x in enumerate(p)} for p in preferences] p = {} for x, y in pairs: p[x] = y @@ -108,7 +116,12 @@ class Solution: ans = 0 for x in range(n): y = p[x] - ans += any(d[u][x] < d[u][p[u]] for u in preferences[x][: d[x][y]]) + for i in range(d[x][y]): + u = preferences[x][i] + v = p[u] + if d[u][x] < d[u][v]: + ans += 1 + break return ans ``` @@ -132,15 +145,14 @@ class Solution { int ans = 0; for (int x = 0; x < n; ++x) { int y = p[x]; - int find = 0; for (int i = 0; i < d[x][y]; ++i) { int u = preferences[x][i]; - if (d[u][x] < d[u][p[u]]) { - find = 1; + int v = p[u]; + if (d[u][x] < d[u][v]) { + ++ans; break; } } - ans += find; } return ans; } @@ -153,13 +165,13 @@ class Solution { class Solution { public: int unhappyFriends(int n, vector>& preferences, vector>& pairs) { - int d[n][n]; - int p[n]; + vector> d(n, vector(n)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n - 1; ++j) { d[i][preferences[i][j]] = j; } } + vector p(n, 0); for (auto& e : pairs) { int x = e[0], y = e[1]; p[x] = y; @@ -168,15 +180,14 @@ public: int ans = 0; for (int x = 0; x < n; ++x) { int y = p[x]; - int find = 0; for (int i = 0; i < d[x][y]; ++i) { int u = preferences[x][i]; - if (d[u][x] < d[u][p[u]]) { - find = 1; + int v = p[u]; + if (d[u][x] < d[u][v]) { + ++ans; break; } } - ans += find; } return ans; } @@ -188,34 +199,70 @@ public: ```go func unhappyFriends(n int, preferences [][]int, pairs [][]int) (ans int) { d := make([][]int, n) - p := make([]int, n) for i := range d { d[i] = make([]int, n) + } + + for i := 0; i < n; i++ { for j := 0; j < n-1; j++ { d[i][preferences[i][j]] = j } } + + p := make([]int, n) for _, e := range pairs { x, y := e[0], e[1] p[x] = y p[y] = x } + for x := 0; x < n; x++ { y := p[x] - find := 0 for i := 0; i < d[x][y]; i++ { u := preferences[x][i] - if d[u][x] < d[u][p[u]] { - find = 1 + v := p[u] + if d[u][x] < d[u][v] { + ans++ break } } - ans += find } + return } ``` +#### TypeScript + +```ts +function unhappyFriends(n: number, preferences: number[][], pairs: number[][]): number { + const d: number[][] = Array.from({ length: n }, () => Array(n).fill(0)); + for (let i = 0; i < n; ++i) { + for (let j = 0; j < n - 1; ++j) { + d[i][preferences[i][j]] = j; + } + } + const p: number[] = Array(n).fill(0); + for (const [x, y] of pairs) { + p[x] = y; + p[y] = x; + } + let ans = 0; + for (let x = 0; x < n; ++x) { + const y = p[x]; + for (let i = 0; i < d[x][y]; ++i) { + const u = preferences[x][i]; + const v = p[u]; + if (d[u][x] < d[u][v]) { + ++ans; + break; + } + } + } + return ans; +} +``` + diff --git a/solution/1500-1599/1583.Count Unhappy Friends/Solution.cpp b/solution/1500-1599/1583.Count Unhappy Friends/Solution.cpp index 43e244a03c13b..6721226583a9f 100644 --- a/solution/1500-1599/1583.Count Unhappy Friends/Solution.cpp +++ b/solution/1500-1599/1583.Count Unhappy Friends/Solution.cpp @@ -1,13 +1,13 @@ class Solution { public: int unhappyFriends(int n, vector>& preferences, vector>& pairs) { - int d[n][n]; - int p[n]; + vector> d(n, vector(n)); for (int i = 0; i < n; ++i) { for (int j = 0; j < n - 1; ++j) { d[i][preferences[i][j]] = j; } } + vector p(n, 0); for (auto& e : pairs) { int x = e[0], y = e[1]; p[x] = y; @@ -16,16 +16,15 @@ class Solution { int ans = 0; for (int x = 0; x < n; ++x) { int y = p[x]; - int find = 0; for (int i = 0; i < d[x][y]; ++i) { int u = preferences[x][i]; - if (d[u][x] < d[u][p[u]]) { - find = 1; + int v = p[u]; + if (d[u][x] < d[u][v]) { + ++ans; break; } } - ans += find; } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/1500-1599/1583.Count Unhappy Friends/Solution.go b/solution/1500-1599/1583.Count Unhappy Friends/Solution.go index 3bd3b7569b6da..5378fd67c295f 100644 --- a/solution/1500-1599/1583.Count Unhappy Friends/Solution.go +++ b/solution/1500-1599/1583.Count Unhappy Friends/Solution.go @@ -1,28 +1,33 @@ func unhappyFriends(n int, preferences [][]int, pairs [][]int) (ans int) { d := make([][]int, n) - p := make([]int, n) for i := range d { d[i] = make([]int, n) + } + + for i := 0; i < n; i++ { for j := 0; j < n-1; j++ { d[i][preferences[i][j]] = j } } + + p := make([]int, n) for _, e := range pairs { x, y := e[0], e[1] p[x] = y p[y] = x } + for x := 0; x < n; x++ { y := p[x] - find := 0 for i := 0; i < d[x][y]; i++ { u := preferences[x][i] - if d[u][x] < d[u][p[u]] { - find = 1 + v := p[u] + if d[u][x] < d[u][v] { + ans++ break } } - ans += find } + return -} \ No newline at end of file +} diff --git a/solution/1500-1599/1583.Count Unhappy Friends/Solution.java b/solution/1500-1599/1583.Count Unhappy Friends/Solution.java index ce59dbd1b51e1..c29a704d948ee 100644 --- a/solution/1500-1599/1583.Count Unhappy Friends/Solution.java +++ b/solution/1500-1599/1583.Count Unhappy Friends/Solution.java @@ -15,16 +15,15 @@ public int unhappyFriends(int n, int[][] preferences, int[][] pairs) { int ans = 0; for (int x = 0; x < n; ++x) { int y = p[x]; - int find = 0; for (int i = 0; i < d[x][y]; ++i) { int u = preferences[x][i]; - if (d[u][x] < d[u][p[u]]) { - find = 1; + int v = p[u]; + if (d[u][x] < d[u][v]) { + ++ans; break; } } - ans += find; } return ans; } -} \ No newline at end of file +} diff --git a/solution/1500-1599/1583.Count Unhappy Friends/Solution.py b/solution/1500-1599/1583.Count Unhappy Friends/Solution.py index f12881a6547c9..b95751d86b8b4 100644 --- a/solution/1500-1599/1583.Count Unhappy Friends/Solution.py +++ b/solution/1500-1599/1583.Count Unhappy Friends/Solution.py @@ -2,7 +2,7 @@ class Solution: def unhappyFriends( self, n: int, preferences: List[List[int]], pairs: List[List[int]] ) -> int: - d = [{p: i for i, p in enumerate(v)} for v in preferences] + d = [{x: j for j, x in enumerate(p)} for p in preferences] p = {} for x, y in pairs: p[x] = y @@ -10,5 +10,10 @@ def unhappyFriends( ans = 0 for x in range(n): y = p[x] - ans += any(d[u][x] < d[u][p[u]] for u in preferences[x][: d[x][y]]) + for i in range(d[x][y]): + u = preferences[x][i] + v = p[u] + if d[u][x] < d[u][v]: + ans += 1 + break return ans diff --git a/solution/1500-1599/1583.Count Unhappy Friends/Solution.ts b/solution/1500-1599/1583.Count Unhappy Friends/Solution.ts new file mode 100644 index 0000000000000..3780a05daba04 --- /dev/null +++ b/solution/1500-1599/1583.Count Unhappy Friends/Solution.ts @@ -0,0 +1,26 @@ +function unhappyFriends(n: number, preferences: number[][], pairs: number[][]): number { + const d: number[][] = Array.from({ length: n }, () => Array(n).fill(0)); + for (let i = 0; i < n; ++i) { + for (let j = 0; j < n - 1; ++j) { + d[i][preferences[i][j]] = j; + } + } + const p: number[] = Array(n).fill(0); + for (const [x, y] of pairs) { + p[x] = y; + p[y] = x; + } + let ans = 0; + for (let x = 0; x < n; ++x) { + const y = p[x]; + for (let i = 0; i < d[x][y]; ++i) { + const u = preferences[x][i]; + const v = p[u]; + if (d[u][x] < d[u][v]) { + ++ans; + break; + } + } + } + return ans; +}