leetcode

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

commit 4c4488067228a6eadc6f183bcdb58962fde2f454
parent 5d90da24072a917af37b6e21bab3489754150bd8
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Wed, 24 Jul 2024 21:41:58 +0200

Daily Problem

Diffstat:
AProblems/2191.cpp | 32++++++++++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 33 insertions(+), 0 deletions(-)

diff --git a/Problems/2191.cpp b/Problems/2191.cpp @@ -0,0 +1,32 @@ +class Solution { + public: + vector<int> sortJumbled(const vector<int> &mapping, vector<int> &nums) const { + const auto shuffle = [&](int a) { + if (a == 0) return mapping[0]; + + int res = 0, acc = 1; + while (a > 0) { + const int digit = a % 10; + res += acc * mapping[digit]; + acc *= 10; + a /= 10; + } + + return res; + }; + + static int idxs[30001], tmp[30001]; + const int n = size(nums); + + for (int i = 0; i < n; i++) + tmp[i] = shuffle(nums[i]); + + iota(idxs, idxs + n, 0); + sort(idxs, idxs + n, [&](int a, int b) { return tmp[a] != tmp[b] ? tmp[a] < tmp[b] : a < b; }); + + for (int i = 0; i < n; i++) + tmp[i] = nums[idxs[i]]; + + return vector(tmp, tmp + n); + } +}; diff --git a/README.md b/README.md @@ -1092,6 +1092,7 @@ for solving problems. | 2182 | Medium | [Construct String With Repeat Limit](Problems/2182.cpp) | | 2186 | Medium | [Minimum Number of Steps to Make Two Strings Anagram II](Problems/2186.cpp) | | 2187 | Medium | [Minimum Time to Complete Trips](Problems/2187.cpp) | +| 2191 | Medium | [Sort the Jumbled Numbers](Problems/2191.cpp) | | 2192 | Medium | [All Ancestors of a Node in a Directed Acyclic Graph](Problems/2192.cpp) | | 2196 | Medium | [Create Binary Tree From Descriptions](Problems/2196.cpp) | | 2201 | Medium | [Count Artifacts That Can Be Extracted](Problems/2201.cpp) |