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 12a43be

Browse files
feat: solve No.1583,1624
1 parent 76b22e0 commit 12a43be

File tree

2 files changed

+217
-0
lines changed

2 files changed

+217
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# 1583. Count Unhappy Friends
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Simulation.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
You are given a list of `preferences` for `n` friends, where `n` is always **even**.
10+
11+
For each person `i`, `preferences[i]` contains a list of friends **sorted** in the **order of preference**. In other words, a friend earlier in the list is more preferred than a friend later in the list. Friends in each list are denoted by integers from `0` to `n-1`.
12+
13+
All the friends are divided into pairs. The pairings are given in a list `pairs`, where `pairs[i] = [xi, yi]` denotes `xi` is paired with `yi` and `yi` is paired with `xi`.
14+
15+
However, this pairing may cause some of the friends to be unhappy. A friend `x` is unhappy if `x` is paired with `y` and there exists a friend `u` who is paired with `v` but:
16+
17+
18+
19+
- `x` prefers `u` over `y`, and
20+
21+
- `u` prefers `x` over `v`.
22+
23+
24+
Return **the number of unhappy friends**.
25+
26+
27+
Example 1:
28+
29+
```
30+
Input: n = 4, preferences = [[1, 2, 3], [3, 2, 0], [3, 1, 0], [1, 2, 0]], pairs = [[0, 1], [2, 3]]
31+
Output: 2
32+
Explanation:
33+
Friend 1 is unhappy because:
34+
- 1 is paired with 0 but prefers 3 over 0, and
35+
- 3 prefers 1 over 2.
36+
Friend 3 is unhappy because:
37+
- 3 is paired with 2 but prefers 1 over 2, and
38+
- 1 prefers 3 over 0.
39+
Friends 0 and 2 are happy.
40+
```
41+
42+
Example 2:
43+
44+
```
45+
Input: n = 2, preferences = [[1], [0]], pairs = [[1, 0]]
46+
Output: 0
47+
Explanation: Both friends 0 and 1 are happy.
48+
```
49+
50+
Example 3:
51+
52+
```
53+
Input: n = 4, preferences = [[1, 3, 2], [2, 3, 0], [1, 3, 0], [0, 2, 1]], pairs = [[1, 3], [0, 2]]
54+
Output: 4
55+
```
56+
57+
58+
**Constraints:**
59+
60+
61+
62+
- `2 <= n <= 500`
63+
64+
- `n` is even.
65+
66+
- `preferences.length == n`
67+
68+
- `preferences[i].length == n - 1`
69+
70+
- `0 <= preferences[i][j] <= n - 1`
71+
72+
- `preferences[i]` does not contain `i`.
73+
74+
- All values in `preferences[i]` are unique.
75+
76+
- `pairs.length == n/2`
77+
78+
- `pairs[i].length == 2`
79+
80+
- `xi != yi`
81+
82+
- `0 <= xi, yi <= n - 1`
83+
84+
- Each person is contained in **exactly one** pair.
85+
86+
87+
88+
## Solution
89+
90+
```javascript
91+
/**
92+
* @param {number} n
93+
* @param {number[][]} preferences
94+
* @param {number[][]} pairs
95+
* @return {number}
96+
*/
97+
var unhappyFriends = function(n, preferences, pairs) {
98+
var preferenceMap = Array(n).fill(0).map(() => Array(n));
99+
for (var i = 0; i < preferences.length; i++) {
100+
for (var j = 0; j < preferences[i].length; j++) {
101+
preferenceMap[i][preferences[i][j]] = j;
102+
}
103+
}
104+
var pairMap = Array(n);
105+
for (var m = 0; m < pairs.length; m++) {
106+
pairMap[pairs[m][0]] = pairs[m][1];
107+
pairMap[pairs[m][1]] = pairs[m][0];
108+
}
109+
var res = 0;
110+
for (var k = 0; k < n; k++) {
111+
judge(preferenceMap, pairMap, k, n) && res++;
112+
}
113+
return res;
114+
};
115+
116+
var judge = function(preferenceMap, pairMap, i, n) {
117+
for (var k = 0; k < n; k++) {
118+
if (k === i || k === pairMap[i]) continue;
119+
if (preferenceMap[i][pairMap[i]] > preferenceMap[i][k]
120+
&& preferenceMap[k][pairMap[k]] > preferenceMap[k][i]) {
121+
return true;
122+
}
123+
}
124+
return false;
125+
};
126+
```
127+
128+
**Explain:**
129+
130+
nope.
131+
132+
**Complexity:**
133+
134+
* Time complexity : O(n * n).
135+
* Space complexity : O(n * n).
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# 1624. Largest Substring Between Two Equal Characters
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Hash Table, String.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
Given a string `s`, return **the length of the longest substring between two equal characters, excluding the two characters.** If there is no such substring return `-1`.
10+
11+
A **substring** is a contiguous sequence of characters within a string.
12+
13+
14+
Example 1:
15+
16+
```
17+
Input: s = "aa"
18+
Output: 0
19+
Explanation: The optimal substring here is an empty substring between the two 'a's.
20+
```
21+
22+
Example 2:
23+
24+
```
25+
Input: s = "abca"
26+
Output: 2
27+
Explanation: The optimal substring here is "bc".
28+
```
29+
30+
Example 3:
31+
32+
```
33+
Input: s = "cbzxy"
34+
Output: -1
35+
Explanation: There are no characters that appear twice in s.
36+
```
37+
38+
39+
**Constraints:**
40+
41+
42+
43+
- `1 <= s.length <= 300`
44+
45+
- `s` contains only lowercase English letters.
46+
47+
48+
49+
## Solution
50+
51+
```javascript
52+
/**
53+
* @param {string} s
54+
* @return {number}
55+
*/
56+
var maxLengthBetweenEqualCharacters = function(s) {
57+
var leftPosMap = Array(26);
58+
var rightPosMap = Array(26);
59+
var a = 'a'.charCodeAt(0);
60+
for (var i = 0; i < s.length; i++) {
61+
var j = s[i].charCodeAt(0) - a;
62+
if (leftPosMap[j] === undefined) leftPosMap[j] = i;
63+
rightPosMap[j] = i;
64+
}
65+
var max = -1;
66+
for (var m = 0; m < 26; m++) {
67+
if (leftPosMap[m] !== rightPosMap[m]) {
68+
max = Math.max(max, rightPosMap[m] - leftPosMap[m] - 1);
69+
}
70+
}
71+
return max;
72+
};
73+
```
74+
75+
**Explain:**
76+
77+
nope.
78+
79+
**Complexity:**
80+
81+
* Time complexity : O(n).
82+
* Space complexity : O(1).

0 commit comments

Comments
(0)

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