【问题标题】:Failed testcases for lilys homework hackerranklily 的作业hackerrank 失败的测试用例
【发布时间】:2018-03-26 03:55:23
【问题描述】:

问题

每当乔治约莉莉出去玩时,她都在忙着做作业。

George 想帮她更快地完成它,但他太难了!

你能帮助乔治了解莉莉的作业,以便她可以和他一起出去玩吗?

考虑一个不同整数的数组, . George 可以任意交换数组的任意两个元素。如果其中的总和尽可能小(在可能执行一些交换之后),则数组是美丽的。

给定数组,找出并打印为使数组美观而应执行的最小交换次数。

输入格式

第一行包含一个整数,表示数组中元素的数量。 第二行包含空格分隔 描述 的各个不同值的整数。

输出格式

打印应执行的最小交换次数,以便 使阵列美观。

示例输入

4

2 5 3 1

样本输出

2

请查看完整描述here

我的努力

我遵循的算法是:

1.为列表创建值-位置哈希图

2.对数组排序

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

【问题讨论】:

标签: python algorithm sorting


【解决方案1】:

我们可以通过使用 2 个地图和 4 个数组来解决这个问题。

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

我们需要注意的一点是,要使数组漂亮,它应该排序(升序或降序)。


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

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


如需更详细的解释,我建议您观看 alGOds 的此视频:

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


【讨论】:

    【解决方案2】:

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

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

    最初position_mapnum_list 的值映射到它们的索引。然后,当您根据sorted_listnum_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)
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-24
      • 1970-01-01
      • 2021-12-22
      • 2019-03-14
      • 2018-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多