Skip to content

Commit a74a9bf

Browse files
add 1583
1 parent 3ef6e92 commit a74a9bf

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+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class _1583 {
7+
public static class Solution1 {
8+
public int unhappyFriends(int n, int[][] preferences, int[][] pairs) {
9+
int unhappyFriends = 0;
10+
Map<Integer, Integer> assignedPair = new HashMap<>();
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+
return unhappyFriends;
24+
}
25+
26+
private boolean isUnHappy(int self, int assignedFriend, int[][] preferences, Map<Integer, Integer> assignedPairs) {
27+
int[] preference = preferences[self];
28+
int assignedFriendPreferenceIndex = findIndex(preference, assignedFriend);
29+
for (int i = 0; i <= assignedFriendPreferenceIndex; i++) {
30+
int preferredFriend = preference[i];
31+
int preferredFriendAssignedFriend = assignedPairs.get(preferredFriend);
32+
if (preferredFriendAssignedFriend == self) {
33+
return false;
34+
}
35+
int candidateAssignedFriendIndex = findIndex(preferences[preferredFriend], preferredFriendAssignedFriend);
36+
if (isPreferred(self, preferences[preferredFriend], candidateAssignedFriendIndex)) {
37+
return true;
38+
}
39+
}
40+
return false;
41+
}
42+
43+
private boolean isPreferred(int self, int[] preference, int boundary) {
44+
for (int i = 0; i <= boundary; i++) {
45+
if (self == preference[i]) {
46+
return true;
47+
}
48+
}
49+
return false;
50+
}
51+
52+
private int findIndex(int[] preference, int assignedFriend) {
53+
for (int i = 0; i < preference.length; i++) {
54+
if (preference[i] == assignedFriend) {
55+
return i;
56+
}
57+
}
58+
return 0;
59+
}
60+
}
61+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1583;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _1583Test {
10+
private static _1583.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1583.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(2, solution1.unhappyFriends(4, new int[][]{
20+
{1, 2, 3},
21+
{3, 2, 0},
22+
{3, 1, 0},
23+
{1, 2, 0}
24+
},
25+
new int[][]{
26+
{0, 1},
27+
{2, 3}
28+
}));
29+
}
30+
31+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy