Skip to content

Commit

Permalink
Implemented RankTransformArray algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
BiagioFesta committed May 25, 2021
1 parent 0e1a86f commit 4996b4c
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ target_sources(${PROJECT_NAME}
${PROJECT_SOURCE_DIR}/src/Array/Partition.cpp
${PROJECT_SOURCE_DIR}/src/Array/PlusOne.cpp
${PROJECT_SOURCE_DIR}/src/Array/RadixSort.cpp
${PROJECT_SOURCE_DIR}/src/Array/RankTransformArray.cpp
${PROJECT_SOURCE_DIR}/src/Array/RemoveDuplicatesSortedArrayII.cpp
${PROJECT_SOURCE_DIR}/src/Array/RotateRight.cpp
${PROJECT_SOURCE_DIR}/src/Array/Search2DMatrix.cpp
Expand Down Expand Up @@ -228,6 +229,7 @@ add_executable(${PROJECT_NAME}_test
${PROJECT_SOURCE_DIR}/test/Array/testPartition.cpp
${PROJECT_SOURCE_DIR}/test/Array/testPlusOne.cpp
${PROJECT_SOURCE_DIR}/test/Array/testRadixSort.cpp
${PROJECT_SOURCE_DIR}/test/Array/testRankTransformArray.cpp
${PROJECT_SOURCE_DIR}/test/Array/testRemoveDuplicatesSortedArrayII.cpp
${PROJECT_SOURCE_DIR}/test/Array/testRotateRight.cpp
${PROJECT_SOURCE_DIR}/test/Array/testSearch2DMatrix.cpp
Expand Down
2 changes: 2 additions & 0 deletions benchmark/benchmarkTemplateTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <benchmark/benchmark.h>
#include <algorithms/Array/CountSmallerAfterSelf.hpp>
#include <algorithms/Array/DeckIncreasingReveal.hpp>
#include <algorithms/Array/RankTransformArray.hpp>
#include <algorithms/Array/ValidateStackSequences.hpp>
#include <algorithms/BackTracking/BeautifulArrangement.hpp>
#include <algorithms/BackTracking/NumbersConsecutiveDifferences.hpp>
Expand Down Expand Up @@ -102,5 +103,6 @@ GENERATE_BENCHMARK(PalindromePartitioningII,
GENERATE_BENCHMARK(CountSmallerAfterSelf, SeqList<1024>());
GENERATE_BENCHMARK(NextGreaterElementIII, 1999999999);
GENERATE_BENCHMARK(ValidPalindrome, "A man, a plan, a canal: Panama");
GENERATE_BENCHMARK(RankTransformArray, {37, 12, 28, 9, 100, 56, 80, 5, 12});

} // namespace algorithms::benchmark
3 changes: 2 additions & 1 deletion docs/GenTable/ProblemsListTable.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Problems List
![Easy_counter](https://img.shields.io/badge/Easy-18-green.svg?style=for-the-badge&logo=c%2b%2b)
![Easy_counter](https://img.shields.io/badge/Easy-19-green.svg?style=for-the-badge&logo=c%2b%2b)
![Medium_counter](https://img.shields.io/badge/Medium-94-yellow.svg?style=for-the-badge&logo=c%2b%2b)
![Hard_counter](https://img.shields.io/badge/Hard-15-red.svg?style=for-the-badge&logo=c%2b%2b)
![Undefined_counter](https://img.shields.io/badge/Undefined-36-lightgray.svg?style=for-the-badge&logo=c%2b%2b)
Expand Down Expand Up @@ -132,6 +132,7 @@
| [Plus One](https://leetcode.com/problems/plus-one/description/) | ![Easy](https://img.shields.io/badge/Difficulty-Easy-green.svg) | [Source](https://github.com/BiagioFesta/algorithms/blob/master/src/Array/PlusOne.cpp) |
| [Priority queue](https://en.wikipedia.org/wiki/Priority_queue) | ![Not Defined](https://img.shields.io/badge/Difficulty-ND-lightgray.svg) | [Source](https://github.com/BiagioFesta/algorithms/blob/master/include/algorithms/PriorityQueue/PriorityQueue.hpp) |
| [Radix sort](https://en.wikipedia.org/wiki/Radix_sort) | ![Not Defined](https://img.shields.io/badge/Difficulty-ND-lightgray.svg) | [Source](https://github.com/BiagioFesta/algorithms/blob/master/src/Array/RadixSort.cpp) |
| [Rank Transform of an Array](https://leetcode.com/problems/rank-transform-of-an-array/) | ![Easy](https://img.shields.io/badge/Difficulty-Easy-green.svg) | [Source](https://github.com/BiagioFesta/algorithms/blob/master/src/Array/RankTransformArray.cpp) |
| [Reconstruct Itinerary](https://leetcode.com/problems/reconstruct-itinerary/) | ![Medium](https://img.shields.io/badge/Difficulty-Medium-yellow.svg) | [Source](https://github.com/BiagioFesta/algorithms/blob/master/src/Misc/FindItinerary.cpp) |
| [Remove Duplicates]() | ![Not Defined](https://img.shields.io/badge/Difficulty-ND-lightgray.svg) | [Source](https://github.com/BiagioFesta/algorithms/blob/master/src/CTCI/Chapter2/RemoveDuplicates.cpp) |
| [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/) | ![Medium](https://img.shields.io/badge/Difficulty-Medium-yellow.svg) | [Source](https://github.com/BiagioFesta/algorithms/blob/master/src/Array/RemoveDuplicatesSortedArrayII.cpp) |
Expand Down
40 changes: 40 additions & 0 deletions include/algorithms/Array/RankTransformArray.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright (C) 2020 Biagio Festa
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ALGORITHMS__ARRAY__RANKTRANSFORMARRAY__HPP
#define ALGORITHMS__ARRAY__RANKTRANSFORMARRAY__HPP
#include <algorithm>
#include <vector>

namespace algorithms {

/*! \brief Given an array of integers arr, replace each element with its rank.
* The rank represents how large the element is. The rank has the following
* rules:
* - Rank is an integer starting from 1.
* - The larger the element, the larger the rank. If two elements are equal,
* their rank must be the same.
* - Rank should be as small as possible.
*
* \note Complexity --> Time: O(NlogN) Space: O(N)
* \note https://leetcode.com/problems/rank-transform-of-an-array/
*/
std::vector<int> RankTransformArray(const std::vector<int>& v);

} // namespace algorithms

#endif // ALGORITHMS__ARRAY__RANKTRANSFORMARRAY__HPP
46 changes: 46 additions & 0 deletions src/Array/RankTransformArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright (C) 2020 Biagio Festa
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <algorithms/Array/RankTransformArray.hpp>
#include <vector>

namespace algorithms {

std::vector<int> RankTransformArray(const std::vector<int>& v) {
const std::size_t size = v.size();

std::vector<std::pair<int, std::size_t>> meta;
meta.reserve(size);
for (const int n : v) { // O(N)
meta.emplace_back(n, meta.size());
}
std::sort(meta.begin(), meta.end()); // O(NlogN)

std::vector<int> rtn(size);
int counter = 1;
for (std::size_t i = 0; i < size; ++i) { // O(N)
if (i > 0 && meta[i].first != meta[i - 1].first) {
++counter;
}
rtn[meta[i].second] = counter;
}

return rtn;
}

} // namespace algorithms
37 changes: 37 additions & 0 deletions test/Array/testRankTransformArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright (C) 2020 Biagio Festa
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gtest/gtest.h>
#include <algorithms/Array/RankTransformArray.hpp>
#include <utility>
#include <vector>

namespace algorithms::test {

TEST(Array, RankTransformArray) {
using Test = std::pair<std::vector<int>, std::vector<int>>;
const std::vector<Test> testCases = {
{{40, 10, 20, 30}, {4, 1, 2, 3}},
{{100, 100, 100}, {1, 1, 1}},
{{37, 12, 28, 9, 100, 56, 80, 5, 12}, {5, 3, 4, 2, 8, 6, 7, 1, 3}}};

for (const auto& [v, expt] : testCases) {
ASSERT_EQ(RankTransformArray(v), expt);
}
}

} // namespace algorithms::test

0 comments on commit 4996b4c

Please sign in to comment.