leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE |

commit0e05b9b95b1528c636d7dd850bddddc033414018
parentc43702b12814d5d6d24f74a54ec7beda177d0cc6
authorDimitrije Dobrota <mail@dimitrijedobrota.com>
dateSat, 3 Feb 2024 22:44:03 +0000

1 Random Problem

Diffstat:
AProblems/2672.cpp|++++++++++++++++++++++++++++
MREADME.md|+

2 files changed, 29 insertions(+), 0 deletions(-)


diff --git a/Problems/2672.cpp b/Problems/2672.cpp

@@ -0,0 +1,28 @@

class Solution {
public:
vector<int> colorTheArray(const int n, const vector<vector<int>> &queries) const {
static int nums[100002];
memset(nums, 0x00, sizeof(nums));
const int m = size(queries);
vector<int> res(m);
int crnt = 0;
for (int i = 0; i < m; i++) {
const int idx = queries[i][0] + 1;
const int color = queries[i][1];
if (nums[idx] != color) {
if (nums[idx]) {
if (nums[idx] == nums[idx - 1]) crnt--;
if (nums[idx] == nums[idx + 1]) crnt--;
}
nums[idx] = color;
if (nums[idx] == nums[idx - 1]) crnt++;
if (nums[idx] == nums[idx + 1]) crnt++;
}
res[i] = crnt;
}
return res;
}
};

diff --git a/README.md b/README.md

@@ -1095,6 +1095,7 @@ for solving problems.

| 2665 | Easy | [Counter II](Problems/2665.js) |
| 2666 | Easy | [Allow One Function Call](Problems/2666.js) |
| 2667 | Easy | [Create Hello World Function](Problems/2667.js) |
| 2672 | Medium | [Number of Adjacent Elements With the Same Color](Problems/2672.cpp) |
| 2673 | Medium | [Make Costs of Paths Equal in a Binary Tree](Problems/2673.cpp) |
| 2676 | Medium | [Throttle](Problems/2676.js) |
| 2679 | Medium | [Sum in a Matrix](Problems/2679.cpp) |