Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve Problem 1051. Height Checker #1047

Merged
merged 2 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ LeetSpace serves as a dedicated workspace and archive for my [LeetCode](https://
| [1026. Maximum Difference Between Node and Ancestor](https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/) | Medium | [C++](./old-problems/1026/solution.cpp) |
| [1043. Partition Array for Maximum Sum](https://leetcode.com/problems/partition-array-for-maximum-sum/) | Medium | [C](./old-problems/1043/c/solution.c) [C++](./old-problems/1043/solution.cpp) |
| [1048. Longest String Chain](https://leetcode.com/problems/longest-string-chain) | Medium | [C++](./problems/1048/solution.cpp) |
| [1051. Height Checker](https://leetcode.com/problems/height-checker/) | Easy | [C++](./old-problems/1051/solution.cpp) |
| [1081. Smallest Subsequence of Distinct Characters](https://leetcode.com/problems/smallest-subsequence-of-distinct-characters) | Medium | [C++](./problems/1081/solution.cpp) |
| [1137. N-th Tribonacci Number](https://leetcode.com/problems/n-th-tribonacci-number/) | Easy | [C](./old-problems/1137/c/solution.c) [C++](./old-problems/1137/solution.cpp) |
| [1143. Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/) | Medium | [C](./old-problems/1143/c/solution.c) [C++](./old-problems/1143/solution.cpp) |
Expand Down
2 changes: 2 additions & 0 deletions old-problems/1051/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
get_dir_name(id)
add_problem_test(test-${id} test.yaml)
17 changes: 17 additions & 0 deletions old-problems/1051/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <algorithm>
#include <vector>

class Solution {
public:
int heightChecker(std::vector<int>& heights) {
std::vector<int> sortedHeights{heights};
std::sort(sortedHeights.begin(), sortedHeights.end());

int diff{0};
for (int i = heights.size() - 1; i >= 0; --i) {
if (heights[i] != sortedHeights[i]) ++diff;
}

return diff;
}
};
26 changes: 26 additions & 0 deletions old-problems/1051/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: 1051. Height Checker

types:
inputs:
heights: std::vector<int>
output: int

solutions:
cpp:
function: heightChecker

test_cases:
example_1:
inputs:
heights: [1, 1, 4, 2, 1, 3]
output: 3

example_2:
inputs:
heights: [5, 1, 2, 3, 4]
output: 5

example_3:
inputs:
heights: [1, 2, 3, 4, 5]
output: 0