Lily's Homework

  • + 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))