From c56fb6afd31770866ed9d7380f63c0153d0532c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=BE=D1=80=D0=B5=D0=B2=20=D0=90=D0=BB=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B4=D1=80?= Date: Thu, 19 Oct 2023 03:12:34 +0300 Subject: [PATCH] Largest Number After Digit Swaps by Parity --- CMakeLists.txt | 1 + .../CMakeLists.txt | 1 + .../solution.hpp | 33 +++++++++++++++++++ .../test.cpp | 26 +++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 solutions/largest-number-after-digit-swaps-by-parity/CMakeLists.txt create mode 100644 solutions/largest-number-after-digit-swaps-by-parity/solution.hpp create mode 100644 solutions/largest-number-after-digit-swaps-by-parity/test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d7bcfff7..069cfaff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -322,6 +322,7 @@ add_task(kth-smallest-element-in-a-bst) add_task(largest-3-same-digit-number-in-string) add_task(largest-color-value-in-a-directed-graph) add_task(largest-local-values-in-a-matrix) +add_task(largest-number-after-digit-swaps-by-parity) add_task(largest-perimeter-triangle) add_task(largest-positive-integer-that-exists-with-its-negative) add_task(largest-rectangle-in-histogram) diff --git a/solutions/largest-number-after-digit-swaps-by-parity/CMakeLists.txt b/solutions/largest-number-after-digit-swaps-by-parity/CMakeLists.txt new file mode 100644 index 00000000..fd490573 --- /dev/null +++ b/solutions/largest-number-after-digit-swaps-by-parity/CMakeLists.txt @@ -0,0 +1 @@ +add_catch(test_largest_number_after_digit_swaps_by_parity test.cpp) diff --git a/solutions/largest-number-after-digit-swaps-by-parity/solution.hpp b/solutions/largest-number-after-digit-swaps-by-parity/solution.hpp new file mode 100644 index 00000000..3bffbbab --- /dev/null +++ b/solutions/largest-number-after-digit-swaps-by-parity/solution.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include +#include + +class Solution { +public: + static int largestInteger(int num) { + const auto digits = getDigits(num); + + std::vector> heaps(2); + for (auto digit : digits) { + heaps[digit & 1].push(digit); + } + + int ans = 0; + for (auto digit : digits) { + ans = ans * 10 + heaps[digit & 1].top(); + heaps[digit & 1].pop(); + } + return ans; + } + +private: + static std::vector getDigits(int n) { + std::vector digits; + for (int i = n; i; i /= 10) { + digits.push_back(i % 10); + } + std::reverse(digits.begin(), digits.end()); + return digits; + } +}; diff --git a/solutions/largest-number-after-digit-swaps-by-parity/test.cpp b/solutions/largest-number-after-digit-swaps-by-parity/test.cpp new file mode 100644 index 00000000..ef2cdb29 --- /dev/null +++ b/solutions/largest-number-after-digit-swaps-by-parity/test.cpp @@ -0,0 +1,26 @@ +#include + +#include + +TEST_CASE("Simple") { + struct TestCase { + int num; + int expected; + }; + + std::vector test_cases{ + { + .num = 1234, + .expected = 3412, + }, + { + .num = 65875, + .expected = 87655, + }, + }; + + for (const auto &[num, expected] : test_cases) { + const auto actual = Solution::largestInteger(num); + REQUIRE(expected == actual); + } +}