题目如下:

Given an array nums of integers, a move consists of choosing any element and decreasing it by 1.

An array A is 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 nums into 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: 4

Constraints:

  • 1 <= nums.length <= 1000
  • 1 <= 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的更多相关文章

  1. 【LeetCode】167. Two Sum II - Input array is sorted

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...

  2. 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告

    今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sort ...

  3. 【LeetCode】702. Search in a Sorted Array of Unknown Size 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 二分查找 日期 题目地址:https://lee ...

  4. 【LeetCode】453. Minimum Moves to Equal Array Elements 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:模拟过程 方法二:求和-n*最小值 方法三: ...

  5. 【LeetCode】462. Minimum Moves to Equal Array Elements II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 方法二:直接找中位数 日期 题目地址: ...

  6. 【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 ...

  7. 【leetcode】453. Minimum Moves to Equal Array Elements

    problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...

  8. 【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. ...

  9. 【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 ...

随机推荐

  1. 系统分析与设计HW6

    1. 使用 UML State Model建模 建模对象: 参考 Asg_RH 文档, 对 Reservation/Order 对象建模. 建模要求: 参考练习不能提供足够信息帮助你对订单对象建模,请 ...

  2. CentOS7 yum报 Cannot retrieve metalink for repository: epel/x86_64. Please verify its path解决方法

    打开/etc/yum.repos.d/epel.repo,将 [epel] name=Extra Packages for Enterprise Linux 6 – $basearch baseurl ...

  3. java四舍五入及注意点

    package com.example.newtest.test; import java.math.BigDecimal; import java.math.RoundingMode; import ...

  4. 应用安全 - Web安全 - 远程控制管理工具 - 汇总

    菜刀 蚁剑 冰蝎 DarkCommet ADT windows/upexec/reverse_tcp set PEXEC xxx

  5. 伯努利分布、二项分布、多项分布、Beta分布、Dirichlet分布

    1. 伯努利分布 伯努利分布(Bernoulli distribution)又名两点分布或0-1分布,介绍伯努利分布前首先需要引入伯努利试验(Bernoulli trial). 伯努利试验是只有两种可 ...

  6. 【Qt开发】QT中显示图像数据

    一般图像数据都是以RGBRGBRGB--字节流的方式(解码完成后的原始图像流),我说成字节流,那就表明R,G,B的值各占一个字节,在编程时表示的就是unsigned char * data. 我们先来 ...

  7. 【Linux开发】Linux启动脚本设置

    前言linux有自己一套完整的启动 体系,抓住了linux启动 的脉络,linux的启动 过程将不再神秘.阅读之前建议先看一下附图.本文中假设inittab中设置的init tree为:/etc/rc ...

  8. Zend Framework MVC的结构

    The Zend Framework MVC Architecture 一.概述: In this chapter, we will cover the following topics:1. Zen ...

  9. navicat和Pycharm的连接

    要安装好Mysql,并且实现了Mysql和Navicat的连接: 2.连接界面如下:点击连接,然后点击MySQL就可以看到如下界面 3. 然后就出现新建连接的设置,连接名自己起,用户名和密码和在MyS ...

  10. 手写spring事务框架, 揭秘AOP实现原理。

    AOP面向切面编程:主要是通过切面类来提高代码的复用,降低业务代码的耦合性,从而提高开发效率.主要的功能是:日志记录,性能统计,安全控制,事务处理,异常处理等等. AOP实现原理:aop是通过cgli ...