【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-1
to 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 <= 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的更多相关文章
- 【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 ...
随机推荐
- elk logstash Managing Multiline Events
1.Java程序的日志特征,logstash 正为此准备好了 codec/multiline 插件! 有时候应用程序会抛异常,就存在着如何合并多行信息的问题,我这里做的配置就是如果当前行是以‘空格’, ...
- kmp算法分析和C++实现
知乎高赞分析 作者:逍遥行 链接:https://www.zhihu.com/question/21923021/answer/37475572来源:知乎著作权归作者所有.商业转载请联系作者获得授权, ...
- hue改下载行数
参考: https://blog.csdn.net/lingbo229/article/details/85991230 修改hue所在机器的默认配置后,重启hue即可 find / -name be ...
- axios模块封装和分类列表实现
这个作用 主要还是为了让代码更加的,清晰. 不要全部都放到 created(){} 这个方法下面.把这些代码全部抽离出去. 这里就只是去调用方法.1. src 目录下,新建文件夹--- rest ...
- Mongo数据库备份
安全访问状态下 手动在线备份: mongodump -h 127.0.0.1:27017 -u=username -p=123456 -d dbname -o /home/backups 手动恢复: ...
- SSM @Autowired注入失败
1, Intellij IDEA中Mybatis Mapper自动注入警告的6种解决方案 https://blog.csdn.net/weixin_30945319/article/details/9 ...
- linux_文本编译使用命令
一:字符模式与shell命令 字符界面和图形界面 字符界面优点: 1):系统执行效率高,稳定性高,执行结果可直接返回 2):节省系统资源,对一个服务器至关重要 3):节省大量网络开销,大幅降低运行成本 ...
- cut,sort,awk,sed,tr,find,wc,uniq在Linux中的用法
cut语法cut [-bn] [file]cut [-c] [file]cut [-df] [file] -b :以字节为单位进行分割.这些字节位置将忽略多字节字符边界,除非也指定了 -n 标志.-c ...
- Linux服务器Java进程突然消失排查办法
出处:JAVA进程突然消失的原因? 问题描述 在实际生产环境下,如果我们遇见Java进程突然消失,该如何去排查问题? 思路 可能有几种原因: ①.Java应用程序的问题:发生OOM导致进程Crash ...
- C语言函数调用时候内存中栈的动态变化详细分析(彩图)
版权声明:本文为博主原创文章,未经博主允许不得转载.欢迎联系我qq2488890051 https://blog.csdn.net/kangkanglhb88008/article/details/8 ...