You are viewing a single comment's thread. Return to all comments →
public static int findSwaps(List<int> arr) { Dictionary<int, int> d = new Dictionary<int, int>(); int swaps = 0; for(int i=0; i < arr.Count; i++) { d[arr[i]] = i; } List<int> sorted = arr.ToList(); sorted.Sort(); for(int i=0; i<sorted.Count; i++) { if(arr[i] != sorted[i]) { swaps++; int index = d[sorted[i]]; d[arr[i]] = index; int temp = arr[i]; arr[i] = arr[index]; arr[index] = temp; } } return swaps; } public static int lilysHomework(List<int> arr) { List<int> rev_arr = new List<int>(); for(int i=0; i< arr.Count; i++) { rev_arr.Add(arr[arr.Count-i-1]); } int res = Math.Min(findSwaps(arr), findSwaps(rev_arr)); return res; }
Seems like cookies are disabled on this browser, please enable them to open this website
Lily's Homework
You are viewing a single comment's thread. Return to all comments →
C# code