-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Largest Number After Digit Swaps by Parity
- Loading branch information
Showing
4 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
solutions/largest-number-after-digit-swaps-by-parity/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_catch(test_largest_number_after_digit_swaps_by_parity test.cpp) |
33 changes: 33 additions & 0 deletions
33
solutions/largest-number-after-digit-swaps-by-parity/solution.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include <queue> | ||
#include <vector> | ||
|
||
class Solution { | ||
public: | ||
static int largestInteger(int num) { | ||
const auto digits = getDigits(num); | ||
|
||
std::vector<std::priority_queue<int>> 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<int> getDigits(int n) { | ||
std::vector<int> digits; | ||
for (int i = n; i; i /= 10) { | ||
digits.push_back(i % 10); | ||
} | ||
std::reverse(digits.begin(), digits.end()); | ||
return digits; | ||
} | ||
}; |
26 changes: 26 additions & 0 deletions
26
solutions/largest-number-after-digit-swaps-by-parity/test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <catch.hpp> | ||
|
||
#include <solution.hpp> | ||
|
||
TEST_CASE("Simple") { | ||
struct TestCase { | ||
int num; | ||
int expected; | ||
}; | ||
|
||
std::vector<TestCase> 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); | ||
} | ||
} |