题目如下:

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 day i-1 to produce the array of day i:

  1. If an element is smaller than both its left neighbor and its right neighbor, then this element is incremented.
  2. If an element is bigger than both its left neighbor and its right neighbor, then this element is decremented.
  3. 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 <= 100
  • 1 <= 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的更多相关文章

  1. 【LeetCode】954. Array of Doubled Pairs 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【LeetCode】561. Array Partition I 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...

  3. 【LeetCode】565. Array Nesting 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 【LeetCode】Rotate Array

    Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...

  5. 【leetcode】561. Array Partition I

    原题: Given an array of 2n integers, your task is to group these integers into n pairs of integer, say ...

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

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

  8. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  9. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

随机推荐

  1. Microsoft Visual Studio 中工具箱不显示DevExpress控件的解决办法

    我安装的是DevExpress15.2 1.找到安装目录D:\Program Files\DevExpress15.2\Components\Tools, 运行控制台 内容换成cmd 2.执行下面的命 ...

  2. selenium-server--chromedriver环境

    x 准备环境: 1.selenium-server-standalone-3.8.1.jar 2.chromedriver.exe 一.查看本地电脑chrome浏览器版本信息: 二.访问代理地址:ht ...

  3. Quartz持久化到mongodb

    springboot中集成quzrtz ,持久到mongodb 1.pom引用 <?xml version="1.0" encoding="UTF-8"? ...

  4. Elasticsearch-更新现有文档

    ES-更新现有文档 ES的更新API允许发送文档所需要做的修改,而且API会返回一个答复,告知操作是否成功.更新流程如下 1. 检索现有的文档.为了使这步奏效,必须打开_source字段,否则ES并不 ...

  5. python 爬虫--下载图片,下载音乐

    #下载图片 imgUrl='http://www.pptbz.com/pptpic/UploadFiles_6909/201211/2012111719294197.jpg' r=requests.g ...

  6. sql实现同时向主表和子表插入数据方法

    使用sql语句实现同时向主表和子表插入数据方法: Oracle: -- oracle创建sequence create sequence SEQ_test minvalue 1 maxvalue 99 ...

  7. 使用Visual Studio 2019--调试汇编32位代码的详细步骤

    声明:本文使用32位masm,代码与16位,64位不同 ------------------------------------------------------------------------ ...

  8. gunicorn 介绍与性能分析

    阅读此文前建议先阅读 我的博客 gunicorn 是一个 python wsgi http server,只支持在 unix 系统上运行 安装 gunicorn 其实是 python 的一个包,安装方 ...

  9. python基础数据类型之一

    python属于解释型(有良好的平台兼容性,在任何环境中都可以运行,修改代码的时候直接修改就可以,可以快速部署,不用停机维护).动态的(python在编程之前不需要提前设定好各种变量,C语言之类的需要 ...

  10. Task的取消

    原文:.NET 4 并行(多核)编程系列之三 从Task的取消 .NET 4 并行(多核)编程系列之三 从Task的取消 前言:因为Task是.NET 4并行编程最为核心的一个类,也我们在是在并行编程 ...