zoukankan      html  css  js  c++  java
  • 独角兽公司电话面试 Coding 题目的解答 2019-June

    前提假设是 两边 边界, 有无穷高的墙.

    这里是完整的代码和输出示例.

    # Time: max(A) * len(A)
    # Space: O(1)
    
    def fill_slot(B, curt):
        if not B:
            return
        n = len(B)
        i = curt
        while i > 0 and B[i - 1] <= B[i]:
            i -= 1
        left, left_low = i, B[i]
        i = curt
        while i < n - 1 and B[i + 1] <= B[i]:
            i += 1
        right, right_low = i, B[i]
        if left_low <= right_low:
            B[left] += 1
            return
        B[right] += 1
    
    def print_hist(A, position, qty):
        B = A[:]
        print(A)
        for _ in range(qty):
            fill_slot(B, position)
            print(B)
        
        height = max(B)
        n = len(B)
        for h in range(height, 0, -1):
            line = []
            for i in range(n):
                if h > B[i]:
                    line.append(" ")
                    print(" ", end = " ")
                elif A[i] < h <= B[i]:
                    line.append("o")
                    print("o", end = " ")
                elif h <= A[i]:
                    line.append("x")
                    print("x", end = " ")
            print("", end = "
    ")
            # print(" ".join(line))
        print(" ".join([str(i) for i in range(n)]))
    A = [7,1,3,1,0,4,1,0]
    print_hist(A, 5, 4)
    print_hist(A, 5, 9)
    print_hist(A, 4, 9)

    输出 - print_hist(A, 5, 4)

    [7, 1, 3, 1, 0, 4, 1, 0]
    [7, 1, 3, 1, 1, 4, 1, 0]
    [7, 1, 3, 1, 1, 4, 1, 1]
    [7, 1, 3, 2, 1, 4, 1, 1]
    [7, 1, 3, 2, 2, 4, 1, 1]
    
    
    x
    x
    x
    x         x
    x   x     x
    x   x o o x
    x x x x o x x o
    
    0 1 2 3 4 5 6 7

    输出 - print_hist(A, 5, 9)

    [7, 1, 3, 1, 0, 4, 1, 0]
    [7, 1, 3, 1, 1, 4, 1, 0]
    [7, 1, 3, 1, 1, 4, 1, 1]
    [7, 1, 3, 2, 1, 4, 1, 1]
    [7, 1, 3, 2, 2, 4, 1, 1]
    [7, 1, 3, 2, 2, 4, 1, 2]
    [7, 1, 3, 2, 2, 4, 2, 2]
    [7, 1, 3, 3, 2, 4, 2, 2]
    [7, 1, 3, 3, 3, 4, 2, 2]
    [7, 2, 3, 3, 3, 4, 2, 2]
    
    x
    x
    x
    x         x
    x   x o o x
    x o x o o x o o
    x x x x o x x o
    
    0 1 2 3 4 5 6 7
    print_hist(A, 4, 9)
    [7, 1, 3, 1, 0, 4, 1, 0]
    [7, 1, 3, 1, 1, 4, 1, 0]
    [7, 1, 3, 2, 1, 4, 1, 0]
    [7, 1, 3, 2, 2, 4, 1, 0]
    [7, 1, 3, 3, 2, 4, 1, 0]
    [7, 1, 3, 3, 3, 4, 1, 0]
    [7, 2, 3, 3, 3, 4, 1, 0]
    [7, 3, 3, 3, 3, 4, 1, 0]
    [7, 4, 3, 3, 3, 4, 1, 0]
    [7, 4, 4, 3, 3, 4, 1, 0]
    
    x
    x
    x
    x o o     x
    x o x o o x
    x o x o o x
    x x x x o x x
    
    0 1 2 3 4 5 6 7
  • 相关阅读:
    tcp流协议产生的粘包问题和解决方案
    使用fork并发处理多个client的请求和对等通信p2p
    最简单的回射客户/服务器程序、time_wait 状态
    C/S程序的一般流程和基本socket函数
    socket概述和字节序、地址转换函数
    IP数据报格式和IP地址路由
    利用ARP和ICMP协议解释ping命令
    TCP/IP协议栈与数据报封装
    从汇编角度来理解linux下多层函数调用堆栈运行状态
    read/write函数与(非)阻塞I/O的概念
  • 原文地址:https://www.cnblogs.com/goodwish/p/10984067.html
Copyright © 2011-2022 走看看