-
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
5 changed files
with
94 additions
and
6 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
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,37 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2023 nanoseeds | ||
*/ | ||
#include "leetcode_1365_test.hpp" | ||
|
||
namespace leetcode_1365 { | ||
|
||
vector<int32_t> leetcode_1365::smallerNumbersThanCurrent(const vector<int32_t> &nums) { | ||
constexpr const auto max_num{100}; | ||
const auto nums_size{nums.size()}; | ||
for (const auto num: nums) { | ||
assert(num >= 0); | ||
assert(num <= max_num); | ||
} | ||
assert(nums_size >= 2); | ||
assert(nums_size <= 500); | ||
std::array<int32_t, max_num + 1> arrs{0,}; | ||
for (const auto num: nums) { | ||
arrs[num] += 1; | ||
} | ||
for (int32_t i{1}; i < max_num + 1; i++) { | ||
arrs[i] = arrs[i] + arrs[i - 1]; | ||
} | ||
vector<int32_t> will_return(nums_size, 0); | ||
for (size_t i{0}; i < nums_size; i++) { | ||
if (nums[i] != 0) { | ||
will_return[i] = arrs[nums[i] - 1]; | ||
} | ||
} | ||
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,51 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2023 nanoseeds | ||
*/ | ||
//@Tag array | ||
//@Tag 数组 | ||
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1365_TEST_HPP | ||
#define CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1365_TEST_HPP | ||
|
||
#include <catch_main.hpp> | ||
#include <cstdint> | ||
#include <cstddef> | ||
#include <vector> | ||
#include <string> | ||
|
||
namespace leetcode_1365 { | ||
using std::vector; | ||
|
||
namespace leetcode_1365 { | ||
vector<int32_t> smallerNumbersThanCurrent(const vector<int32_t> &nums); | ||
} | ||
|
||
using Catch::Matchers::Equals; | ||
|
||
TEST_CASE("test case 1-1 {test_1365}", "{test_1365}") { | ||
const vector<int32_t> nums{8, 1, 2, 2, 3}; | ||
const vector<int32_t> result{4, 0, 1, 1, 3}; | ||
CHECK_THAT(result, Equals(leetcode_1365::smallerNumbersThanCurrent(nums))); | ||
} | ||
|
||
TEST_CASE("test case 1-2 {test_1365}", "{test_1365}") { | ||
const vector<int32_t> nums{6, 5, 4, 8}; | ||
const vector<int32_t> result{2, 1, 0, 3}; | ||
CHECK_THAT(result, Equals(leetcode_1365::smallerNumbersThanCurrent(nums))); | ||
} | ||
|
||
TEST_CASE("test case 1-3 {test_1365}", "{test_1365}") { | ||
const vector<int32_t> nums{7, 7, 7, 7}; | ||
const vector<int32_t> result{0, 0, 0, 0}; | ||
CHECK_THAT(result, Equals(leetcode_1365::smallerNumbersThanCurrent(nums))); | ||
} | ||
TEST_CASE("test case 1-4 {test_1365}", "{test_1365}") { | ||
const vector<int32_t> nums{0, 0, 0, 0}; | ||
const vector<int32_t> result{0, 0, 0, 0}; | ||
CHECK_THAT(result, Equals(leetcode_1365::smallerNumbersThanCurrent(nums))); | ||
} | ||
} | ||
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_ARRAY_LEETCODE_1365_TEST_HPP |