-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_2340.java
49 lines (44 loc) · 1.47 KB
/
_2340.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.fishercoder.solutions.thirdthousand;
public class _2340 {
public static class Solution1 {
public int minimumSwaps(int[] nums) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int num : nums) {
min = Math.min(min, num);
max = Math.max(max, num);
}
int minIndex = -1;
for (int i = 0; i < nums.length; i++) {
if (min == nums[i]) {
minIndex = i;
break;
}
}
int minSwaps = 0;
// now move the leftmost smallest index to the beginning of the array
for (int i = minIndex; i > 0; i--) {
swap(nums, i, i - 1);
minSwaps++;
}
int maxIndex = -1;
for (int i = nums.length - 1; i >= 0; i--) {
if (max == nums[i]) {
maxIndex = i;
break;
}
}
// now move the leftmost smallest index to the beginning of the array
for (int i = maxIndex; i < nums.length - 1; i++) {
swap(nums, i, i + 1); // this line is optional at this point
minSwaps++;
}
return minSwaps;
}
private void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
}