Skip to content

Commit c4a9118

Browse files
committed
feat: add solutions to lc problem: No.1583
No.1583.Count Unhappy Friends
1 parent b9e714e commit c4a9118

File tree

6 files changed

+329
-40
lines changed

6 files changed

+329
-40
lines changed

solution/1500-1599/1583.Count Unhappy Friends/README.md

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,143 @@
7676

7777
<!-- 这里可写通用的实现逻辑 -->
7878

79+
**方法一:数组 + 枚举**
80+
81+
我们用数组 $d$ 记录每个朋友与其它朋友的亲近程度,其中 $d[i][j]$ 表示朋友 $i$ 对 $j$ 的亲近程度(值越小,越亲近),另外,用数组 $p$ 记录每个朋友的配对朋友。
82+
83+
我们枚举每个朋友 $x$,对于 $x$ 的配对朋友 $y$,我们找到 $x$ 对 $y$ 的亲近程度 $d[x][y]$,然后枚举比 $d[x][y]$ 更亲近的其它朋友 $u$,如果存在 $u$ 对 $x$ 的亲近程度 $d[u][x]$ 比 $d[u][y]$ 更高,那么 $x$ 就是不开心的朋友,将结果加一即可。
84+
85+
枚举结束后,即可得到不开心的朋友的数目。
86+
87+
时间复杂度 $O(n^2)$,空间复杂度 $O(n^2)$。其中 $n$ 为朋友的数目。
88+
7989
<!-- tabs:start -->
8090

8191
### **Python3**
8292

8393
<!-- 这里可写当前语言的特殊实现逻辑 -->
8494

8595
```python
86-
96+
class Solution:
97+
def unhappyFriends(
98+
self, n: int, preferences: List[List[int]], pairs: List[List[int]]
99+
) -> int:
100+
d = [{p: i for i, p in enumerate(v)} for v in preferences]
101+
p = {}
102+
for x, y in pairs:
103+
p[x] = y
104+
p[y] = x
105+
ans = 0
106+
for x in range(n):
107+
y = p[x]
108+
ans += any(d[u][x] < d[u][p[u]] for u in preferences[x][: d[x][y]])
109+
return ans
87110
```
88111

89112
### **Java**
90113

91114
<!-- 这里可写当前语言的特殊实现逻辑 -->
92115

93116
```java
117+
class Solution {
118+
public int unhappyFriends(int n, int[][] preferences, int[][] pairs) {
119+
int[][] d = new int[n][n];
120+
for (int i = 0; i < n; ++i) {
121+
for (int j = 0; j < n - 1; ++j) {
122+
d[i][preferences[i][j]] = j;
123+
}
124+
}
125+
int[] p = new int[n];
126+
for (var e : pairs) {
127+
int x = e[0], y = e[1];
128+
p[x] = y;
129+
p[y] = x;
130+
}
131+
int ans = 0;
132+
for (int x = 0; x < n; ++x) {
133+
int y = p[x];
134+
int find = 0;
135+
for (int i = 0; i < d[x][y]; ++i) {
136+
int u = preferences[x][i];
137+
if (d[u][x] < d[u][p[u]]) {
138+
find = 1;
139+
break;
140+
}
141+
}
142+
ans += find;
143+
}
144+
return ans;
145+
}
146+
}
147+
```
148+
149+
### **C++**
150+
151+
```cpp
152+
class Solution {
153+
public:
154+
int unhappyFriends(int n, vector<vector<int>>& preferences, vector<vector<int>>& pairs) {
155+
int d[n][n];
156+
int p[n];
157+
for (int i = 0; i < n; ++i) {
158+
for (int j = 0; j < n - 1; ++j) {
159+
d[i][preferences[i][j]] = j;
160+
}
161+
}
162+
for (auto& e : pairs) {
163+
int x = e[0], y = e[1];
164+
p[x] = y;
165+
p[y] = x;
166+
}
167+
int ans = 0;
168+
for (int x = 0; x < n; ++x) {
169+
int y = p[x];
170+
int find = 0;
171+
for (int i = 0; i < d[x][y]; ++i) {
172+
int u = preferences[x][i];
173+
if (d[u][x] < d[u][p[u]]) {
174+
find = 1;
175+
break;
176+
}
177+
}
178+
ans += find;
179+
}
180+
return ans;
181+
}
182+
};
183+
```
94184
185+
### **Go**
186+
187+
```go
188+
func unhappyFriends(n int, preferences [][]int, pairs [][]int) (ans int) {
189+
d := make([][]int, n)
190+
p := make([]int, n)
191+
for i := range d {
192+
d[i] = make([]int, n)
193+
for j := 0; j < n-1; j++ {
194+
d[i][preferences[i][j]] = j
195+
}
196+
}
197+
for _, e := range pairs {
198+
x, y := e[0], e[1]
199+
p[x] = y
200+
p[y] = x
201+
}
202+
for x := 0; x < n; x++ {
203+
y := p[x]
204+
find := 0
205+
for i := 0; i < d[x][y]; i++ {
206+
u := preferences[x][i]
207+
if d[u][x] < d[u][p[u]] {
208+
find = 1
209+
break
210+
}
211+
}
212+
ans += find
213+
}
214+
return
215+
}
95216
```
96217

97218
### **...**

solution/1500-1599/1583.Count Unhappy Friends/README_EN.md

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,124 @@ Friends 0 and 2 are happy.
7575
### **Python3**
7676

7777
```python
78-
78+
class Solution:
79+
def unhappyFriends(
80+
self, n: int, preferences: List[List[int]], pairs: List[List[int]]
81+
) -> int:
82+
d = [{p: i for i, p in enumerate(v)} for v in preferences]
83+
p = {}
84+
for x, y in pairs:
85+
p[x] = y
86+
p[y] = x
87+
ans = 0
88+
for x in range(n):
89+
y = p[x]
90+
ans += any(d[u][x] < d[u][p[u]] for u in preferences[x][: d[x][y]])
91+
return ans
7992
```
8093

8194
### **Java**
8295

8396
```java
97+
class Solution {
98+
public int unhappyFriends(int n, int[][] preferences, int[][] pairs) {
99+
int[][] d = new int[n][n];
100+
for (int i = 0; i < n; ++i) {
101+
for (int j = 0; j < n - 1; ++j) {
102+
d[i][preferences[i][j]] = j;
103+
}
104+
}
105+
int[] p = new int[n];
106+
for (var e : pairs) {
107+
int x = e[0], y = e[1];
108+
p[x] = y;
109+
p[y] = x;
110+
}
111+
int ans = 0;
112+
for (int x = 0; x < n; ++x) {
113+
int y = p[x];
114+
int find = 0;
115+
for (int i = 0; i < d[x][y]; ++i) {
116+
int u = preferences[x][i];
117+
if (d[u][x] < d[u][p[u]]) {
118+
find = 1;
119+
break;
120+
}
121+
}
122+
ans += find;
123+
}
124+
return ans;
125+
}
126+
}
127+
```
128+
129+
### **C++**
130+
131+
```cpp
132+
class Solution {
133+
public:
134+
int unhappyFriends(int n, vector<vector<int>>& preferences, vector<vector<int>>& pairs) {
135+
int d[n][n];
136+
int p[n];
137+
for (int i = 0; i < n; ++i) {
138+
for (int j = 0; j < n - 1; ++j) {
139+
d[i][preferences[i][j]] = j;
140+
}
141+
}
142+
for (auto& e : pairs) {
143+
int x = e[0], y = e[1];
144+
p[x] = y;
145+
p[y] = x;
146+
}
147+
int ans = 0;
148+
for (int x = 0; x < n; ++x) {
149+
int y = p[x];
150+
int find = 0;
151+
for (int i = 0; i < d[x][y]; ++i) {
152+
int u = preferences[x][i];
153+
if (d[u][x] < d[u][p[u]]) {
154+
find = 1;
155+
break;
156+
}
157+
}
158+
ans += find;
159+
}
160+
return ans;
161+
}
162+
};
163+
```
84164
165+
### **Go**
166+
167+
```go
168+
func unhappyFriends(n int, preferences [][]int, pairs [][]int) (ans int) {
169+
d := make([][]int, n)
170+
p := make([]int, n)
171+
for i := range d {
172+
d[i] = make([]int, n)
173+
for j := 0; j < n-1; j++ {
174+
d[i][preferences[i][j]] = j
175+
}
176+
}
177+
for _, e := range pairs {
178+
x, y := e[0], e[1]
179+
p[x] = y
180+
p[y] = x
181+
}
182+
for x := 0; x < n; x++ {
183+
y := p[x]
184+
find := 0
185+
for i := 0; i < d[x][y]; i++ {
186+
u := preferences[x][i]
187+
if d[u][x] < d[u][p[u]] {
188+
find = 1
189+
break
190+
}
191+
}
192+
ans += find
193+
}
194+
return
195+
}
85196
```
86197

87198
### **...**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public:
3+
int unhappyFriends(int n, vector<vector<int>>& preferences, vector<vector<int>>& pairs) {
4+
int d[n][n];
5+
int p[n];
6+
for (int i = 0; i < n; ++i) {
7+
for (int j = 0; j < n - 1; ++j) {
8+
d[i][preferences[i][j]] = j;
9+
}
10+
}
11+
for (auto& e : pairs) {
12+
int x = e[0], y = e[1];
13+
p[x] = y;
14+
p[y] = x;
15+
}
16+
int ans = 0;
17+
for (int x = 0; x < n; ++x) {
18+
int y = p[x];
19+
int find = 0;
20+
for (int i = 0; i < d[x][y]; ++i) {
21+
int u = preferences[x][i];
22+
if (d[u][x] < d[u][p[u]]) {
23+
find = 1;
24+
break;
25+
}
26+
}
27+
ans += find;
28+
}
29+
return ans;
30+
}
31+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
func unhappyFriends(n int, preferences [][]int, pairs [][]int) (ans int) {
2+
d := make([][]int, n)
3+
p := make([]int, n)
4+
for i := range d {
5+
d[i] = make([]int, n)
6+
for j := 0; j < n-1; j++ {
7+
d[i][preferences[i][j]] = j
8+
}
9+
}
10+
for _, e := range pairs {
11+
x, y := e[0], e[1]
12+
p[x] = y
13+
p[y] = x
14+
}
15+
for x := 0; x < n; x++ {
16+
y := p[x]
17+
find := 0
18+
for i := 0; i < d[x][y]; i++ {
19+
u := preferences[x][i]
20+
if d[u][x] < d[u][p[u]] {
21+
find = 1
22+
break
23+
}
24+
}
25+
ans += find
26+
}
27+
return
28+
}

0 commit comments

Comments
 (0)