【leetcode】1144. Decrease Elements To Make Array Zigzag
题目如下:
Given an array
numsof integers, a move consists of choosing any element and decreasing it by 1.An array
Ais a zigzag array if either:
- Every even-indexed element is greater than adjacent elements, ie.
A[0] > A[1] < A[2] > A[3] < A[4] > ...- OR, every odd-indexed element is greater than adjacent elements, ie.
A[0] < A[1] > A[2] < A[3] > A[4] < ...Return the minimum number of moves to transform the given array
numsinto a zigzag array.Example 1:
Input: nums = [1,2,3]
Output: 2
Explanation: We can decrease 2 to 0 or 3 to 1.Example 2:
Input: nums = [9,6,1,6,2]
Output: 4Constraints:
1 <= nums.length <= 10001 <= nums[i] <= 1000
解题思路:本题无外乎两种情况,一种是nums[0] > nums[1],另一种是nums[0] < nums[1],把这两种情况计算一遍求较小值即可。在计算过程中,如果要求nums[i] > nums[i-1],那么把nums[i-1] 减到nums[i] - 1;如果要求nums[i] < nums[i-1],则把nums[i]减少到nums[i-1] - 1。
代码如下:
class Solution(object):
def movesToMakeZigzag(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
# flag - 0: decrease; 1:increase
def process(flag,nums):
count = 0
for i in range(1, len(nums)):
if flag == 0 and nums[i] >= nums[i - 1]:
count += (nums[i] - nums[i - 1] + 1)
nums[i] = nums[i - 1] - 1
elif flag == 1 and nums[i] <= nums[i - 1]:
count += (nums[i - 1] - nums[i] + 1)
nums[i - 1] = nums[i] - 1
flag = not flag
return count #nums[0] > nums[1]
res = process(0,nums[::])
# nums[1] > nums[0]
res = min(res,process(1,nums))
return res
【leetcode】1144. Decrease Elements To Make Array Zigzag的更多相关文章
- 【LeetCode】167. Two Sum II - Input array is sorted
Difficulty:easy More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告
今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...
- 【LeetCode】702. Search in a Sorted Array of Unknown Size 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 二分查找 日期 题目地址:https://lee ...
- 【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:模拟过程 方法二:求和-n*最小值 方法三: ...
- 【LeetCode】462. Minimum Moves to Equal Array Elements II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 方法二:直接找中位数 日期 题目地址: ...
- 【LeetCode】462. Minimum Moves to Equal Array Elements II
Given a non-empty integer array, find the minimum number of moves required to make all array element ...
- 【leetcode】453. Minimum Moves to Equal Array Elements
problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...
- 【leetcode】1261. Find Elements in a Contaminated Binary Tree
题目如下: Given a binary tree with the following rules: root.val == 0 If treeNode.val == x and treeNode. ...
- 【leetcode】Find Minimum in Rotated Sorted Array I&&II
题目概述: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 ...
随机推荐
- 移动端自动化==>Windows-Android-Appium环境搭建
第一步 安装JDK,本机如果带有1.7及以上版本且配置好了环境变量,则可忽略此安装步骤. JDK的安装包,百度下载即可.安装完成后配置环境变量. cmd下验证环境变量是否配置成功 第二步 安装Andr ...
- 6.824 Lab 2: Raft 2A
6.824 Lab 2: Raft Part 2A Due: Feb 23 at 11:59pm Part 2B Due: Mar 2 at 11:59pm Part 2C Due: Mar 9 at ...
- 正则表达式断言(Assertions)
一 零宽正向先行断言 x(?=y) 仅匹配被y跟随的x. const regExp = /Jack(?=Sparrow|Dawson)/g; const str = 'JackJones JackSp ...
- 【ABAP系列】SAP ABAP DYNP_VALUES_UPDATE 更新屏幕字段的函数及用法
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP DYNP_VA ...
- 数据结构系列之2-3树的插入、查找、删除和遍历完整版代码实现(dart语言实现)
弄懂了二叉树以后,再来看2-3树.网上.书上看了一堆文章和讲解,大部分是概念,很少有代码实现,尤其是删除操作的代码实现.当然,因为2-3树的特性,插入和删除都是比较复杂的,因此经过思考,独创了删除时分 ...
- 【Linux开发】为qt-embedded添加jpeg库的交叉编译方法for arm
看了一个文章: =====================================谢论坛内各位的帮助,我的qt/e2.3.10和qtopia2.1.1终于全部编译通过. 下面是jpeg和uui ...
- Java——ArrayList源码解析
以下针对JDK 1.8版本中的ArrayList进行分析. 概述 ArrayList基于List接口实现的大小可变的数组.其实现了所有可选的List操作,并且元素允许为任意类型,包括null元 ...
- 使用使用dockerfile构建webapi镜像然后使用link和bridge两种方式进行桥接
首先新增一个webapi的项目 项目核心代码 UserContext using Microsoft.EntityFrameworkCore; using System; using System.C ...
- Java基础语法-运算符
1算术运算符 1.1运算符和表达式 运算符:对常量和变量进行操作的符号. 表达式:用运算符把常量或者变量连接起来符合java语法的式子就可以称为表达式. 不同运算符链接的表达式体现的是不同类型的表达式 ...
- ES5中的继承
继承 在面向对象的语言中, 大多语言都支持两种继承方式: 接口继承 和 实现继承, 接口继承 只继承方法签名, 实现继承 才继承实际的方法, ECMAScript 值支持 实现继承, 今天我们来谈谈实 ...