-
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.
Signed-off-by: Certseeds <[email protected]>
- Loading branch information
Showing
4 changed files
with
151 additions
and
5 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
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,54 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2023 nanoseeds | ||
*/ | ||
#include "leetcode_1366_test.hpp" | ||
|
||
namespace leetcode_1366 { | ||
|
||
string leetcode_1366::rankTeams(const vector<string> &votes) { | ||
if (votes.empty() || votes.front().empty()) { | ||
return {}; | ||
} | ||
struct sorted { | ||
std::vector<int32_t> vec_votes{}; | ||
char ch{'A'}; | ||
int16_t count{0}; | ||
}; | ||
const auto votes_size{votes.size()}; | ||
const auto vote_of_each{votes.front().size()}; | ||
constexpr const auto groups{26}; | ||
vector<sorted> voted_array(groups); | ||
for (size_t i{0}; i < groups; ++i) { | ||
voted_array[i].ch += i; | ||
voted_array[i].vec_votes.resize(vote_of_each); | ||
} | ||
for (size_t i{0}; i < vote_of_each; ++i) { | ||
for (size_t j{0}; j < votes_size; ++j) { | ||
const auto ch{votes[j][i] - 'A'}; | ||
voted_array[ch].vec_votes[i] += 1; | ||
voted_array[ch].count += 1; | ||
} | ||
} | ||
// or can filter count = 0 in there | ||
std::sort(voted_array.begin(), voted_array.end(), [vote_of_each](auto obj1, auto obj2) { | ||
for (size_t i{0}; i < vote_of_each; ++i) { | ||
if (obj1.vec_votes[i] != obj2.vec_votes[i]) { | ||
return obj1.vec_votes[i] > obj2.vec_votes[i]; | ||
} | ||
} | ||
return obj1.ch < obj2.ch; | ||
}); | ||
string will_return{}; | ||
for (const auto &voted: voted_array) { | ||
if (voted.count != 0) { | ||
will_return += voted.ch; | ||
} | ||
} | ||
return will_return; | ||
} | ||
|
||
} |
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,92 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2023 nanoseeds | ||
*/ | ||
//@Tag string | ||
//@Tag 字符串 | ||
|
||
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_STRING_LEETCODE_1366_TEST_HPP | ||
#define CS203_DSAA_TEMPLATE_ALGORITHM_STRING_LEETCODE_1366_TEST_HPP | ||
|
||
#include <catch_main.hpp> | ||
#include <cstdint> | ||
#include <cstddef> | ||
#include <string> | ||
|
||
namespace leetcode_1366 { | ||
using std::string; | ||
|
||
namespace leetcode_1366 { | ||
string rankTeams(const vector<string> &votes); | ||
|
||
} | ||
|
||
TEST_CASE("1 [test_1366]", "[test_1366]") { | ||
const vector<string> input{"ABC", "ACB", "ABC", "ACB", "ACB"}; | ||
const char *const result{"ACB"}; | ||
CHECK(result == leetcode_1366::rankTeams(input)); | ||
} | ||
|
||
TEST_CASE("2 [test_1366]", "[test_1366]") { | ||
const vector<string> input{"WXYZ", "XYZW"}; | ||
const char *const result{"XWYZ"}; | ||
CHECK(result == leetcode_1366::rankTeams(input)); | ||
} | ||
|
||
|
||
TEST_CASE("3 [test_1366]", "[test_1366]") { | ||
const vector<string> input{"ZMNAGUEDSJYLBOPHRQICWFXTVK"}; | ||
const char *const result{"ZMNAGUEDSJYLBOPHRQICWFXTVK"}; | ||
CHECK(result == leetcode_1366::rankTeams(input)); | ||
} | ||
|
||
|
||
TEST_CASE("4 [test_1366]", "[test_1366]") { | ||
const vector<string> input{"BCA", "CAB", "CBA", "ABC", "ACB", "BAC"}; | ||
const char *const result{"ABC"}; | ||
CHECK(result == leetcode_1366::rankTeams(input)); | ||
} | ||
|
||
|
||
TEST_CASE("5 [test_1366]", "[test_1366]") { | ||
const vector<string> input{"M", "M", "M", "M"}; | ||
const char *const result{"M"}; | ||
CHECK(result == leetcode_1366::rankTeams(input)); | ||
} | ||
|
||
TEST_CASE("6 [test_1366]", "[test_1366]") { | ||
const vector<string> input{ "FVSHJIEMNGYPTQOURLWCZKAX", | ||
"AITFQORCEHPVJMXGKSLNZWUY", | ||
"OTERVXFZUMHNIYSCQAWGPKJL", | ||
"VMSERIJYLZNWCPQTOKFUHAXG", | ||
"VNHOZWKQCEFYPSGLAMXJIUTR", | ||
"ANPHQIJMXCWOSKTYGULFVERZ", | ||
"RFYUXJEWCKQOMGATHZVILNSP", | ||
"SCPYUMQJTVEXKRNLIOWGHAFZ", | ||
"VIKTSJCEYQGLOMPZWAHFXURN", | ||
"SVJICLXKHQZTFWNPYRGMEUAO", | ||
"JRCTHYKIGSXPOZLUQAVNEWFM", | ||
"NGMSWJITREHFZVQCUKXYAPOL", | ||
"WUXJOQKGNSYLHEZAFIPMRCVT", | ||
"PKYQIOLXFCRGHZNAMJVUTWES", | ||
"FERSGNMJVZXWAYLIKCPUQHTO", | ||
"HPLRIUQMTSGYJVAXWNOCZEKF", | ||
"JUVWPTEGCOFYSKXNRMHQALIZ", | ||
"MWPIAZCNSLEYRTHFKQXUOVGJ", | ||
"EZXLUNFVCMORSIWKTYHJAQPG", | ||
"HRQNLTKJFIEGMCSXAZPYOVUW", | ||
"LOHXVYGWRIJMCPSQENUAKTZF", | ||
"XKUTWPRGHOAQFLVYMJSNEIZC", | ||
"WTCRQMVKPHOSLGAXZUEFYNJI"}; | ||
const char *const result{"VWFHSJARNPEMOXLTUKICZGYQ"}; | ||
CHECK(result == leetcode_1366::rankTeams(input)); | ||
} | ||
|
||
|
||
|
||
|
||
} | ||
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_STRING_LEETCODE_1366_TEST_HPP |