zoukankan      html  css  js  c++  java
  • [leetcode]1054. Distant Barcodes

    出差没有状态,啃不动前面几道硬骨头,倒着做。

    Ver.1:

    时间复杂度很美,但是无法处理 1 2 2 2 5, 1 1 1 2 3这类输入:

    #1 2 2 2 5
    #2 1 2 5 2

    #2 1 1 1 3
    #1 1 1 2 3
    #1 2 1 3 1

    class Solution:
        def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
            lengthS =len(barcodes)
            #0,1
            if lengthS == 0 or lengthS == 1:
                return barcodes
            #regular
            barcodes = sorted(barcodes)
            ret =[]
            ret.append(barcodes[lengthS // 2])
            index = 0
            while(len(ret) < lengthS):
                #
                if len(ret) < lengthS:
                    if ret[-1] != barcodes[index]:
                        ret.append(barcodes[index])
                else:
                    return ret
                #
                if len(ret) < lengthS:
                    if ret[-1] != barcodes[- (index+1)]:
                        ret.append(barcodes[- (index+1)])
                else:
                    return ret
                #check
                if len(ret)%2 == 1:
                    index = index + 1
            return ret

    Ver.2:

     其实只要去掉末尾的判断,然后不断缩短list就行了:

    Runtime: 2480 ms, faster than 100.00% of Python3 online submissions forDistant Barcodes.
    Memory Usage: 14.4 MB, less than 100.00% of Python3 online submissions for Distant Barcodes.
     

    Submission Detail

    57 / 57 test cases passed.
    Status: 

    Accepted

    Runtime: 2480 ms
    Memory Usage: 14.4 MB
    Submitted: 1 minute ago

    Accepted Solutions Runtime Distribution

    Sorry. We do not have enough accepted submissions to show distribution chart.

    Accepted Solutions Memory Distribution

    Sorry. We do not have enough accepted submissions to show distribution chart.
    class Solution:
        def rearrangeBarcodes(self, barcodes: List[int]) -> List[int]:
            lengthS =len(barcodes)
            #0,1
            if lengthS == 0 or lengthS == 1:
                return barcodes
            #regular
            barcodes = sorted(barcodes)
            ret = []
            index = 0
            curr_lengthS = lengthS
            #init
            ret.append(barcodes[lengthS // 2])
            barcodes.remove(barcodes[lengthS // 2])
            curr_lengthS = curr_lengthS -1
            while(len(ret)<lengthS):
                #head
                if len(ret) < lengthS:
                    if ret[-1] != barcodes[index]:
                        ret.append(barcodes[index])
                        barcodes.remove(barcodes[index])
                        curr_lengthS = curr_lengthS - 1
                else:
                    return ret
                #midd
                if len(ret) < lengthS:
                    if ret[-1] != barcodes[curr_lengthS // 2]:
                        ret.append(barcodes[curr_lengthS // 2])
                        barcodes.remove(barcodes[curr_lengthS // 2])
                        curr_lengthS = curr_lengthS - 1
                else:
                    return ret
            return ret
  • 相关阅读:
    [置顶] Blender 三维绘图及渲染软件 (开源优秀强大跨平台)
    观察力训练(福尔摩斯演绎法)
    Shiro 权限框架使用总结
    [置顶] 如何编写出优美的 JavaScript 代码
    TopCoder 入门教程 sqybi完善版
    开源 3D CAD 软件 gCAD3D 1.9.1 发布
    Java 局部内部类访问局部变量必须加 final 关键字
    [置顶] 程序员如何做出“不难看”的设计
    JavaFX DirectoryChooser[目录选择器]使用实例及源代码[图文]
    项目管理软件收集
  • 原文地址:https://www.cnblogs.com/alfredsun/p/10928294.html
Copyright © 2011-2022 走看看