Movatterモバイル変換


[0]ホーム

URL:


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

Commita74a9bf

Browse files
add 1583
1 parent3ef6e92 commita74a9bf

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1583|[Count Unhappy Friends](https://leetcode.com/problems/count-unhappy-friends/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1583.java)||Array|Medium|
1112
|1582|[Special Positions in a Binary Matrix](https://leetcode.com/problems/special-positions-in-a-binary-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1582.java)||Array|Easy|
1213
|1576|[Replace All ?'s to Avoid Consecutive Repeating Characters](https://leetcode.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1576.java)|[:tv:](https://youtu.be/SJBDLYqrIsM)|String|Easy|
1314
|1574|[Shortest Subarray to be Removed to Make Array Sorted](https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1574.java)||Array, Binary Search|Medium|
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.HashMap;
4+
importjava.util.Map;
5+
6+
publicclass_1583 {
7+
publicstaticclassSolution1 {
8+
publicintunhappyFriends(intn,int[][]preferences,int[][]pairs) {
9+
intunhappyFriends =0;
10+
Map<Integer,Integer>assignedPair =newHashMap<>();
11+
for (int[]pair :pairs) {
12+
assignedPair.put(pair[0],pair[1]);
13+
assignedPair.put(pair[1],pair[0]);
14+
}
15+
for (int[]pair :pairs) {
16+
if (isUnHappy(pair[1],pair[0],preferences,assignedPair)) {
17+
unhappyFriends++;
18+
}
19+
if (isUnHappy(pair[0],pair[1],preferences,assignedPair)) {
20+
unhappyFriends++;
21+
}
22+
}
23+
returnunhappyFriends;
24+
}
25+
26+
privatebooleanisUnHappy(intself,intassignedFriend,int[][]preferences,Map<Integer,Integer>assignedPairs) {
27+
int[]preference =preferences[self];
28+
intassignedFriendPreferenceIndex =findIndex(preference,assignedFriend);
29+
for (inti =0;i <=assignedFriendPreferenceIndex;i++) {
30+
intpreferredFriend =preference[i];
31+
intpreferredFriendAssignedFriend =assignedPairs.get(preferredFriend);
32+
if (preferredFriendAssignedFriend ==self) {
33+
returnfalse;
34+
}
35+
intcandidateAssignedFriendIndex =findIndex(preferences[preferredFriend],preferredFriendAssignedFriend);
36+
if (isPreferred(self,preferences[preferredFriend],candidateAssignedFriendIndex)) {
37+
returntrue;
38+
}
39+
}
40+
returnfalse;
41+
}
42+
43+
privatebooleanisPreferred(intself,int[]preference,intboundary) {
44+
for (inti =0;i <=boundary;i++) {
45+
if (self ==preference[i]) {
46+
returntrue;
47+
}
48+
}
49+
returnfalse;
50+
}
51+
52+
privateintfindIndex(int[]preference,intassignedFriend) {
53+
for (inti =0;i <preference.length;i++) {
54+
if (preference[i] ==assignedFriend) {
55+
returni;
56+
}
57+
}
58+
return0;
59+
}
60+
}
61+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1583;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticjunit.framework.TestCase.assertEquals;
8+
9+
publicclass_1583Test {
10+
privatestatic_1583.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1583.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(2,solution1.unhappyFriends(4,newint[][]{
20+
{1,2,3},
21+
{3,2,0},
22+
{3,1,0},
23+
{1,2,0}
24+
},
25+
newint[][]{
26+
{0,1},
27+
{2,3}
28+
}));
29+
}
30+
31+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp