当前位置: 首页 > 工具软件 > hacker-blocks > 使用案例 >

Piling Up! in Python 3 - Hackerrank Solution

郜修雅
2023-12-01

Piling Up!

问题描述

  • 大概就是要求从左到右,从右到左往中间逐次减少

样例演示

  • 输入
STDIN        Function
-----        --------
2            T = 2
6            blocks[] size n = 6
4 3 2 1 3 4  blocks = [4, 3, 2, 1, 3, 4]
3            blocks[] size n = 3
1 3 2        blocks = [1, 3, 2]
  • 输出
Yes
No

参考代码

from collections import deque

N = int(input())

for _ in range(N):
    flag = True
    input()
    d = deque(map(int, input().strip().split()))
    if(d[0] >= d[-1]):
        max = d.popleft()
    else:
        max = d.pop()
    while d:
        if(len(d)==1):
            if(d[0] <= max):
                break
            else:
                flag = False
                break
        else:
            if(d[0]<=max and d[-1]<=max):
                if(d[0]>=d[-1]):
                    max = d.popleft()
                else:
                    max = d.pop()
            elif(d[0]<=max):
                max = d.popleft()
            elif(d[-1]<=max):
                max = d.pop()
            else:
                flag = False
                break
    if flag:
        print("Yes")
    else:
        print("No")
  • 代码转载https://blog.csdn.net/huatian5/article/details/78503761
from collections import deque

T = int(input())
for case in range(T):
    n = int(input())
    ls = list(map(int,input().split()))
    pos = ls.index(min(ls))
    left = ls[:pos]
    right = ls[pos:]
    if left == sorted(left,reverse=True) and right == sorted(right):
        print("Yes")
    else:
        print("No")

总结

  • from collections import deque代表从collections模块中导入双向队列
 类似资料:

相关阅读

相关文章

相关问答