Skip to content

Commit

Permalink
Solved (?) wiggle sort II
Browse files Browse the repository at this point in the history
  • Loading branch information
orhtej2 committed Sep 17, 2024
1 parent d68613d commit d0c2a05
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ add_subdirectory( valid-parentheses )
add_subdirectory( convert-1d-array-into-2d-array )
add_subdirectory( kth-largest-element-in-a-stream )
add_subdirectory( kth-largest-element-in-a-array )
add_subdirectory( wiggle-sort-ii )
24 changes: 24 additions & 0 deletions wiggle-sort-ii/CMakeLists.txt
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)
5 changes: 5 additions & 0 deletions wiggle-sort-ii/README.md
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.
16 changes: 16 additions & 0 deletions wiggle-sort-ii/runner.cpp
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;
}
32 changes: 32 additions & 0 deletions wiggle-sort-ii/solution.hpp
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++;
}
}
}
};
53 changes: 53 additions & 0 deletions wiggle-sort-ii/tests.cpp
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 })
)
);

0 comments on commit d0c2a05

Please sign in to comment.