Lilys homework hackerrank

时间:2018-03-26 03:55:24

标签: algorithm sorting

我正试图在hackerrank上解决这个问题

链接:https://www.hackerrank.com/challenges/lilys-homework/problem

Whenever George asks Lily to hang out, she's busy doing homework. George wants to help her finish it faster, but he's in over his head! Can you help George understand Lily's homework so she can hang out with him?

Consider an array of  distinct integers, . George can swap any two elements of the array any number of times. An array is beautiful if the sum of  among  is minimal possible (after, possibly, performing some swaps).

Given the array , find and print the minimum number of swaps that should be performed in order to make the array beautiful.

Input Format

The first line contains a single integer, , denoting the number of elements in the array . 
The second line contains  space-separated integers describing the respective distinct values of .

Constraints

Output Format

Print the minimum number of swaps that should be performed in order to make the array beautiful.

Sample Input

4
2 5 3 1
Sample Output

2

我跟随的算法是:

1.为列表创建值位置散列图

2.sort the array

3.通过列表迭代,如果元素与排序列表元素匹配 - 继续增加交换数量,交换相应的元素。

这是我的代码

import sys

def count_swaps(sorted_list, num_list, position_map):
    swaps = 0
    for index, num in enumerate(num_list):
        if num != sorted_list[index]:

            swaps += 1
            temp = num_list[index]
            pos = position_map[sorted_list[index]]
            num_list[index] = num_list[pos]
            num_list[pos] = temp


    return swaps


def lilysHomework(arr):
    # Complete this function
    position_map = {}
    arr_copy = []
    for index, num in enumerate(arr):
        position_map[num] = index
        arr_copy.append(num)

    sorted_list_asc = sorted(arr, reverse = True)
    sorted_list_dsc = sorted(arr_copy, reverse = False)

    swaps_a = count_swaps(sorted_list_asc, arr, position_map)
    swaps_d = count_swaps(sorted_list_dsc, arr_copy, position_map)

    return min(swaps_a, swaps_d)

if __name__ == "__main__":
    n = int(input().strip())
    arr = list(map(int, input().strip().split(' ')))
    result = lilysHomework(arr)
    print(result)

但这是3个测试用例的失败而且它们很大并且我无法用它们进行调试。我试图调试4小时但没有运气

输入一个失败的测试用例

274
656085744 592976686 3037922 82266352 17574000 344340000 8406892 591292449 569625472 899357375 251327440 301303036 281400020 77370228 15516426 82859300 88364436 39767760 148417500 306863056 10926174 118195200 310408774 309307894 200852782 82193280 424056750 249277128 180368880 477624006 86748948 7434336 48882310 112635040 6614541 503907132 598034610 160500171 70444416 72752680 271416096 30521205 529365648 399367584 129849984 263500556 76737948 464269640 613416088 162724716 163420800 720512988 1217212920 727647624 383190420 8350904 3456024 289141064 123384024 158867856 82005504 36225521 533012608 54370440 17671500 53627000 26597644 855638940 55343960 57828624 108025344 21431808 1182600 265643950 30054300 219553915 74203500 658061160 7502400 931691763 295769136 107345925 80109000 130922055 33544944 65280 452996453 301655430 7828912 425016000 297635898 26861016 739961600 928116 19645470 8691456 5123880 596100015 2735436 25596483 173620720 202797504 161748972 30122300 11082820 574006860 426732182 71136825 105659136 1808140278 450779280 286831620 104683008 938781480 175050736 255681692 67096152 119518575 15449840 25273458 165048960 5642130 85199958 354920488 446786340 131214816 41533296 25766518 90782304 59007600 700369740 122021794 56982366 238027920 434370816 223677580 72463156 355858300 144914616 145950 13822570 19914930 11072100 21450528 124958730 105156800 20843784 781192188 448358850 6139822 95694780 78713888 677177472 9963972 366432768 181113408 725292862 473528052 12864000 518355540 55832070 318508876 89963781 1796290329 844308846 428627693 276255100 123609720 440449488 27589680 426614166 110068200 408846096 620309228 186565236 49051552 561738897 650114105 32646556 7174400 275364045 945364797 3674160 66314292 11073770 14885370 245088324 669628848 33110250 971699976 324099072 259496060 492110802 52206516 508725376 1534995792 148078816 57993375 121071195 381960195 12496176 23728250 159836835 712982980 160098622 909675852 110400300 485423372 30637838 339925836 285371600 13618242 80809650 92375040 265612788 1151909241 234661320 16962144 213417000 269646860 1015650090 117439476 53164566 6946134 89506800 305469360 13406432 292353000 9969642 43982198 23887296 67730660 16384192 218824704 1082660306 1679473908 136336179 39265884 1077096020 464272368 87048192 56752487 156212388 360621525 8472816 17613600 62143172 82696127 79939536 155805468 159499963 262072360 39827904 179532360 2455040 327280740 148409340 73738980 3394872 3082560 225009981 188912256 179487784 340340 315171072 96680664 621206280 536379504 391714074 65055960 105570465 365721408 106712025 286764660

2 个答案:

答案 0 :(得分:2)

我们可以使用2个映射和4个数组来解决此问题。

2个用于存储原始数组的数组和1个用于存储升序排列的数组的数组和1个用于存储降序排列的数组的数组。

我们需要注意的一件事是,要使数组美丽,应该对其进行排序(以升序或降序排列)。


要确定将给定数组转换为已排序数组所需的最小交换次数,我们可以使用映射。

我们的答案是将交换数组转换为升序或降序的最小交换次数


有关详细说明,我建议您观看alGOds的这段视频:

链接:https://youtu.be/W8oGaAEOeRU


答案 1 :(得分:1)

我什至不知道您的代码如何通过样本测试(测试在提交代码进行评估之前运行)-对我而言,其中之一失败了。

代码的主要问题是,每次position_map时,您不会更新num != sorted_list[index],而是交换值而不是索引。

最初,position_mapnum_list的值映射到它们的索引。然后,当您根据num_listsorted_list进行排序时,num_list中的值将被交换,position_map中的索引也必须被交换(但它们不会)。因此,请更新索引!

此外,如果position_mapcount_swaps内被修改,则无法重复使用。因此,应将其副本发送到count_swaps或直接在count_swaps中创建。

考虑到固定代码上方的几点,

...
def count_swaps(sorted_list, num_list):
    swaps = 0

    position_map = {}
    for index, num in enumerate(num_list):
        position_map[num] = index

    for index, num in enumerate(num_list):
        if num != sorted_list[index]:
            swaps += 1
            pos = position_map[sorted_list[index]]
            # IMPORTANT: update indices within position_map 
            position_map[num_list[index]] = pos
            position_map[num_list[pos]] = index
            # swap values as well
            num_list[index], num_list[pos] = num_list[pos], num_list[index]      

    return swaps


def lilysHomework(arr):
    arr_copy = list(arr)

    sorted_list_asc = sorted(arr, reverse = False)
    sorted_list_dsc = sorted(arr_copy, reverse = True)

    swaps_a = count_swaps(sorted_list_asc, arr)
    swaps_d = count_swaps(sorted_list_dsc, arr_copy)

    return min(swaps_a, swaps_d)
...