【leetcode】1243. Array Transformation
题目如下:
Given an initial array
arr, every day you produce a new array using the array of the previous day.On the
i-th day, you do the following operations on the array of dayi-1to produce the array of dayi:
- If an element is smaller than both its left neighbor and its right neighbor, then this element is incremented.
- If an element is bigger than both its left neighbor and its right neighbor, then this element is decremented.
- The first and last elements never change.
After some days, the array does not change. Return that final array.
Example 1:
Input: arr = [6,2,3,4]
Output: [6,3,3,4]
Explanation:
On the first day, the array is changed from [6,2,3,4] to [6,3,3,4].
No more operations can be done to this array.Example 2:
Input: arr = [1,6,3,4,3,5]
Output: [1,4,4,4,4,5]
Explanation:
On the first day, the array is changed from [1,6,3,4,3,5] to [1,5,4,3,4,5].
On the second day, the array is changed from [1,5,4,3,4,5] to [1,4,4,4,4,5].
No more operations can be done to this array.Constraints:
1 <= arr.length <= 1001 <= arr[i] <= 100
解题思路:题目不难,注意每次变换前先备份arr,再根据备份的arr的值修改本身的arr。
代码如下:
class Solution(object):
def transformArray(self, arr):
"""
:type arr: List[int]
:rtype: List[int]
"""
while True:
arr_bak = arr[::]
for i in range(1,len(arr_bak)-1):
if arr_bak[i] > arr_bak[i-1] and arr_bak[i] > arr_bak[i+1]:
arr[i] -= 1
elif arr_bak[i] < arr_bak[i-1] and arr_bak[i] < arr_bak[i+1]:
arr[i] += 1
if arr_bak == arr:
break
return arr
【leetcode】1243. Array Transformation的更多相关文章
- 【LeetCode】954. Array of Doubled Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】561. Array Partition I 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- 【LeetCode】565. Array Nesting 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】Rotate Array
Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...
- 【leetcode】561. Array Partition I
原题: Given an array of 2n integers, your task is to group these integers into n pairs of integer, say ...
- 【leetcode】954. Array of Doubled Pairs
题目如下: Given an array of integers A with even length, return true if and only if it is possible to re ...
- 【leetcode】565. Array Nesting
You are given an integer array nums of length n where nums is a permutation of the numbers in the ra ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
随机推荐
- 【Ansible】记一次技术博客害死人的经历——ansible模板变量注入探究
风和日丽,夏天的北京湿热并举,睁不开的眼睛里,横竖都看着是“吃人”. 带薪学习的日子不好过,要在几天内迅速掌握导师下发要求学习的技能,看着以前一起蹲IT坑的同事人来人往,用隔壁同性黄同学的话来说,就是 ...
- JS小知识--获取当前日期的时间和上周五时间
获取当前日期的时间和上周五时间 var today=new Date();//获取当前时间var weekday=today.getDay();//获取星期几 var monday=new Da ...
- mysql 构造连续的日期
需求,我想以 年-月-日的格式,统计自 2019-08-20日 前10天的记录数,如果该天没有任何一条记录,则给予0 原始数据->我想要的结果集数据 ==============> ...
- python list 中extend()与append()区别
def changextend(str): "print string with extend" mylist.extend([40,50,60]); print(mylist) ...
- 有人向你扔了一个bug,哈哈哈哈
有人向你扔了一个bug. "26楼会议室的灯亮着.它应该是熄灭着的." bug的备注里写道"你应该能在5分钟内搞定,只要按一下开关就好了."你去了26楼的会议室 ...
- java this的作用
this: 区分成员变量和局部变量的重名问题,this.name指的是类中的成员变量,而不是方法内部的
- 爬取百度贴吧前1000页内容(requests库面向对象思想实现)
此程序以李毅吧为例子,以面向对象的设计思想实现爬取保存网页数据,暂时并未用到并发处理,以后有机会的话会加以改善 首先去百度贴吧分析贴吧地址栏中url后的参数,找到分页对应的参数pn,贴吧名字对应的参数 ...
- 2019中山纪念中学夏令营-Day12[JZOJ]
Begin (题目的排序方式:题号) 每期新姿势:(今天推荐一位巨佬)Cefola-Kiroxs 推荐知识:namespace的用法(本赛我的代码中将用到) 2019.08.12[NOIP普及组]模拟 ...
- 2019-2020Nowcoder Girl初赛题解
写了一天计算几何,心态崩了,水一篇题解休息休息. emmmm,如果您是一名现役OIer/CSPer,那看这篇文章也许并不能在你的生命中留下些什么(潮子语录),因为相比NOIP/CSP这个比赛其实比较简 ...
- 最长上升子序列(Longest increasing subsequence)
问题描述 对于一串数A={a1a2a3…an},它的子序列为S={s1s2s3…sn},满足{s1<s2<s3<…<sm}.求A的最长子序列的长度. 动态规划法 ...