765. Couples Holding Hands Hard

@problem@discussion
#Greedy#Depth-First Search#Breadth-First Search#Union Find#Graph



1/**
2 * [765] Couples Holding Hands
3 *
4 * There are n couples sitting in 2n seats arranged in a row and want to hold hands.
5 * The people and seats are represented by an integer array row where row[i] is the ID of the person sitting in the i^th seat. The couples are numbered in order, the first couple being (0, 1), the second couple being (2, 3), and so on with the last couple being (2n - 2, 2n - 1).
6 * Return the minimum number of swaps so that every couple is sitting side by side. A swap consists of choosing any two people, then they stand up and switch seats.
7 *  
8 * Example 1:
9 * 
10 * Input: row = [0,2,1,3]
11 * Output: 1
12 * Explanation: We only need to swap the second (row[1]) and third (row[2]) person.
13 * 
14 * Example 2:
15 * 
16 * Input: row = [3,2,0,1]
17 * Output: 0
18 * Explanation: All couples are already seated side by side.
19 * 
20 *  
21 * Constraints:
22 * 
23 * 	2n == row.length
24 * 	2 <= n <= 30
25 * 	n is even.
26 * 	0 <= row[i] < 2n
27 * 	All the elements of row are unique.
28 * 
29 */
30pub struct Solution {}
31
32// problem: https://leetcode.com/problems/couples-holding-hands/
33// discuss: https://leetcode.com/problems/couples-holding-hands/discuss/?currentPage=1&orderBy=most_votes&query=
34
35// submission codes start here
36
37impl Solution {
38    pub fn min_swaps_couples(row: Vec<i32>) -> i32 {
39        0
40    }
41}
42
43// submission codes end
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_765() {
51    }
52}
53


Back
© 2025 bowen.ge All Rights Reserved.