scala 复杂代码研究1 1144. 递减元素使数组呈锯齿状

发布时间:2020年03月19日 阅读:237 次

https://leetcode-cn.com/problems/decrease-elements-to-make-array-zigzag/

object Solution {
    def movesToMakeZigzag(A: Array[Int]): Int = {
        def f(i:Int):Int = {
            A.indices.toList
            .filter(_%2 == i)
            .map{x =>
             0 max (List(x-1, x+1) filter A.indices.contains map A map (y => A(x) - y + 1) max)}
            .sum
      }
        f(0) min f(1)
    }
}

首先输入数组A 然后取其数组下标 转换为List

然后删选 偶数下标 和 奇数下标

分析该数 是否 可以减去。

将 x 转换为该位置的贡献。


取该位置左侧右侧下标 删选下标是否合法

然后取数组内的数据

然后取两侧哪边改变最大

list(x-1,x+1) filter A.indices.contains _ .map(A).map(y=>A(x)-y+1).max

Tag:spark
相关文章

发表评论: