leetcode

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

commit 8d772ecef89d3e60977ecd3346e3e8527a481955
parent 8e5b2dfac45e150fefbb5d8eae017f2c7600a385
author Dimitrije Dobrota <mail@dimitrijedobrota.com>
date Mon, 19 Feb 2024 12:06:24 +0000

1 Random Problem

Diffstat:
A Problems/3039.cpp | ++++++++++++++++++++++++++
M README.md | +

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


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

@@ -0,0 +1,26 @@
class Solution {
public:
string lastNonEmptyString(const string &s) const {
int count[27] = {0}, last[27] = {0};
int maxi = 0;
for (int i = 0; i < size(s); i++) {
const int idx = s[i] & 0x1F;
maxi = max(maxi, ++count[idx]);
last[idx] = i;
}
vector<int> make;
for (int i = 1; i <= 26; i++) {
if (count[i] != maxi) continue;
make.push_back(i);
}
sort(begin(make), end(make), [&](int i, int j) { return last[i] < last[j]; });
string res(size(make), 0);
for (int i = 0; i < size(make); i++)
res[i] = '`' + make[i];
return res;
}
};

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

@@ -1136,3 +1136,4 @@ for solving problems. | 2971 | Medium | [Find Polygon With the Largest Perimeter](Problems/2971.cpp) | | 2997 | Medium | [Minimum Number of Operations to Make Array XOR Equal to K](Problems/2997.cpp) | | 3015 | Medium | [Count the Number of Houses at a Certain Distance I](Problems/3015.cpp) |
| 3039 | Medium | [Apply Operations to Make String Empty](Problems/3039.cpp) |