2672. Number of Adjacent Elements With the Same Color Medium

@problem@discussion
#Array



1/**
2 * [2672] Number of Adjacent Elements With the Same Color
3 *
4 * You are given an integer n representing an array colors of length n where all elements are set to 0's meaning uncolored. You are also given a 2D integer array queries where queries[i] = [indexi, colori]. For the i^th query:
5 * 
6 * 	Set colors[indexi] to colori.
7 * 	Count adjacent pairs in colors set to the same color (regardless of colori).
8 * 
9 * Return an array answer of the same length as queries where answer[i] is the answer to the i^th query.
10 *  
11 * <strong class="example">Example 1:
12 * <div class="example-block">
13 * Input: <span class="example-io">n = 4, queries = [[0,2],[1,2],[3,1],[1,1],[2,1]]</span>
14 * Output: <span class="example-io">[0,1,1,0,2]</span>
15 * Explanation:
16 * 
17 * 	Initially array colors = [0,0,0,0], where 0 denotes uncolored elements of the array.
18 * 	After the 1^st query colors = [2,0,0,0]. The count of adjacent pairs with the same color is 0.
19 * 	After the 2^nd query colors = [2,2,0,0]. The count of adjacent pairs with the same color is 1.
20 * 	After the 3^rd query colors = [2,2,0,1]. The count of adjacent pairs with the same color is 1.
21 * 	After the 4^th query colors = [2,1,0,1]. The count of adjacent pairs with the same color is 0.
22 * 	After the 5^th query colors = [2,1,1,1]. The count of adjacent pairs with the same color is 2.
23 * </div>
24 * <strong class="example">Example 2:
25 * <div class="example-block">
26 * Input: <span class="example-io">n = 1, queries = [[0,100000]]</span>
27 * Output: <span class="example-io">[0]</span>
28 * Explanation:
29 * After the 1^st query colors = [100000]. The count of adjacent pairs with the same color is 0.
30 * </div>
31 *  
32 * Constraints:
33 * 
34 * 	1 <= n <= 10^5
35 * 	1 <= queries.length <= 10^5
36 * 	queries[i].length == 2
37 * 	0 <= indexi <= n - 1
38 * 	1 <=  colori <= 10^5
39 * 
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/number-of-adjacent-elements-with-the-same-color/
44// discuss: https://leetcode.com/problems/number-of-adjacent-elements-with-the-same-color/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49    pub fn color_the_array(n: i32, queries: Vec<Vec<i32>>) -> Vec<i32> {
50        vec![]
51    }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn test_2672() {
62    }
63}
64


Back
© 2025 bowen.ge All Rights Reserved.