题解 | #合唱队#

合唱队

https://www.nowcoder.com/practice/6d9d69e3898f45169a441632b325c7b4

import Foundation

var i = 0
while let line = readLine() {
    let parts = line.split(separator: " ")
    if (i == 0) {
        i = i + 1
        continue
    }
    
    if (i == 2) {
        break
    }
    i = i + 1
    // print(parts)
    var arr:[Int] = parts.map { return Int(String($0))!}
    var upArr:[Int] = [Int](repeating: 0, count: arr.count)
    var downArr:[Int] = [Int](repeating: 0, count: arr.count)
    for i in 0 ... parts.count - 1 {
        if i == 0 {
            upArr[0] = 1
            continue
        }
        var up = 1
        if i > 0 {
            for k in 0 ... i - 1 {
                if arr[i] > arr[k] {
                    upArr[i] = upArr[k] + 1
                    up = max(up, upArr[i])
                }
            }
        }
        upArr[i] = up
        
        
        var down = 1
        var j = parts.count - 1 - i
        if j == parts.count - 1 {
            downArr[j] = 1
        } else {
            for k in j + 1 ... parts.count - 1 {
                if arr[j] > arr[k] {
                    downArr[j] = downArr[k] + 1
                    down = max(down, downArr[j])
                }
            }
        }
        downArr[j] = down
    }
    
    var maxValue = 1
    for i in 0 ... arr.count - 1 {
        maxValue = max(upArr[i] + downArr[i] - 1, maxValue)
    }
    
    print(arr.count - maxValue)
}

#终于自己想出来了一个算法题#
全部评论

相关推荐

投递华为等公司10个岗位
点赞 评论 收藏
分享
无情咸鱼王的秋招日记之薛定谔的Offer:好拒信,偷了,希望有机会用到
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务