leetcode

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

commit4335e1da28fdac35c6b1fb26cc7738fdcbd4311c
parent25df156a12d0592b79d956c9c99c52fb404af70b
authorDimitrije Dobrota <mail@dimitrijedobrota.com>
dateMon, 30 Oct 2023 12:54:38 +0000

Daily Problem and 1 Random Problem

Diffstat:
AProblems/1267.cpp|++++++++++++++++++++++
AProblems/1356.cpp|+++++++++++
MREADME.md|++

3 files changed, 35 insertions(+), 0 deletions(-)


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

@@ -0,0 +1,22 @@

class Solution {
public:
int countServers(const vector<vector<int>> &grid) {
const int n = grid.size(), m = grid[0].size();
int row[256] = {0}, col[256] = {0};
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j]) row[i]++, col[j]++;
}
}
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j]) res += row[i] > 1 || col[j] > 1;
}
}
return res;
}
};

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

@@ -0,0 +1,11 @@

class Solution {
public:
vector<int> sortByBits(vector<int> &arr) const {
sort(begin(arr), end(arr), [](int a, int b) {
const int x = __builtin_popcount(a);
const int y = __builtin_popcount(b);
return x != y ? x < y : a < b;
});
return arr;
}
};

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

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

| 1249 | Medium | [Minimum Remove to Make Valid Parentheses](Problems/1249.cpp) |
| 1254 | Medium | [Number of Closed Islands](Problems/1254.cpp) |
| 1261 | Medium | [Find Elements in a Contaminated Binary Tree](Problems/1261.cpp) |
| 1267 | Medium | [Count Servers that Communicate](Problems/1267.cpp) |
| 1268 | Medium | [Search Suggestions System](Problems/1268.cpp) |
| 1269 | Hard | [Number of Ways to Stay in the Same Place After Some Steps](Problems/1269.cpp) |
| 1277 | Medium | [Count Square Submatrices with All Ones](Problems/1277.cpp) |

@@ -592,6 +593,7 @@ for solving problems.

| 1346 | Easy | [Check if N and Its Double Exist](Problems/1346.cpp) |
| 1347 | Medium | [Minimum Number of Steps to Make Two Strings Anagram](Problems/1347.cpp) |
| 1351 | Easy | [Count Negative Numbers in a Sorted Matrix](Problems/1351.cpp) |
| 1356 | Easy | [Sort Integers by The Number of 1 Bits](Problems/1356.cpp) |
| 1357 | Medium | [Apply Discount Every n Orders](Problems/1357.cpp) |
| 1358 | Medium | [Number of Substrings Containing All Three Characters](Problems/1358.cpp) |
| 1359 | Hard | [Count All Valid Pickup and Delivery Options](Problems/1359.cpp) |