Skip to content

Commit b06e016

Browse files
authored
Update 1850-minimum-adjacent-swaps-to-reach-the-kth-smallest-number.js
1 parent 290f751 commit b06e016

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

1850-minimum-adjacent-swaps-to-reach-the-kth-smallest-number.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @param {number} k
44
* @return {number}
55
*/
6-
const getMinSwaps = function (num, k) {
6+
const getMinSwaps = function (num, k) {
77
const temp = num.split('')
88
for (let i = 0; i < k; i++) nextPermutation(temp)
99
return count(num.split(''), temp, temp.length)
@@ -16,7 +16,7 @@ function nextPermutation(a) {
1616
//If we found an element
1717
if (i >= 0) {
1818
// Find the rightmost element such that a[j] > a[i]
19-
let j = bSearch(a, i + 1, a.length - 1, a[i])
19+
const j = bSearch(a, i + 1, a.length - 1, a[i])
2020
// swap a[i] and a[j]
2121
a[i] = a[i] ^ a[j] ^ (a[j] = a[i])
2222
}
@@ -26,8 +26,7 @@ function nextPermutation(a) {
2626

2727
function bSearch(a, i, j, key) {
2828
while (i <= j) {
29-
let mid = (i + j) >>> 1
30-
29+
const mid = (i + j) >>> 1
3130
if (key < a[mid]) i = mid + 1
3231
else j = mid - 1
3332
}
@@ -45,11 +44,9 @@ function count(s1, s2, n) {
4544

4645
while (i < n) {
4746
j = i
48-
4947
while (s1[j] != s2[i]) j++
50-
5148
while (i < j) {
52-
let temp = s1[j]
49+
const temp = s1[j]
5350
s1[j] = s1[j - 1]
5451
s1[j-- - 1] = temp
5552
++res

0 commit comments

Comments
 (0)