【leetcode】1144. Decrease Elements To Make Array Zigzag
题目如下:
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: 4Constraints:
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的更多相关文章
- 【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 ...
随机推荐
- 系统分析与设计HW6
1. 使用 UML State Model建模 建模对象: 参考 Asg_RH 文档, 对 Reservation/Order 对象建模. 建模要求: 参考练习不能提供足够信息帮助你对订单对象建模,请 ...
- Flink的基本概念
Stream.Transformation.Operator 用户实现的Flink程序是由Stream和Transformation这两个基本构建块组成,其中Stream是一个中间结果数据,而Tran ...
- CSS——插入形式 基本格式 常见css代码
常见css代码 无下划线链接 字体颜色 + 左边距 背景颜色 字体.字体颜色.大小 文本对齐方式[取代了<center>]
- bzoj3028食物 关于(1+x+x2+x3+x4+...)^k的第i项系数就是c(i+k−1,k−1)的证明
关于(1+x+x2+x3+x4+...)^k的第i项系数就是c(i+k−1,k−1)的证明对于第i项,假设为5x^5=x^0*x^5x^5=x^1*x^4x^5=x^2*x^3........也就是说 ...
- 简述Vue的路由与视图
1.vue-router 安装方式 npm/cnpm:(个人偏向于cnpm) npm/cnpm install vue-router --save-dev bower: bower install v ...
- ball小游戏
2019第三次课程设计实验报告 一.实验项目 -- ball 二.实验功能描述: 玩家通过wsad移动下面的挡板,接住下落的弹球,弹击上头的球获得积分,弹球没接住则比赛结束,计算积分 三.项目模板结构 ...
- centos6.9 安装mysql8
centos6.9 安装 mysql8 # 安装mysql8 1.下载https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.16-2.el6.x86 ...
- Docker 容器(container)及资源限制
Container: 既然container是由image运行起来的,那么是否可以理解为container和image有某种关系?先来看张图: 其实可以理解为container只是基于image之后的 ...
- kafka 安装教程
安装详述: https://www.jianshu.com/p/596f107e901a 3.0:运行:cd 到: D:\Installed_software\Professional\kafka_2 ...
- 如何在centos6和centos7上部署nfs共享服务器和客户端
nfs共享服务为中小型企业在存储上提供了有效的节省空间,许多大型的网站也在使用nfs,如百度和阿里等,下面结合自己所学的知识,阐述如何在centos6和centos7下配置nfs.注:除了必要的说明外 ...