-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLily's Homework.java
59 lines (50 loc) · 1.96 KB
/
Lily's Homework.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
50
51
52
53
54
55
56
57
58
/*
Problem Title: Lily's Homework
Problem URL: https://www.hackerrank.com/challenges/one-month-preparation-kit-lilys-homework/problem?h_l=interview&playlist_slugs%5B%5D%5B%5D=preparation-kits&playlist_slugs%5B%5D%5B%5D=one-month-preparation-kit&playlist_slugs%5B%5D%5B%5D=one-month-week-four
Max Score: 100
Score: 100
Language: Java
Category: One Month Preparation
*/
public static int lilysHomework(List<Integer> arr) {
TreeMap<Integer, Integer> incSeq = new TreeMap<Integer, Integer>();
TreeMap<Integer, Integer> decSeq = new TreeMap<Integer, Integer>();
List<Integer> arrCopy = new ArrayList<Integer>();
int sz = arr.size();
for (int i = 0; i < sz; ++i) {
incSeq.put(arr.get(i), i);
decSeq.put(arr.get(i), i);
arrCopy.add(arr.get(i));
}
int incSwaps = 0;
for (int i = 0; i < sz; ++i)
{
int targetValue = incSeq.firstKey();
if (arr.get(i) != targetValue)
{
int targetIndex = incSeq.get(targetValue);
int temp = arr.get(i);
arr.set(i, targetValue);
arr.set(targetIndex, temp);
incSeq.put(arr.get(targetIndex), targetIndex);
incSwaps++;
}
incSeq.remove(targetValue);
}
int decSwaps = 0;
for (int i = sz-1; i > -1; --i)
{
int targetValue = decSeq.firstKey();
if (arrCopy.get(i) != targetValue)
{
int targetIndex = decSeq.get(targetValue);
int temp = arrCopy.get(i);
arrCopy.set(i, targetValue);
arrCopy.set(targetIndex, temp);
decSeq.put(arrCopy.get(targetIndex), targetIndex);
decSwaps++;
}
decSeq.remove(targetValue);
}
return Math.min(incSwaps, decSwaps);
}