-
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.
- Loading branch information
Showing
6 changed files
with
131 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
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,24 @@ | ||
project(wiggle_sort_ii) | ||
|
||
add_executable( | ||
wiggle_sort_ii_tests | ||
tests.cpp | ||
) | ||
|
||
add_executable( | ||
wiggle_sort_ii_runner | ||
runner.cpp | ||
) | ||
|
||
target_link_libraries( | ||
wiggle_sort_ii_tests | ||
utils | ||
GTest::gtest_main | ||
) | ||
|
||
target_link_libraries( | ||
wiggle_sort_ii_runner | ||
utils | ||
) | ||
|
||
gtest_discover_tests(wiggle_sort_ii_tests) |
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,5 @@ | ||
# 324. Wiggle Sort II | ||
|
||
[LeetCode link](https://leetcode.com/problems/wiggle-sort-ii/) | ||
|
||
Solution is based on modified [Dutch national flag problem](https://en.wikipedia.org/wiki/Dutch_national_flag_problem#Pseudocode)-based solution suggested in Solutions section. |
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,16 @@ | ||
#include <iostream> | ||
#include <vector> | ||
|
||
#include "print_vector.hpp" | ||
|
||
#include "solution.hpp" | ||
|
||
int main() | ||
{ | ||
Solution s; | ||
std::vector<int> input = { 1, 2, 3, 4 }; | ||
std::cout << input << std::endl; | ||
s.wiggleSort(input); | ||
std::cout << input << std::endl; | ||
return 0; | ||
} |
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,32 @@ | ||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <vector> | ||
#include <queue> | ||
|
||
class Solution { | ||
public: | ||
void wiggleSort(std::vector<int>& nums) { | ||
auto len = std::distance(std::begin(nums), std::end(nums)); | ||
auto mid_it = std::begin(nums); | ||
std::advance(mid_it, len / 2); | ||
std::nth_element(std::begin(nums), mid_it, std::end(nums)); | ||
auto mid = *mid_it; | ||
|
||
#define GET(X) nums[(1 + 2*X) % (len|1)] | ||
|
||
size_t i = 0, j = 0, k = len - 1; | ||
while (j <= k) { | ||
if (GET(j) > mid) { | ||
std::swap(GET(i), GET(j)); | ||
i++; | ||
j++; | ||
} else if (GET(j) < mid) { | ||
std::swap(GET(j), GET(k)); | ||
k--; | ||
} else { | ||
j++; | ||
} | ||
} | ||
} | ||
}; |
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,53 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include <algorithm> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include "print_vector.hpp" | ||
|
||
#include "solution.hpp" | ||
|
||
typedef std::vector<int> ll; | ||
typedef ll::size_type ll_size_type; | ||
|
||
class KthLargestInArrayTest :public ::testing::TestWithParam<std::tuple<ll>> { | ||
}; | ||
|
||
void check(const ll& v) { | ||
for (ll_size_type i = 1; i < v.size(); i += 2) { | ||
ASSERT_LT(v[i - 1], v[i]) << v; | ||
if (i < v.size() - 1) { | ||
ASSERT_LT(v[i + 1], v[i]) << v; | ||
} | ||
} | ||
} | ||
|
||
TEST_P(KthLargestInArrayTest, CheckSolution) { | ||
auto param = GetParam(); | ||
ll input = std::get<0>(param); | ||
|
||
Solution solution; | ||
solution.wiggleSort(input); | ||
|
||
check(input); | ||
|
||
for (int i = 0; i < 20; ++i) { | ||
std::random_shuffle(input.begin(), input.end()); | ||
solution.wiggleSort(input); | ||
|
||
check(input); | ||
} | ||
} | ||
|
||
INSTANTIATE_TEST_SUITE_P( | ||
KthLargestInArrayTests, | ||
KthLargestInArrayTest, | ||
::testing::Values( | ||
std::tuple<ll>({ 1, 2, 3 }), | ||
std::tuple<ll>({ 3, 1 }), | ||
std::tuple<ll>({ 1 }), | ||
std::tuple<ll>({ 1, 5, 1, 1, 6, 4 }), | ||
std::tuple<ll>({ 1, 3, 2, 2, 3, 1 }) | ||
) | ||
); |