Lily's Homework

Sort by

recency

|

30 Discussions

|

  • + 0 comments

    Java 8 - used stream to create a set if index that are sorted.

    public static int lilysHomework(List<Integer> arr) {
            // The key to solving this problem is to create a sorted indexs
            // and count the number of the swaps it takes to get things
            // in the corrector order.
            List<Integer> indicesAssending = IntStream.range(0, arr.size())
                .boxed()
                .sorted(Comparator.comparingInt(i -> arr.get(i)))
                .collect(Collectors.toList()) ;
                
            List<Integer> indicesDesending = indicesAssending.stream()
                .collect(Collectors.toList());
            Collections.reverse(indicesDesending);
            
            int resultAssending = swapToOrder(indicesAssending);
            int resultDesending = swapToOrder(indicesDesending);
            
            return Math.min(resultAssending, resultDesending);
        }
        
        /** Swap each sorted indice into it proper location **/
        private static int swapToOrder(List<Integer> indices) {
            int result = 0 ;
            
            for (int i=0; i<indices.size();) {
                if (i != indices.get(i).intValue() ) {
                    ++result;
                    swap(indices, i, indices.get(i)) ;
                } else {
                    ++i;
                }            
            }
            return result;
        }
        
        /** Simple swaping of two indices values**/
        private static void swap(List<Integer> arr, int i1, int i2) {
            Integer v1 = arr.get(i1);
            arr.set(i1, arr.get(i2));
            arr.set(i2, v1); 
        }
    
  • + 0 comments

    Python

    def count_ops(arr, result_arr):
        arr = arr.copy()
        operations = 0
        arr_map = {value: index for index, value in enumerate(arr)}
        
        for index, value in enumerate(arr):
            target_value = result_arr[index]
            if value != target_value:
                target_index = arr_map[target_value]
                arr[target_index] = value
                arr_map[value] = target_index
                operations += 1
        
        return operations
    
    def lilysHomework(arr):
        reversed_arr = arr[::-1]
        sorted_arr = sorted(arr)
        
        return min(count_ops(arr, sorted_arr), count_ops(reversed_arr, sorted_arr))
    
  • + 0 comments

    I try to find cycles by comparing the list to sorted array. i check forwards and backwards and find which one has the min. O(n log n) space, 0(n) space ` def lilysHomework(arr): # Write your code here sorted_arr = arr.copy() sorted_arr.sort() arr_map = {v: k for k, v in enumerate(sorted_arr)} visited = [0] * len(arr)

    swaps = 0
    r_swaps = 0
    # print(arr_map)
    for i in range(len(arr)):
        if arr[i] != sorted_arr[i]:
            k = i
            if not visited[k]:
                swaps -= 1
            while not visited[k]:
                # print("visiting", k)
                visited[k] = 1
                k = arr_map[arr[k]]
                swaps += 1
    
    sorted_arr.reverse()
    arr_map = {v: k for k, v in enumerate(sorted_arr)}
    visited = [0] * len(arr)
    
    for i in range(len(arr)):
        if arr[i] != sorted_arr[i]:
            k = i
            if not visited[k]:
                r_swaps -= 1
            while not visited[k]:
                # print("r visiting", k)
                visited[k] = 1
                k = arr_map[arr[k]]
                r_swaps += 1
    
    return min(swaps, r_swaps)
    
  • + 0 comments

    Here is HackerRank Lily's Homework problem solution in Python java c++ c and javascript

  • + 0 comments

    My solution only passed test case 0, 7, 10 and 11. Does anyone know why?

    public static int lilysHomework(List<Integer> arr) {
            List<Integer> ascendingSorted = new ArrayList<>(arr);
            List<Integer> descendingSorted = new ArrayList<>(arr);
            Collections.sort(ascendingSorted);
            Collections.sort(descendingSorted, (a,b) -> (b - a));
            
            if(arr.equals(ascendingSorted) || arr.equals(descendingSorted))
                return 0;
                
            List<Integer> ascendingList = new ArrayList<>(arr);
            List<Integer> descendingList = new ArrayList<>(arr);
            
            Map<Integer, Integer> ascendingIdxMap = new HashMap<>();
            Map<Integer, Integer> descendingIdxMap = new HashMap<>();
            for(int i = 0; i < arr.size(); i++){
                ascendingIdxMap.put(ascendingSorted.get(i), i);
                descendingIdxMap.put(descendingSorted.get(i), i);
            }
            int ascCount = 0, desCount = 0;
            for(int i = 0; i < arr.size(); i++){
                if(ascendingList.get(i) != ascendingSorted.get(i)){
                    int tmp = ascendingList.get(i);
                    int idx = ascendingIdxMap.get(ascendingList.get(i));
                    ascendingList.set(idx, tmp);
                    ascCount++;
                }                
                if(descendingList.get(i) != descendingSorted.get(i)){
                    int tmp = descendingList.get(i);
                    int idx = descendingIdxMap.get(descendingList.get(i));
                    descendingList.set(idx, tmp);
                    desCount++;
                }                
            }
            return Math.min(ascCount, desCount);
        }