返回介绍

solution / 1200-1299 / 1243.Array Transformation / README_EN

发布于 2024-06-17 01:03:21 字数 4372 浏览 0 评论 0 收藏 0

1243. Array Transformation

中文文档

Description

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:

  • 3 <= arr.length <= 100
  • 1 <= arr[i] <= 100

Solutions

Solution 1: Simulation

Simulate each day. For each element, if it is greater than its left and right neighbors, it decreases by 1, otherwise, it increases by 1. If the array no longer changes on a certain day, return that array.

The time complexity is $O(n \times m)$, and the space complexity is $O(n)$. Where $n$ is the length of the array, and $m$ is the maximum value in the array.

class Solution:
  def transformArray(self, arr: List[int]) -> List[int]:
    f = True
    while f:
      f = False
      t = arr[:]
      for i in range(1, len(t) - 1):
        if t[i] > t[i - 1] and t[i] > t[i + 1]:
          arr[i] -= 1
          f = True
        if t[i] < t[i - 1] and t[i] < t[i + 1]:
          arr[i] += 1
          f = True
    return arr
class Solution {
  public List<Integer> transformArray(int[] arr) {
    boolean f = true;
    while (f) {
      f = false;
      int[] t = arr.clone();
      for (int i = 1; i < t.length - 1; ++i) {
        if (t[i] > t[i - 1] && t[i] > t[i + 1]) {
          --arr[i];
          f = true;
        }
        if (t[i] < t[i - 1] && t[i] < t[i + 1]) {
          ++arr[i];
          f = true;
        }
      }
    }
    List<Integer> ans = new ArrayList<>();
    for (int x : arr) {
      ans.add(x);
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> transformArray(vector<int>& arr) {
    bool f = true;
    while (f) {
      f = false;
      vector<int> t = arr;
      for (int i = 1; i < arr.size() - 1; ++i) {
        if (t[i] > t[i - 1] && t[i] > t[i + 1]) {
          --arr[i];
          f = true;
        }
        if (t[i] < t[i - 1] && t[i] < t[i + 1]) {
          ++arr[i];
          f = true;
        }
      }
    }
    return arr;
  }
};
func transformArray(arr []int) []int {
  f := true
  for f {
    f = false
    t := make([]int, len(arr))
    copy(t, arr)
    for i := 1; i < len(arr)-1; i++ {
      if t[i] > t[i-1] && t[i] > t[i+1] {
        arr[i]--
        f = true
      }
      if t[i] < t[i-1] && t[i] < t[i+1] {
        arr[i]++
        f = true
      }
    }
  }
  return arr
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文