We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 44eac72 commit 099dbffCopy full SHA for 099dbff
765-couples-holding-hands.js
@@ -72,3 +72,35 @@ const minSwapsCouples = function (row) {
72
}
73
74
75
+// another
76
+
77
+/**
78
+ * @param {number[]} row
79
+ * @return {number}
80
+ */
81
+const minSwapsCouples = function (row) {
82
+ let res = 0
83
+ const n = row.length
84
+ const ptn = Array(n).fill(0), pos = Array(n).fill(0)
85
86
+ for(let i = 0; i < n; i++) {
87
+ ptn[i] = (i % 2 === 0 ? i + 1 : i - 1)
88
+ pos[row[i]] = i
89
+ }
90
91
+ for (let i = 0; i < n ;i++) {
92
+ for (let j = ptn[pos[ptn[row[i]]]]; i != j; j = ptn[pos[ptn[row[i]]]]) {
93
+ swap(row, i, j);
94
+ swap(pos, row[i], row[j]);
95
+ res++;
96
97
98
99
+ return res
100
101
+ function swap(arr, i, j) {
102
+ const val = arr[i]
103
+ arr[i] = arr[j]
104
+ arr[j] = val
105
106
+}
0 commit comments