返回介绍

solution / 2600-2699 / 2672.Number of Adjacent Elements With the Same Color / README_EN

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

2672. Number of Adjacent Elements With the Same Color

中文文档

Description

There is a 0-indexed array nums of length n. Initially, all elements are uncolored (has a value of 0).

You are given a 2D integer array queries where queries[i] = [indexi, colori].

For each query, you color the index indexi with the color colori in the array nums.

Return _an array _answer_ of the same length as _queries_ where _answer[i]_ is the number of adjacent elements with the same color after the _ith_ query_.

More formally, answer[i] is the number of indices j, such that 0 <= j < n - 1 and nums[j] == nums[j + 1] and nums[j] != 0 after the ith query.

 

Example 1:

Input: n = 4, queries = [[0,2],[1,2],[3,1],[1,1],[2,1]]
Output: [0,1,1,0,2]
Explanation: Initially array nums = [0,0,0,0], where 0 denotes uncolored elements of the array.
- After the 1st query nums = [2,0,0,0]. The count of adjacent elements with the same color is 0.
- After the 2nd query nums = [2,2,0,0]. The count of adjacent elements with the same color is 1.
- After the 3rd query nums = [2,2,0,1]. The count of adjacent elements with the same color is 1.
- After the 4th query nums = [2,1,0,1]. The count of adjacent elements with the same color is 0.
- After the 5th query nums = [2,1,1,1]. The count of adjacent elements with the same color is 2.

Example 2:

Input: n = 1, queries = [[0,100000]]
Output: [0]
Explanation: Initially array nums = [0], where 0 denotes uncolored elements of the array.
- After the 1st query nums = [100000]. The count of adjacent elements with the same color is 0.

 

Constraints:

  • 1 <= n <= 105
  • 1 <= queries.length <= 105
  • queries[i].length == 2
  • 0 <= indexi <= n - 1
  • 1 <=  colori <= 105

Solutions

Solution 1

class Solution:
  def colorTheArray(self, n: int, queries: List[List[int]]) -> List[int]:
    nums = [0] * n
    ans = [0] * len(queries)
    x = 0
    for k, (i, c) in enumerate(queries):
      if i > 0 and nums[i] and nums[i - 1] == nums[i]:
        x -= 1
      if i < n - 1 and nums[i] and nums[i + 1] == nums[i]:
        x -= 1
      if i > 0 and nums[i - 1] == c:
        x += 1
      if i < n - 1 and nums[i + 1] == c:
        x += 1
      ans[k] = x
      nums[i] = c
    return ans
class Solution {
  public int[] colorTheArray(int n, int[][] queries) {
    int m = queries.length;
    int[] nums = new int[n];
    int[] ans = new int[m];
    for (int k = 0, x = 0; k < m; ++k) {
      int i = queries[k][0], c = queries[k][1];
      if (i > 0 && nums[i] > 0 && nums[i - 1] == nums[i]) {
        --x;
      }
      if (i < n - 1 && nums[i] > 0 && nums[i + 1] == nums[i]) {
        --x;
      }
      if (i > 0 && nums[i - 1] == c) {
        ++x;
      }
      if (i < n - 1 && nums[i + 1] == c) {
        ++x;
      }
      ans[k] = x;
      nums[i] = c;
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> colorTheArray(int n, vector<vector<int>>& queries) {
    vector<int> nums(n);
    vector<int> ans;
    int x = 0;
    for (auto& q : queries) {
      int i = q[0], c = q[1];
      if (i > 0 && nums[i] > 0 && nums[i - 1] == nums[i]) {
        --x;
      }
      if (i < n - 1 && nums[i] > 0 && nums[i + 1] == nums[i]) {
        --x;
      }
      if (i > 0 && nums[i - 1] == c) {
        ++x;
      }
      if (i < n - 1 && nums[i + 1] == c) {
        ++x;
      }
      ans.push_back(x);
      nums[i] = c;
    }
    return ans;
  }
};
func colorTheArray(n int, queries [][]int) (ans []int) {
  nums := make([]int, n)
  x := 0
  for _, q := range queries {
    i, c := q[0], q[1]
    if i > 0 && nums[i] > 0 && nums[i-1] == nums[i] {
      x--
    }
    if i < n-1 && nums[i] > 0 && nums[i+1] == nums[i] {
      x--
    }
    if i > 0 && nums[i-1] == c {
      x++
    }
    if i < n-1 && nums[i+1] == c {
      x++
    }
    ans = append(ans, x)
    nums[i] = c
  }
  return
}
function colorTheArray(n: number, queries: number[][]): number[] {
  const nums: number[] = new Array(n).fill(0);
  const ans: number[] = [];
  let x = 0;
  for (const [i, c] of queries) {
    if (i > 0 && nums[i] > 0 && nums[i - 1] == nums[i]) {
      --x;
    }
    if (i < n - 1 && nums[i] > 0 && nums[i + 1] == nums[i]) {
      --x;
    }
    if (i > 0 && nums[i - 1] == c) {
      ++x;
    }
    if (i < n - 1 && nums[i + 1] == c) {
      ++x;
    }
    ans.push(x);
    nums[i] = c;
  }
  return ans;
}

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

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

发布评论

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