Lily's Homework

Sort by

recency

|

29 Discussions

|

  • + 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);
        }
    
  • + 0 comments
    def num_of_swaps(arr):
        """Calculates min swaps to sort an array."""
        # Create a dictionary to store the original indices of elements
        ori_idx = {val: i for i, val in enumerate(arr)}
        # Sort the array and store the sorted version
        sorted_arr = sorted(arr)
        swap_num = 0  # Initialize swap counter
    
        # Iterate over the array
        for i in range(len(arr)):
            # If the element is out of place
            if arr[i] != sorted_arr[i]:
                # Find the index of the corresponding element in the original array
                j = ori_idx[sorted_arr[i]]
                # Swap the elements
                arr[i], arr[j] = arr[j], arr[i]
                # Update the indices in the ori_idx dictionary
                ori_idx[arr[i]], ori_idx[arr[j]] = i, j
                swap_num += 1  # Increment swap counter
    
        return swap_num  # Return total swaps
    
    def lilysHomework(arr):
        """Calculates min swaps for ascending/descending sort."""
        # Calculate the minimum number of swaps needed for both ascending and descending orders
        return min(num_of_swaps(arr[:]), num_of_swaps(arr[::-1]))