Skip to content

feat: add solutions to lc problem: No.1583 #3592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 59 additions & 20 deletions solution/1500-1599/1583.Count Unhappy Friends/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ tags:

<!-- solution:start -->

### 方法一:数组 + 枚举
### 方法一:枚举

我们用数组 $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$ 就是不开心的朋友,将结果加一即可。

枚举结束后,即可得到不开心的朋友的数目。

Expand All @@ -110,15 +110,20 @@ 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
p[y] = x
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
```

Expand All @@ -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;
}
Expand All @@ -163,13 +167,13 @@ class Solution {
class Solution {
public:
int unhappyFriends(int n, vector<vector<int>>& preferences, vector<vector<int>>& pairs) {
int d[n][n];
int p[n];
vector<vector<int>> d(n, vector<int>(n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n - 1; ++j) {
d[i][preferences[i][j]] = j;
}
}
vector<int> p(n, 0);
for (auto& e : pairs) {
int x = e[0], y = e[1];
p[x] = y;
Expand All @@ -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;
}
Expand All @@ -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;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
83 changes: 65 additions & 18 deletions solution/1500-1599/1583.Count Unhappy Friends/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,15 @@ Friends 0 and 2 are happy.

<!-- solution:start -->

### 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.

<!-- tabs:start -->

Expand All @@ -100,15 +108,20 @@ 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
p[y] = x
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
```

Expand All @@ -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;
}
Expand All @@ -153,13 +165,13 @@ class Solution {
class Solution {
public:
int unhappyFriends(int n, vector<vector<int>>& preferences, vector<vector<int>>& pairs) {
int d[n][n];
int p[n];
vector<vector<int>> d(n, vector<int>(n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n - 1; ++j) {
d[i][preferences[i][j]] = j;
}
}
vector<int> p(n, 0);
for (auto& e : pairs) {
int x = e[0], y = e[1];
p[x] = y;
Expand All @@ -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;
}
Expand All @@ -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;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
13 changes: 6 additions & 7 deletions solution/1500-1599/1583.Count Unhappy Friends/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class Solution {
public:
int unhappyFriends(int n, vector<vector<int>>& preferences, vector<vector<int>>& pairs) {
int d[n][n];
int p[n];
vector<vector<int>> d(n, vector<int>(n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n - 1; ++j) {
d[i][preferences[i][j]] = j;
}
}
vector<int> p(n, 0);
for (auto& e : pairs) {
int x = e[0], y = e[1];
p[x] = y;
Expand All @@ -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;
}
};
};
Loading
Loading