Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d693654

Browse files
feat: add solutions to lc problem: No.1583 (doocs#3592)
No.1583.Count Unhappy Friends
1 parent 873947b commit d693654

File tree

7 files changed

+178
-58
lines changed

7 files changed

+178
-58
lines changed

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

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ tags:
9191

9292
<!-- solution:start -->
9393

94-
### 方法一:数组 + 枚举
94+
### 方法一:枚举
9595

96-
我们用数组 $d$ 记录每个朋友与其它朋友的亲近程度,其中 $d[i][j]$ 表示朋友 $i$ 对 $j$ 的亲近程度(值越小,越亲近),另外,用数组 $p$ 记录每个朋友的配对朋友。
96+
我们用数组 $\textit{d}$ 记录每个朋友与其它朋友的亲近程度,其中 $\textit{d}[i][j]$ 表示朋友 $i$ 对 $j$ 的亲近程度(值越小,越亲近),另外,用数组 $\textit{p}$ 记录每个朋友的配对朋友。
9797

98-
我们枚举每个朋友 $x,ドル对于 $x$ 的配对朋友 $y,ドル我们找到 $x$ 对 $y$ 的亲近程度 $d[x][y],ドル然后枚举比 $d[x][y]$ 更亲近的其它朋友 $u,ドル如果存在 $u$ 对 $x$ 的亲近程度 $d[u][x]$ 比 $d[u][y]$ 更高,那么 $x$ 就是不开心的朋友,将结果加一即可。
98+
我们枚举每个朋友 $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$ 就是不开心的朋友,将结果加一即可。
9999

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

@@ -110,15 +110,20 @@ class Solution:
110110
def unhappyFriends(
111111
self, n: int, preferences: List[List[int]], pairs: List[List[int]]
112112
) -> int:
113-
d = [{p: i for i, p in enumerate(v)} for v in preferences]
113+
d = [{x: j for j, x in enumerate(p)} for p in preferences]
114114
p = {}
115115
for x, y in pairs:
116116
p[x] = y
117117
p[y] = x
118118
ans = 0
119119
for x in range(n):
120120
y = p[x]
121-
ans += any(d[u][x] < d[u][p[u]] for u in preferences[x][: d[x][y]])
121+
for i in range(d[x][y]):
122+
u = preferences[x][i]
123+
v = p[u]
124+
if d[u][x] < d[u][v]:
125+
ans += 1
126+
break
122127
return ans
123128
```
124129

@@ -142,15 +147,14 @@ class Solution {
142147
int ans = 0;
143148
for (int x = 0; x < n; ++x) {
144149
int y = p[x];
145-
int find = 0;
146150
for (int i = 0; i < d[x][y]; ++i) {
147151
int u = preferences[x][i];
148-
if (d[u][x] < d[u][p[u]]) {
149-
find = 1;
152+
int v = p[u];
153+
if (d[u][x] < d[u][v]) {
154+
++ans;
150155
break;
151156
}
152157
}
153-
ans += find;
154158
}
155159
return ans;
156160
}
@@ -163,13 +167,13 @@ class Solution {
163167
class Solution {
164168
public:
165169
int unhappyFriends(int n, vector<vector<int>>& preferences, vector<vector<int>>& pairs) {
166-
int d[n][n];
167-
int p[n];
170+
vector<vector<int>> d(n, vector<int>(n));
168171
for (int i = 0; i < n; ++i) {
169172
for (int j = 0; j < n - 1; ++j) {
170173
d[i][preferences[i][j]] = j;
171174
}
172175
}
176+
vector<int> p(n, 0);
173177
for (auto& e : pairs) {
174178
int x = e[0], y = e[1];
175179
p[x] = y;
@@ -178,15 +182,14 @@ public:
178182
int ans = 0;
179183
for (int x = 0; x < n; ++x) {
180184
int y = p[x];
181-
int find = 0;
182185
for (int i = 0; i < d[x][y]; ++i) {
183186
int u = preferences[x][i];
184-
if (d[u][x] < d[u][p[u]]) {
185-
find = 1;
187+
int v = p[u];
188+
if (d[u][x] < d[u][v]) {
189+
++ans;
186190
break;
187191
}
188192
}
189-
ans += find;
190193
}
191194
return ans;
192195
}
@@ -198,34 +201,70 @@ public:
198201
```go
199202
func unhappyFriends(n int, preferences [][]int, pairs [][]int) (ans int) {
200203
d := make([][]int, n)
201-
p := make([]int, n)
202204
for i := range d {
203205
d[i] = make([]int, n)
206+
}
207+
208+
for i := 0; i < n; i++ {
204209
for j := 0; j < n-1; j++ {
205210
d[i][preferences[i][j]] = j
206211
}
207212
}
213+
214+
p := make([]int, n)
208215
for _, e := range pairs {
209216
x, y := e[0], e[1]
210217
p[x] = y
211218
p[y] = x
212219
}
220+
213221
for x := 0; x < n; x++ {
214222
y := p[x]
215-
find := 0
216223
for i := 0; i < d[x][y]; i++ {
217224
u := preferences[x][i]
218-
if d[u][x] < d[u][p[u]] {
219-
find = 1
225+
v := p[u]
226+
if d[u][x] < d[u][v] {
227+
ans++
220228
break
221229
}
222230
}
223-
ans += find
224231
}
232+
225233
return
226234
}
227235
```
228236

237+
#### TypeScript
238+
239+
```ts
240+
function unhappyFriends(n: number, preferences: number[][], pairs: number[][]): number {
241+
const d: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
242+
for (let i = 0; i < n; ++i) {
243+
for (let j = 0; j < n - 1; ++j) {
244+
d[i][preferences[i][j]] = j;
245+
}
246+
}
247+
const p: number[] = Array(n).fill(0);
248+
for (const [x, y] of pairs) {
249+
p[x] = y;
250+
p[y] = x;
251+
}
252+
let ans = 0;
253+
for (let x = 0; x < n; ++x) {
254+
const y = p[x];
255+
for (let i = 0; i < d[x][y]; ++i) {
256+
const u = preferences[x][i];
257+
const v = p[u];
258+
if (d[u][x] < d[u][v]) {
259+
++ans;
260+
break;
261+
}
262+
}
263+
}
264+
return ans;
265+
}
266+
```
267+
229268
<!-- tabs:end -->
230269

231270
<!-- solution:end -->

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

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,15 @@ Friends 0 and 2 are happy.
8989

9090
<!-- solution:start -->
9191

92-
### Solution 1
92+
### Solution 1: Enumeration
93+
94+
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.
95+
96+
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.
97+
98+
After the enumeration, we obtain the number of unhappy friends.
99+
100+
The time complexity is $O(n^2),ドル and the space complexity is $O(n^2)$. Here, $n$ is the number of friends.
93101

94102
<!-- tabs:start -->
95103

@@ -100,15 +108,20 @@ class Solution:
100108
def unhappyFriends(
101109
self, n: int, preferences: List[List[int]], pairs: List[List[int]]
102110
) -> int:
103-
d = [{p: i for i, p in enumerate(v)} for v in preferences]
111+
d = [{x: j for j, x in enumerate(p)} for p in preferences]
104112
p = {}
105113
for x, y in pairs:
106114
p[x] = y
107115
p[y] = x
108116
ans = 0
109117
for x in range(n):
110118
y = p[x]
111-
ans += any(d[u][x] < d[u][p[u]] for u in preferences[x][: d[x][y]])
119+
for i in range(d[x][y]):
120+
u = preferences[x][i]
121+
v = p[u]
122+
if d[u][x] < d[u][v]:
123+
ans += 1
124+
break
112125
return ans
113126
```
114127

@@ -132,15 +145,14 @@ class Solution {
132145
int ans = 0;
133146
for (int x = 0; x < n; ++x) {
134147
int y = p[x];
135-
int find = 0;
136148
for (int i = 0; i < d[x][y]; ++i) {
137149
int u = preferences[x][i];
138-
if (d[u][x] < d[u][p[u]]) {
139-
find = 1;
150+
int v = p[u];
151+
if (d[u][x] < d[u][v]) {
152+
++ans;
140153
break;
141154
}
142155
}
143-
ans += find;
144156
}
145157
return ans;
146158
}
@@ -153,13 +165,13 @@ class Solution {
153165
class Solution {
154166
public:
155167
int unhappyFriends(int n, vector<vector<int>>& preferences, vector<vector<int>>& pairs) {
156-
int d[n][n];
157-
int p[n];
168+
vector<vector<int>> d(n, vector<int>(n));
158169
for (int i = 0; i < n; ++i) {
159170
for (int j = 0; j < n - 1; ++j) {
160171
d[i][preferences[i][j]] = j;
161172
}
162173
}
174+
vector<int> p(n, 0);
163175
for (auto& e : pairs) {
164176
int x = e[0], y = e[1];
165177
p[x] = y;
@@ -168,15 +180,14 @@ public:
168180
int ans = 0;
169181
for (int x = 0; x < n; ++x) {
170182
int y = p[x];
171-
int find = 0;
172183
for (int i = 0; i < d[x][y]; ++i) {
173184
int u = preferences[x][i];
174-
if (d[u][x] < d[u][p[u]]) {
175-
find = 1;
185+
int v = p[u];
186+
if (d[u][x] < d[u][v]) {
187+
++ans;
176188
break;
177189
}
178190
}
179-
ans += find;
180191
}
181192
return ans;
182193
}
@@ -188,34 +199,70 @@ public:
188199
```go
189200
func unhappyFriends(n int, preferences [][]int, pairs [][]int) (ans int) {
190201
d := make([][]int, n)
191-
p := make([]int, n)
192202
for i := range d {
193203
d[i] = make([]int, n)
204+
}
205+
206+
for i := 0; i < n; i++ {
194207
for j := 0; j < n-1; j++ {
195208
d[i][preferences[i][j]] = j
196209
}
197210
}
211+
212+
p := make([]int, n)
198213
for _, e := range pairs {
199214
x, y := e[0], e[1]
200215
p[x] = y
201216
p[y] = x
202217
}
218+
203219
for x := 0; x < n; x++ {
204220
y := p[x]
205-
find := 0
206221
for i := 0; i < d[x][y]; i++ {
207222
u := preferences[x][i]
208-
if d[u][x] < d[u][p[u]] {
209-
find = 1
223+
v := p[u]
224+
if d[u][x] < d[u][v] {
225+
ans++
210226
break
211227
}
212228
}
213-
ans += find
214229
}
230+
215231
return
216232
}
217233
```
218234

235+
#### TypeScript
236+
237+
```ts
238+
function unhappyFriends(n: number, preferences: number[][], pairs: number[][]): number {
239+
const d: number[][] = Array.from({ length: n }, () => Array(n).fill(0));
240+
for (let i = 0; i < n; ++i) {
241+
for (let j = 0; j < n - 1; ++j) {
242+
d[i][preferences[i][j]] = j;
243+
}
244+
}
245+
const p: number[] = Array(n).fill(0);
246+
for (const [x, y] of pairs) {
247+
p[x] = y;
248+
p[y] = x;
249+
}
250+
let ans = 0;
251+
for (let x = 0; x < n; ++x) {
252+
const y = p[x];
253+
for (let i = 0; i < d[x][y]; ++i) {
254+
const u = preferences[x][i];
255+
const v = p[u];
256+
if (d[u][x] < d[u][v]) {
257+
++ans;
258+
break;
259+
}
260+
}
261+
}
262+
return ans;
263+
}
264+
```
265+
219266
<!-- tabs:end -->
220267

221268
<!-- solution:end -->
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
class Solution {
22
public:
33
int unhappyFriends(int n, vector<vector<int>>& preferences, vector<vector<int>>& pairs) {
4-
int d[n][n];
5-
int p[n];
4+
vector<vector<int>> d(n, vector<int>(n));
65
for (int i = 0; i < n; ++i) {
76
for (int j = 0; j < n - 1; ++j) {
87
d[i][preferences[i][j]] = j;
98
}
109
}
10+
vector<int> p(n, 0);
1111
for (auto& e : pairs) {
1212
int x = e[0], y = e[1];
1313
p[x] = y;
@@ -16,16 +16,15 @@ class Solution {
1616
int ans = 0;
1717
for (int x = 0; x < n; ++x) {
1818
int y = p[x];
19-
int find = 0;
2019
for (int i = 0; i < d[x][y]; ++i) {
2120
int u = preferences[x][i];
22-
if (d[u][x] < d[u][p[u]]) {
23-
find = 1;
21+
int v = p[u];
22+
if (d[u][x] < d[u][v]) {
23+
++ans;
2424
break;
2525
}
2626
}
27-
ans += find;
2827
}
2928
return ans;
3029
}
31-
};
30+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /