【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 ...
随机推荐
- MySQL学习-入门语句以及增删查改
1. SQL入门语句 SQL,指结构化查询语言,全称是 Structured Query Language,是一种 ANSI(American National Standards Institute ...
- finereport 带多参数查询
1.sql语句 ${,""," and dt.货主地区='"+comboBox0+"'")} ${,""," ...
- IDEA神器
破解 路径:添加-javaagent:JetbrainsCrack-2.7-release-str.jar的路径 例-javaagent:D:\Program Files\JetBrains\Inte ...
- cdh平台问题
问题背景:内容的不懂之处,可以私信博主.友好交流使用.主要针对的问题种类有:网络桥接报错.网卡文件问题(该问题主要看你的安装脚本文件里面写的是否和主机对应,也是运行环境的问题).scm表中没有节点信息 ...
- ubuntu 虚拟机安装
ubuntu16.04.5 LTS 安装 1.下载ubuntu镜像 打开ubuntu官网镜像地址https://launchpad.net/ubuntu/+cdmirrors 在上面中搜索chin ...
- [转帖]APT34攻击全本分析
APT34攻击全本分析 https://zhuanlan.kanxue.com/article-8401.htm 高手太多 我等菜鸟目不暇接.. 一.事件综述 2019年4月18日,黑客/黑客组织使用 ...
- Python模块unittest
Python自带一个单元测试框架,unittest模块,用它来做单元测试,它里面封装好了一些校验返回结果的方法和一些用例执行前的初始化操作 在说unittest之前,先说几个概念: TestCase: ...
- [LeetCode] 113. 路径总和 II
题目链接 : https://leetcode-cn.com/problems/path-sum-ii/ 题目描述: 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径 ...
- D-多连块拼图
多连块是指由多个等大正方形边与边连接而成的平面连通图形. – 维基百科 给一个大多连块和小多连块,你的任务是判断大多连块是否可以由两个这样的小多连块拼成.小多连块只能平移,不能旋转或者翻转.两个小多连 ...
- 各种IE(IE6-IE10)兼容问题一行代码搞定
x-ua-compatible 用来指定IE浏览器解析编译页面的model x-ua-compatible 头标签大小写不敏感,必须用在 head 中,必须在除 title 外的其他 meta 之前使 ...