zoukankan      html  css  js  c++  java
  • HackerRank

    Nothing special, just some corner cases

    n = int(input())
    arr = [int(i) for i in input().strip().split()]
    
    # Collect 
    rec = []
    i = 0
    while i < n - 1:
        if arr[i] > arr[i + 1]:
            inx = i
            cnt = 1
            while i < n - 1 and arr[i] > arr[i + 1]:
                cnt += 1
                i += 1
            rec.append((inx, cnt))
        else:
            i += 1
    
    # Check
    if len(rec) == 1:
        if rec[0][1] == 2:
            if rec[0][0] + 1 < n - 1 and arr[rec[0][0]] > arr[rec[0][0] + 2]:
                print ("no")
            else:
                print ("yes")
                print ("swap %d %d" % (rec[0][0] + 1, rec[0][0] + 2))
        elif rec[0][1] > 2:
            if rec[0][0] + rec[0][1] - 1 < n - 1 and arr[rec[0][0]] > arr[rec[0][0] + rec[0][1]]:
                print ("no")
            else:
                print ("yes")
                print ("reverse %d %d" % (rec[0][0] + 1, rec[0][0] + rec[0][1]))
    elif len(rec) == 2 and rec[0][1] == 2 and rec[1][1] == 2:
        print ("yes")
        print ("swap %d %d" % (rec[0][0] + 1, rec[1][0] + 2))
    else:
        print ("no")
  • 相关阅读:
    maxProfit2
    maxProfit
    getRow
    generate
    hasPathSum
    minDepth
    isBalanced
    sortedArrayToBST
    不得不学的 TCP三次握手和四次挥手
    java生态框架、中间件该有的都有了
  • 原文地址:https://www.cnblogs.com/tonix/p/4346126.html
Copyright © 2011-2022 走看看