cpp(chore): add clang-format style and format
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
2daade49c0
commit
b229608723
50 changed files with 870 additions and 846 deletions
235
cpp/.clang-format
Normal file
235
cpp/.clang-format
Normal file
|
@ -0,0 +1,235 @@
|
||||||
|
---
|
||||||
|
Language: Cpp
|
||||||
|
AccessModifierOffset: -2
|
||||||
|
AlignAfterOpenBracket: Align
|
||||||
|
AlignArrayOfStructures: None
|
||||||
|
AlignConsecutiveAssignments:
|
||||||
|
Enabled: false
|
||||||
|
AcrossEmptyLines: false
|
||||||
|
AcrossComments: false
|
||||||
|
AlignCompound: false
|
||||||
|
PadOperators: true
|
||||||
|
AlignConsecutiveBitFields:
|
||||||
|
Enabled: false
|
||||||
|
AcrossEmptyLines: false
|
||||||
|
AcrossComments: false
|
||||||
|
AlignCompound: false
|
||||||
|
PadOperators: false
|
||||||
|
AlignConsecutiveDeclarations:
|
||||||
|
Enabled: false
|
||||||
|
AcrossEmptyLines: false
|
||||||
|
AcrossComments: false
|
||||||
|
AlignCompound: false
|
||||||
|
PadOperators: false
|
||||||
|
AlignConsecutiveMacros:
|
||||||
|
Enabled: false
|
||||||
|
AcrossEmptyLines: false
|
||||||
|
AcrossComments: false
|
||||||
|
AlignCompound: false
|
||||||
|
PadOperators: false
|
||||||
|
AlignConsecutiveShortCaseStatements:
|
||||||
|
Enabled: false
|
||||||
|
AcrossEmptyLines: false
|
||||||
|
AcrossComments: false
|
||||||
|
AlignCaseColons: false
|
||||||
|
AlignEscapedNewlines: Right
|
||||||
|
AlignOperands: Align
|
||||||
|
AlignTrailingComments:
|
||||||
|
Kind: Always
|
||||||
|
OverEmptyLines: 0
|
||||||
|
AllowAllArgumentsOnNextLine: true
|
||||||
|
AllowAllParametersOfDeclarationOnNextLine: true
|
||||||
|
AllowShortBlocksOnASingleLine: Never
|
||||||
|
AllowShortCaseLabelsOnASingleLine: false
|
||||||
|
AllowShortEnumsOnASingleLine: true
|
||||||
|
AllowShortFunctionsOnASingleLine: All
|
||||||
|
AllowShortIfStatementsOnASingleLine: Never
|
||||||
|
AllowShortLambdasOnASingleLine: All
|
||||||
|
AllowShortLoopsOnASingleLine: false
|
||||||
|
AlwaysBreakAfterDefinitionReturnType: None
|
||||||
|
AlwaysBreakAfterReturnType: None
|
||||||
|
AlwaysBreakBeforeMultilineStrings: false
|
||||||
|
AlwaysBreakTemplateDeclarations: MultiLine
|
||||||
|
AttributeMacros:
|
||||||
|
- __capability
|
||||||
|
BinPackArguments: true
|
||||||
|
BinPackParameters: true
|
||||||
|
BitFieldColonSpacing: Both
|
||||||
|
BraceWrapping:
|
||||||
|
AfterCaseLabel: false
|
||||||
|
AfterClass: false
|
||||||
|
AfterControlStatement: Never
|
||||||
|
AfterEnum: false
|
||||||
|
AfterExternBlock: false
|
||||||
|
AfterFunction: false
|
||||||
|
AfterNamespace: false
|
||||||
|
AfterObjCDeclaration: false
|
||||||
|
AfterStruct: false
|
||||||
|
AfterUnion: false
|
||||||
|
BeforeCatch: false
|
||||||
|
BeforeElse: false
|
||||||
|
BeforeLambdaBody: false
|
||||||
|
BeforeWhile: false
|
||||||
|
IndentBraces: false
|
||||||
|
SplitEmptyFunction: true
|
||||||
|
SplitEmptyRecord: true
|
||||||
|
SplitEmptyNamespace: true
|
||||||
|
BreakAfterAttributes: Never
|
||||||
|
BreakAfterJavaFieldAnnotations: false
|
||||||
|
BreakArrays: true
|
||||||
|
BreakBeforeBinaryOperators: None
|
||||||
|
BreakBeforeConceptDeclarations: Always
|
||||||
|
BreakBeforeBraces: Attach
|
||||||
|
BreakBeforeInlineASMColon: OnlyMultiline
|
||||||
|
BreakBeforeTernaryOperators: true
|
||||||
|
BreakConstructorInitializers: BeforeColon
|
||||||
|
BreakInheritanceList: BeforeColon
|
||||||
|
BreakStringLiterals: true
|
||||||
|
ColumnLimit: 80
|
||||||
|
CommentPragmas: '^ IWYU pragma:'
|
||||||
|
CompactNamespaces: false
|
||||||
|
ConstructorInitializerIndentWidth: 4
|
||||||
|
ContinuationIndentWidth: 4
|
||||||
|
Cpp11BracedListStyle: true
|
||||||
|
DerivePointerAlignment: false
|
||||||
|
DisableFormat: false
|
||||||
|
EmptyLineAfterAccessModifier: Never
|
||||||
|
EmptyLineBeforeAccessModifier: LogicalBlock
|
||||||
|
ExperimentalAutoDetectBinPacking: false
|
||||||
|
FixNamespaceComments: true
|
||||||
|
ForEachMacros:
|
||||||
|
- foreach
|
||||||
|
- Q_FOREACH
|
||||||
|
- BOOST_FOREACH
|
||||||
|
IfMacros:
|
||||||
|
- KJ_IF_MAYBE
|
||||||
|
IncludeBlocks: Preserve
|
||||||
|
IncludeCategories:
|
||||||
|
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
|
||||||
|
Priority: 2
|
||||||
|
SortPriority: 0
|
||||||
|
CaseSensitive: false
|
||||||
|
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
|
||||||
|
Priority: 3
|
||||||
|
SortPriority: 0
|
||||||
|
CaseSensitive: false
|
||||||
|
- Regex: '.*'
|
||||||
|
Priority: 1
|
||||||
|
SortPriority: 0
|
||||||
|
CaseSensitive: false
|
||||||
|
IncludeIsMainRegex: '(Test)?$'
|
||||||
|
IncludeIsMainSourceRegex: ''
|
||||||
|
IndentAccessModifiers: false
|
||||||
|
IndentCaseBlocks: false
|
||||||
|
IndentCaseLabels: false
|
||||||
|
IndentExternBlock: AfterExternBlock
|
||||||
|
IndentGotoLabels: true
|
||||||
|
IndentPPDirectives: None
|
||||||
|
IndentRequiresClause: true
|
||||||
|
IndentWidth: 4
|
||||||
|
IndentWrappedFunctionNames: false
|
||||||
|
InsertBraces: false
|
||||||
|
InsertNewlineAtEOF: false
|
||||||
|
InsertTrailingCommas: None
|
||||||
|
IntegerLiteralSeparator:
|
||||||
|
Binary: 0
|
||||||
|
BinaryMinDigits: 0
|
||||||
|
Decimal: 0
|
||||||
|
DecimalMinDigits: 0
|
||||||
|
Hex: 0
|
||||||
|
HexMinDigits: 0
|
||||||
|
JavaScriptQuotes: Leave
|
||||||
|
JavaScriptWrapImports: true
|
||||||
|
KeepEmptyLinesAtTheStartOfBlocks: true
|
||||||
|
KeepEmptyLinesAtEOF: false
|
||||||
|
LambdaBodyIndentation: Signature
|
||||||
|
LineEnding: DeriveLF
|
||||||
|
MacroBlockBegin: ''
|
||||||
|
MacroBlockEnd: ''
|
||||||
|
MaxEmptyLinesToKeep: 1
|
||||||
|
NamespaceIndentation: None
|
||||||
|
ObjCBinPackProtocolList: Auto
|
||||||
|
ObjCBlockIndentWidth: 2
|
||||||
|
ObjCBreakBeforeNestedBlockParam: true
|
||||||
|
ObjCSpaceAfterProperty: false
|
||||||
|
ObjCSpaceBeforeProtocolList: true
|
||||||
|
PackConstructorInitializers: BinPack
|
||||||
|
PenaltyBreakAssignment: 2
|
||||||
|
PenaltyBreakBeforeFirstCallParameter: 19
|
||||||
|
PenaltyBreakComment: 300
|
||||||
|
PenaltyBreakFirstLessLess: 120
|
||||||
|
PenaltyBreakOpenParenthesis: 0
|
||||||
|
PenaltyBreakString: 1000
|
||||||
|
PenaltyBreakTemplateDeclaration: 10
|
||||||
|
PenaltyExcessCharacter: 1000000
|
||||||
|
PenaltyIndentedWhitespace: 0
|
||||||
|
PenaltyReturnTypeOnItsOwnLine: 60
|
||||||
|
PointerAlignment: Right
|
||||||
|
PPIndentWidth: -1
|
||||||
|
QualifierAlignment: Leave
|
||||||
|
ReferenceAlignment: Pointer
|
||||||
|
ReflowComments: true
|
||||||
|
RemoveBracesLLVM: false
|
||||||
|
RemoveParentheses: Leave
|
||||||
|
RemoveSemicolon: false
|
||||||
|
RequiresClausePosition: OwnLine
|
||||||
|
RequiresExpressionIndentation: OuterScope
|
||||||
|
SeparateDefinitionBlocks: Leave
|
||||||
|
ShortNamespaceLines: 1
|
||||||
|
SortIncludes: CaseSensitive
|
||||||
|
SortJavaStaticImport: Before
|
||||||
|
SortUsingDeclarations: LexicographicNumeric
|
||||||
|
SpaceAfterCStyleCast: false
|
||||||
|
SpaceAfterLogicalNot: false
|
||||||
|
SpaceAfterTemplateKeyword: true
|
||||||
|
SpaceAroundPointerQualifiers: Default
|
||||||
|
SpaceBeforeAssignmentOperators: true
|
||||||
|
SpaceBeforeCaseColon: false
|
||||||
|
SpaceBeforeCpp11BracedList: false
|
||||||
|
SpaceBeforeCtorInitializerColon: true
|
||||||
|
SpaceBeforeInheritanceColon: true
|
||||||
|
SpaceBeforeJsonColon: false
|
||||||
|
SpaceBeforeParens: ControlStatements
|
||||||
|
SpaceBeforeParensOptions:
|
||||||
|
AfterControlStatements: true
|
||||||
|
AfterForeachMacros: true
|
||||||
|
AfterFunctionDefinitionName: false
|
||||||
|
AfterFunctionDeclarationName: false
|
||||||
|
AfterIfMacros: true
|
||||||
|
AfterOverloadedOperator: false
|
||||||
|
AfterRequiresInClause: false
|
||||||
|
AfterRequiresInExpression: false
|
||||||
|
BeforeNonEmptyParentheses: false
|
||||||
|
SpaceBeforeRangeBasedForLoopColon: true
|
||||||
|
SpaceBeforeSquareBrackets: false
|
||||||
|
SpaceInEmptyBlock: false
|
||||||
|
SpacesBeforeTrailingComments: 1
|
||||||
|
SpacesInAngles: Never
|
||||||
|
SpacesInContainerLiterals: true
|
||||||
|
SpacesInLineCommentPrefix:
|
||||||
|
Minimum: 1
|
||||||
|
Maximum: -1
|
||||||
|
SpacesInParens: Never
|
||||||
|
SpacesInParensOptions:
|
||||||
|
InCStyleCasts: false
|
||||||
|
InConditionalStatements: false
|
||||||
|
InEmptyParentheses: false
|
||||||
|
Other: false
|
||||||
|
SpacesInSquareBrackets: false
|
||||||
|
Standard: Latest
|
||||||
|
StatementAttributeLikeMacros:
|
||||||
|
- Q_EMIT
|
||||||
|
StatementMacros:
|
||||||
|
- Q_UNUSED
|
||||||
|
- QT_REQUIRE_VERSION
|
||||||
|
TabWidth: 8
|
||||||
|
UseTab: Never
|
||||||
|
VerilogBreakBetweenInstancePorts: true
|
||||||
|
WhitespaceSensitiveMacros:
|
||||||
|
- BOOST_PP_STRINGIZE
|
||||||
|
- CF_SWIFT_NAME
|
||||||
|
- NS_SWIFT_NAME
|
||||||
|
- PP_STRINGIZE
|
||||||
|
- STRINGIZE
|
||||||
|
...
|
||||||
|
|
|
@ -18,4 +18,3 @@ public:
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,7 @@
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
int numRescueBoats(std::vector<int> people, int limit)
|
int numRescueBoats(std::vector<int> people, int limit) {
|
||||||
{
|
|
||||||
std::sort(people.begin(), people.end());
|
std::sort(people.begin(), people.end());
|
||||||
|
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
|
|
|
@ -5,8 +5,7 @@ using std::vector;
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
bool canPlaceFlowers(vector<int>& flowerbed, int n)
|
bool canPlaceFlowers(vector<int> &flowerbed, int n) {
|
||||||
{
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
int left = 0, right;
|
int left = 0, right;
|
||||||
|
@ -25,8 +24,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
Solution s;
|
Solution s;
|
||||||
|
|
||||||
std::vector flowers{1, 0, 0, 0, 1};
|
std::vector flowers{1, 0, 0, 0, 1};
|
||||||
|
|
|
@ -4,8 +4,7 @@ class Solution {
|
||||||
static const int MOD = 1000000007;
|
static const int MOD = 1000000007;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
int concatenatedBinary(int n)
|
int concatenatedBinary(int n) {
|
||||||
{
|
|
||||||
long joined_number = 0;
|
long joined_number = 0;
|
||||||
|
|
||||||
int padding = 1;
|
int padding = 1;
|
||||||
|
@ -21,8 +20,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
Solution s;
|
Solution s;
|
||||||
|
|
||||||
assert(s.concatenatedBinary(1) == 1);
|
assert(s.concatenatedBinary(1) == 1);
|
||||||
|
|
|
@ -32,4 +32,3 @@ public:
|
||||||
return matrix;
|
return matrix;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -6,23 +6,21 @@
|
||||||
* TreeNode *right;
|
* TreeNode *right;
|
||||||
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
|
||||||
|
* right(right) {}
|
||||||
* };
|
* };
|
||||||
*/
|
*/
|
||||||
class Solution {
|
class Solution {
|
||||||
int goodNodes(TreeNode* root, int m)
|
int goodNodes(TreeNode *root, int m) {
|
||||||
{
|
|
||||||
if (root == nullptr) {
|
if (root == nullptr) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int new_max = std::max(m, root->val);
|
int new_max = std::max(m, root->val);
|
||||||
return (root->val >= m) + goodNodes(root->left, new_max) + goodNodes(root->right, new_max);
|
return (root->val >= m) + goodNodes(root->left, new_max) +
|
||||||
|
goodNodes(root->right, new_max);
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
int goodNodes(TreeNode* root)
|
int goodNodes(TreeNode *root) { return goodNodes(root, INT_MIN); }
|
||||||
{
|
|
||||||
return goodNodes(root, INT_MIN);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -4,13 +4,13 @@
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
int countNegatives(const std::vector<std::vector<int>>& grid)
|
int countNegatives(const std::vector<std::vector<int>> &grid) {
|
||||||
{
|
|
||||||
auto last = 0;
|
auto last = 0;
|
||||||
|
|
||||||
auto negatives = 0;
|
auto negatives = 0;
|
||||||
for (const auto &row : grid) {
|
for (const auto &row : grid) {
|
||||||
auto first_positive = std::lower_bound(row.crbegin() + last, row.crend(), 0);
|
auto first_positive =
|
||||||
|
std::lower_bound(row.crbegin() + last, row.crend(), 0);
|
||||||
auto i = first_positive - row.crbegin();
|
auto i = first_positive - row.crbegin();
|
||||||
|
|
||||||
negatives += i;
|
negatives += i;
|
||||||
|
@ -20,11 +20,13 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
Solution s;
|
Solution s;
|
||||||
|
|
||||||
assert((s.countNegatives(std::vector { std::vector { 4, 3, 2, -1 }, std::vector { 3, 2, 1, -1 }, std::vector { 1, 1, -1, -2 }, std::vector { -1, -1, -2, -3 } }) == 8));
|
assert((s.countNegatives(std::vector{
|
||||||
assert((s.countNegatives(std::vector { std::vector { 3, 2 }, std::vector { 1, 0 } }) == 0));
|
std::vector{4, 3, 2, -1}, std::vector{3, 2, 1, -1},
|
||||||
|
std::vector{1, 1, -1, -2}, std::vector{-1, -1, -2, -3}}) == 8));
|
||||||
|
assert((s.countNegatives(
|
||||||
|
std::vector{std::vector{3, 2}, std::vector{1, 0}}) == 0));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,8 +5,7 @@ class ParkingSystem {
|
||||||
int medium;
|
int medium;
|
||||||
int small;
|
int small;
|
||||||
|
|
||||||
int& get(int carType)
|
int &get(int carType) {
|
||||||
{
|
|
||||||
switch (carType) {
|
switch (carType) {
|
||||||
case 1:
|
case 1:
|
||||||
return big;
|
return big;
|
||||||
|
@ -21,14 +20,9 @@ class ParkingSystem {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ParkingSystem(int big, int medium, int small)
|
ParkingSystem(int big, int medium, int small)
|
||||||
: big(big)
|
: big(big), medium(medium), small(small) {}
|
||||||
, medium(medium)
|
|
||||||
, small(small)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool addCar(int carType)
|
bool addCar(int carType) {
|
||||||
{
|
|
||||||
auto &space = get(carType);
|
auto &space = get(carType);
|
||||||
if (space <= 0) {
|
if (space <= 0) {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -5,8 +5,7 @@
|
||||||
class Solution {
|
class Solution {
|
||||||
using freq_t = std::array<std::size_t, 26>;
|
using freq_t = std::array<std::size_t, 26>;
|
||||||
|
|
||||||
freq_t freqs(const std::string& word)
|
freq_t freqs(const std::string &word) {
|
||||||
{
|
|
||||||
freq_t f{0};
|
freq_t f{0};
|
||||||
|
|
||||||
for (auto c : word) {
|
for (auto c : word) {
|
||||||
|
@ -16,8 +15,7 @@ class Solution {
|
||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
int mask(const freq_t& f)
|
int mask(const freq_t &f) {
|
||||||
{
|
|
||||||
int m = 0;
|
int m = 0;
|
||||||
|
|
||||||
for (auto c : f) {
|
for (auto c : f) {
|
||||||
|
@ -28,8 +26,7 @@ class Solution {
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
bool closeStrings(std::string word1, std::string word2)
|
bool closeStrings(std::string word1, std::string word2) {
|
||||||
{
|
|
||||||
auto f1 = freqs(word1);
|
auto f1 = freqs(word1);
|
||||||
auto m1 = mask(f1);
|
auto m1 = mask(f1);
|
||||||
std::sort(f1.begin(), f1.end());
|
std::sort(f1.begin(), f1.end());
|
||||||
|
|
|
@ -4,15 +4,13 @@
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
char nextGreatestLetter(const std::vector<char>& letters, char target)
|
char nextGreatestLetter(const std::vector<char> &letters, char target) {
|
||||||
{
|
|
||||||
auto it = std::lower_bound(letters.begin(), letters.end(), target + 1);
|
auto it = std::lower_bound(letters.begin(), letters.end(), target + 1);
|
||||||
return it == letters.end() ? letters.front() : *it;
|
return it == letters.end() ? letters.front() : *it;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
Solution s;
|
Solution s;
|
||||||
|
|
||||||
assert((s.nextGreatestLetter(std::vector{'c', 'f', 'j'}, 'a') == 'c'));
|
assert((s.nextGreatestLetter(std::vector{'c', 'f', 'j'}, 'a') == 'c'));
|
||||||
|
|
|
@ -7,8 +7,7 @@ using namespace std;
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
int kthSmallest(vector<vector<int>>& matrix, int k)
|
int kthSmallest(vector<vector<int>> &matrix, int k) {
|
||||||
{
|
|
||||||
int low = matrix.front().front();
|
int low = matrix.front().front();
|
||||||
int high = matrix.back().back();
|
int high = matrix.back().back();
|
||||||
|
|
||||||
|
@ -31,18 +30,13 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
Solution s;
|
Solution s;
|
||||||
|
|
||||||
vector<vector<int>> m;
|
vector<vector<int>> m;
|
||||||
int k;
|
int k;
|
||||||
|
|
||||||
m = {
|
m = {{1, 5, 9}, {10, 11, 13}, {12, 13, 15}};
|
||||||
{ 1, 5, 9 },
|
|
||||||
{ 10, 11, 13 },
|
|
||||||
{ 12, 13, 15 }
|
|
||||||
};
|
|
||||||
k = 8;
|
k = 8;
|
||||||
assert(s.kthSmallest(m, k) == 13);
|
assert(s.kthSmallest(m, k) == 13);
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,8 @@ class Solution {
|
||||||
|
|
||||||
indices down{0, 0}, up{0, static_cast<int>(mat.size()) - 1};
|
indices down{0, 0}, up{0, static_cast<int>(mat.size()) - 1};
|
||||||
indices d_down{1, 1}, d_up{1, -1};
|
indices d_down{1, 1}, d_up{1, -1};
|
||||||
for (std::size_t i = 0; i < mat.size(); ++i, down += d_down, up += d_up) {
|
for (std::size_t i = 0; i < mat.size();
|
||||||
|
++i, down += d_down, up += d_up) {
|
||||||
sum += down[mat];
|
sum += down[mat];
|
||||||
if (down != up) {
|
if (down != up) {
|
||||||
sum += up[mat];
|
sum += up[mat];
|
||||||
|
@ -39,7 +40,8 @@ int main() {
|
||||||
Solution s;
|
Solution s;
|
||||||
|
|
||||||
assert((s.diagonalSum({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}) == 25));
|
assert((s.diagonalSum({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}) == 25));
|
||||||
assert((s.diagonalSum(
|
assert(
|
||||||
|
(s.diagonalSum(
|
||||||
{{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}}) == 8));
|
{{1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1}}) == 8));
|
||||||
assert((s.diagonalSum({{5}}) == 5));
|
assert((s.diagonalSum({{5}}) == 5));
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -2,15 +2,14 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
void row_partial_sums(const std::vector<std::vector<int>>& matrix, std::size_t y, std::vector<int>& partial) const
|
void row_partial_sums(const std::vector<std::vector<int>> &matrix,
|
||||||
{
|
std::size_t y, std::vector<int> &partial) const {
|
||||||
for (std::size_t x = 0; x < partial.size(); x++) {
|
for (std::size_t x = 0; x < partial.size(); x++) {
|
||||||
partial[x] += matrix[y][x];
|
partial[x] += matrix[y][x];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int find(std::vector<int>& partial_sums, int k) const
|
int find(std::vector<int> &partial_sums, int k) const {
|
||||||
{
|
|
||||||
int max_sum = INT_MIN;
|
int max_sum = INT_MIN;
|
||||||
|
|
||||||
std::set<int> prefixes;
|
std::set<int> prefixes;
|
||||||
|
@ -32,8 +31,8 @@ class Solution {
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
int maxSumSubmatrix(const std::vector<std::vector<int>>& matrix, int k) const
|
int maxSumSubmatrix(const std::vector<std::vector<int>> &matrix,
|
||||||
{
|
int k) const {
|
||||||
std::size_t rows = matrix.size();
|
std::size_t rows = matrix.size();
|
||||||
std::size_t cols = matrix.front().size();
|
std::size_t cols = matrix.front().size();
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
long sum(long index, long value, long n)
|
long sum(long index, long value, long n) {
|
||||||
{
|
|
||||||
long count = 0;
|
long count = 0;
|
||||||
|
|
||||||
if (value > index) {
|
if (value > index) {
|
||||||
|
@ -19,12 +18,11 @@ long sum(long index, long value, long n)
|
||||||
|
|
||||||
return count - value;
|
return count - value;
|
||||||
}
|
}
|
||||||
}
|
} // namespace
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
int maxValue(int n, int index, int maxSum)
|
int maxValue(int n, int index, int maxSum) {
|
||||||
{
|
|
||||||
int left = 1, right = maxSum;
|
int left = 1, right = maxSum;
|
||||||
|
|
||||||
while (left < right) {
|
while (left < right) {
|
||||||
|
@ -40,8 +38,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
Solution s;
|
Solution s;
|
||||||
|
|
||||||
assert(s.maxValue(4, 2, 6) == 2);
|
assert(s.maxValue(4, 2, 6) == 2);
|
||||||
|
|
|
@ -3,8 +3,8 @@
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
std::string mergeAlternately(const std::string& word1, const std::string& word2)
|
std::string mergeAlternately(const std::string &word1,
|
||||||
{
|
const std::string &word2) {
|
||||||
std::string result;
|
std::string result;
|
||||||
|
|
||||||
auto l = word1.begin();
|
auto l = word1.begin();
|
||||||
|
@ -27,8 +27,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
Solution s;
|
Solution s;
|
||||||
|
|
||||||
assert(s.mergeAlternately("abc", "pqr") == "apbqcr");
|
assert(s.mergeAlternately("abc", "pqr") == "apbqcr");
|
||||||
|
|
|
@ -10,8 +10,7 @@
|
||||||
*/
|
*/
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
ListNode* middleNode(ListNode* head)
|
ListNode *middleNode(ListNode *head) {
|
||||||
{
|
|
||||||
auto slow = head;
|
auto slow = head;
|
||||||
auto fast = head ? head->next : nullptr;
|
auto fast = head ? head->next : nullptr;
|
||||||
|
|
||||||
|
|
|
@ -3,8 +3,7 @@
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
int minFlips(int a, int b, int c)
|
int minFlips(int a, int b, int c) {
|
||||||
{
|
|
||||||
auto flips = 0;
|
auto flips = 0;
|
||||||
|
|
||||||
for (; (a | b) != c; a >>= 1, b >>= 1, c >>= 1) {
|
for (; (a | b) != c; a >>= 1, b >>= 1, c >>= 1) {
|
||||||
|
@ -25,8 +24,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
Solution s;
|
Solution s;
|
||||||
|
|
||||||
assert(s.minFlips(2, 6, 5) == 3);
|
assert(s.minFlips(2, 6, 5) == 3);
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
int mirrorReflection(int p, int q)
|
int mirrorReflection(int p, int q) {
|
||||||
{
|
|
||||||
auto lcm = p * q / std::gcd(p, q);
|
auto lcm = p * q / std::gcd(p, q);
|
||||||
auto x = lcm / p;
|
auto x = lcm / p;
|
||||||
auto y = lcm / q;
|
auto y = lcm / q;
|
||||||
|
|
|
@ -8,25 +8,20 @@ struct range_t {
|
||||||
int start;
|
int start;
|
||||||
int end;
|
int end;
|
||||||
|
|
||||||
range_t(int start, int end)
|
range_t(int start, int end) : start(start), end(end) {}
|
||||||
: start(start)
|
|
||||||
, end(end)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool has(int x) const { return x > start && x < end; }
|
bool has(int x) const { return x > start && x < end; }
|
||||||
|
|
||||||
bool operator>(const range_t &other) const { return start > other.start; }
|
bool operator>(const range_t &other) const { return start > other.start; }
|
||||||
|
|
||||||
bool operator==(const range_t& other) const
|
bool operator==(const range_t &other) const {
|
||||||
{
|
|
||||||
return start == other.start && end == other.end;
|
return start == other.start && end == other.end;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
bool overlaps(const range_t& a, const range_t& b)
|
bool overlaps(const range_t &a, const range_t &b) {
|
||||||
{
|
return a == b || a.has(b.start) || a.has(b.end) || b.has(a.start) ||
|
||||||
return a == b || a.has(b.start) || a.has(b.end) || b.has(a.start) || b.has(a.end);
|
b.has(a.end);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
@ -37,8 +32,7 @@ class MyCalendar {
|
||||||
public:
|
public:
|
||||||
MyCalendar() = default;
|
MyCalendar() = default;
|
||||||
|
|
||||||
bool book(int start, int end)
|
bool book(int start, int end) {
|
||||||
{
|
|
||||||
entries.emplace_back(start, end);
|
entries.emplace_back(start, end);
|
||||||
|
|
||||||
auto it = entries.end();
|
auto it = entries.end();
|
||||||
|
@ -69,8 +63,7 @@ public:
|
||||||
* bool param_1 = obj->book(start,end);
|
* bool param_1 = obj->book(start,end);
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static void test_case_1()
|
static void test_case_1() {
|
||||||
{
|
|
||||||
std::cout << "=== TEST CASE 1 ===\n";
|
std::cout << "=== TEST CASE 1 ===\n";
|
||||||
|
|
||||||
std::vector<range_t> entries{{47, 50}, {33, 41}, {39, 45}, {33, 42},
|
std::vector<range_t> entries{{47, 50}, {33, 41}, {39, 45}, {33, 42},
|
||||||
|
@ -90,11 +83,11 @@ static void test_case_1()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_case_2()
|
static void test_case_2() {
|
||||||
{
|
|
||||||
std::cout << "=== TEST CASE 2 ===\n";
|
std::cout << "=== TEST CASE 2 ===\n";
|
||||||
|
|
||||||
std::vector<range_t> entries { { 37, 50 }, { 33, 50 }, { 4, 17 }, { 35, 48 }, { 8, 25 } };
|
std::vector<range_t> entries{
|
||||||
|
{37, 50}, {33, 50}, {4, 17}, {35, 48}, {8, 25}};
|
||||||
std::vector<bool> expected{true, false, true, false, false};
|
std::vector<bool> expected{true, false, true, false, false};
|
||||||
|
|
||||||
MyCalendar c;
|
MyCalendar c;
|
||||||
|
@ -108,8 +101,7 @@ static void test_case_2()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_case_3()
|
static void test_case_3() {
|
||||||
{
|
|
||||||
std::cout << "=== TEST CASE 3 ===\n";
|
std::cout << "=== TEST CASE 3 ===\n";
|
||||||
|
|
||||||
std::vector<range_t> entries{
|
std::vector<range_t> entries{
|
||||||
|
@ -117,13 +109,11 @@ static void test_case_3()
|
||||||
{19, 25}, {36, 42}, {45, 50}, {47, 50}, {39, 45}, {44, 50},
|
{19, 25}, {36, 42}, {45, 50}, {47, 50}, {39, 45}, {44, 50},
|
||||||
{16, 25}, {45, 50}, {45, 50}, {12, 20}, {21, 29}, {11, 20},
|
{16, 25}, {45, 50}, {45, 50}, {12, 20}, {21, 29}, {11, 20},
|
||||||
{12, 17}, {34, 40}, {10, 18}, {38, 44}, {23, 32}, {38, 44},
|
{12, 17}, {34, 40}, {10, 18}, {38, 44}, {23, 32}, {38, 44},
|
||||||
{ 15, 20 }, { 27, 33 }, { 34, 42 }, { 44, 50 }, { 35, 40 }, { 24, 31 }
|
{15, 20}, {27, 33}, {34, 42}, {44, 50}, {35, 40}, {24, 31}};
|
||||||
};
|
|
||||||
std::vector<bool> expected{
|
std::vector<bool> expected{
|
||||||
true, false, true, true, false, true, false, true, false, false,
|
true, false, true, true, false, true, false, true, false, false,
|
||||||
false, false, false, false, false, false, false, false, false, false,
|
false, false, false, false, false, false, false, false, false, false,
|
||||||
false, false, false, false, false, false, false, false, false, false
|
false, false, false, false, false, false, false, false, false, false};
|
||||||
};
|
|
||||||
|
|
||||||
MyCalendar c;
|
MyCalendar c;
|
||||||
for (auto i = 0; i < entries.size(); i++) {
|
for (auto i = 0; i < entries.size(); i++) {
|
||||||
|
@ -136,8 +126,7 @@ static void test_case_3()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
MyCalendar c;
|
MyCalendar c;
|
||||||
assert(c.book(10, 20));
|
assert(c.book(10, 20));
|
||||||
assert(!c.book(15, 25));
|
assert(!c.book(15, 25));
|
||||||
|
|
|
@ -21,8 +21,7 @@ public:
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
void levelOrder(vector<vector<int>>& traversal, Node* root, int level)
|
void levelOrder(vector<vector<int>> &traversal, Node *root, int level) {
|
||||||
{
|
|
||||||
if (root == nullptr) {
|
if (root == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -38,8 +37,7 @@ class Solution {
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
vector<vector<int>> levelOrder(Node* root)
|
vector<vector<int>> levelOrder(Node *root) {
|
||||||
{
|
|
||||||
vector<vector<int>> result;
|
vector<vector<int>> result;
|
||||||
levelOrder(result, root, 0);
|
levelOrder(result, root, 0);
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -21,8 +21,7 @@ public:
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
void preorder(vector<int>& traversal, Node* root)
|
void preorder(vector<int> &traversal, Node *root) {
|
||||||
{
|
|
||||||
if (root == nullptr) {
|
if (root == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -34,8 +33,7 @@ class Solution {
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
vector<int> preorder(Node* root)
|
vector<int> preorder(Node *root) {
|
||||||
{
|
|
||||||
vector<int> result;
|
vector<int> result;
|
||||||
preorder(result, root);
|
preorder(result, root);
|
||||||
return result;
|
return result;
|
||||||
|
|
|
@ -9,7 +9,8 @@ public:
|
||||||
|
|
||||||
int last_row = 0;
|
int last_row = 0;
|
||||||
for (const auto &row : bank) {
|
for (const auto &row : bank) {
|
||||||
if (auto current_row = std::count(row.begin(), row.end(), '1'); current_row != 0) {
|
if (auto current_row = std::count(row.begin(), row.end(), '1');
|
||||||
|
current_row != 0) {
|
||||||
beams += last_row * current_row;
|
beams += last_row * current_row;
|
||||||
last_row = current_row;
|
last_row = current_row;
|
||||||
}
|
}
|
||||||
|
@ -18,4 +19,3 @@ public:
|
||||||
return beams;
|
return beams;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
class Solution {
|
class Solution {
|
||||||
bool matches(const string& s, const string& word) const
|
bool matches(const string &s, const string &word) const {
|
||||||
{
|
|
||||||
auto s_i = 0;
|
auto s_i = 0;
|
||||||
|
|
||||||
for (auto i = 0; s_i < s.size() && i < word.size(); i++) {
|
for (auto i = 0; s_i < s.size() && i < word.size(); i++) {
|
||||||
|
@ -12,8 +11,7 @@ class Solution {
|
||||||
return s_i == s.size();
|
return s_i == s.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
map<string, int> preprocess(const vector<string>& words) const
|
map<string, int> preprocess(const vector<string> &words) const {
|
||||||
{
|
|
||||||
map<string, int> histogram;
|
map<string, int> histogram;
|
||||||
|
|
||||||
for (auto &w : words) {
|
for (auto &w : words) {
|
||||||
|
@ -24,11 +22,12 @@ class Solution {
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
int numMatchingSubseq(string s, vector<string>& words)
|
int numMatchingSubseq(string s, vector<string> &words) {
|
||||||
{
|
|
||||||
auto histogram = preprocess(words);
|
auto histogram = preprocess(words);
|
||||||
return accumulate(histogram.begin(), histogram.end(), 0, [&](int acc, const auto& pair) {
|
return accumulate(histogram.begin(), histogram.end(), 0,
|
||||||
return acc + (matches(pair.first, s) ? pair.second : 0);
|
[&](int acc, const auto &pair) {
|
||||||
|
return acc +
|
||||||
|
(matches(pair.first, s) ? pair.second : 0);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,8 +2,7 @@
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
void nums_same_consec_diff(int n, int k, std::vector<int>& nums, int number)
|
void nums_same_consec_diff(int n, int k, std::vector<int> &nums, int number) {
|
||||||
{
|
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
nums.push_back(number);
|
nums.push_back(number);
|
||||||
return;
|
return;
|
||||||
|
@ -17,12 +16,11 @@ void nums_same_consec_diff(int n, int k, std::vector<int>& nums, int number)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} // namespace
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
std::vector<int> numsSameConsecDiff(int n, int k)
|
std::vector<int> numsSameConsecDiff(int n, int k) {
|
||||||
{
|
|
||||||
std::vector<int> nums;
|
std::vector<int> nums;
|
||||||
|
|
||||||
for (int d = 1; d < 10; d++) {
|
for (int d = 1; d < 10; d++) {
|
||||||
|
@ -37,36 +35,32 @@ public:
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
TEST(examples, first)
|
TEST(examples, first) {
|
||||||
{
|
|
||||||
Solution s;
|
Solution s;
|
||||||
ASSERT_EQ(s.numsSameConsecDiff(3, 7), (std::vector { 181, 292, 707, 818, 929 }));
|
ASSERT_EQ(s.numsSameConsecDiff(3, 7),
|
||||||
|
(std::vector{181, 292, 707, 818, 929}));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(examples, second)
|
TEST(examples, second) {
|
||||||
{
|
|
||||||
Solution s;
|
Solution s;
|
||||||
ASSERT_EQ(s.numsSameConsecDiff(2, 1),
|
ASSERT_EQ(s.numsSameConsecDiff(2, 1),
|
||||||
(std::vector { 10, 12, 21, 23, 32, 34, 43, 45, 54, 56, 65, 67, 76, 78,
|
(std::vector{10, 12, 21, 23, 32, 34, 43, 45, 54, 56, 65, 67, 76,
|
||||||
87, 89, 98 }));
|
78, 87, 89, 98}));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(same, two)
|
TEST(same, two) {
|
||||||
{
|
|
||||||
Solution s;
|
Solution s;
|
||||||
ASSERT_EQ(s.numsSameConsecDiff(2, 0),
|
ASSERT_EQ(s.numsSameConsecDiff(2, 0),
|
||||||
(std::vector{11, 22, 33, 44, 55, 66, 77, 88, 99}));
|
(std::vector{11, 22, 33, 44, 55, 66, 77, 88, 99}));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(same, three)
|
TEST(same, three) {
|
||||||
{
|
|
||||||
Solution s;
|
Solution s;
|
||||||
ASSERT_EQ(s.numsSameConsecDiff(3, 0),
|
ASSERT_EQ(s.numsSameConsecDiff(3, 0),
|
||||||
(std::vector{111, 222, 333, 444, 555, 666, 777, 888, 999}));
|
(std::vector{111, 222, 333, 444, 555, 666, 777, 888, 999}));
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char **argv) {
|
||||||
{
|
|
||||||
::testing::InitGoogleTest(&argc, argv);
|
::testing::InitGoogleTest(&argc, argv);
|
||||||
return RUN_ALL_TESTS();
|
return RUN_ALL_TESTS();
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,9 +9,7 @@ private:
|
||||||
|
|
||||||
std::vector<std::vector<std::map<int, unsigned>>> paths;
|
std::vector<std::vector<std::map<int, unsigned>>> paths;
|
||||||
|
|
||||||
int
|
int dfs(int y, int x, int moves) {
|
||||||
dfs(int y, int x, int moves)
|
|
||||||
{
|
|
||||||
if (y < 0 || y >= rows || x < 0 || x >= cols) {
|
if (y < 0 || y >= rows || x < 0 || x >= cols) {
|
||||||
// BASE: we got out of the bounds
|
// BASE: we got out of the bounds
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -39,24 +37,15 @@ private:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
dp(int rows, int cols, int maxMove)
|
dp(int rows, int cols, int maxMove)
|
||||||
: rows(rows)
|
: rows(rows), cols(cols), maxMove(maxMove),
|
||||||
, cols(cols)
|
paths(rows, std::vector<std::map<int, unsigned>>(cols)) {}
|
||||||
, maxMove(maxMove)
|
|
||||||
, paths(rows, std::vector<std::map<int, unsigned>>(cols))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
int get(int row, int col) { return dfs(row, col, maxMove); }
|
||||||
get(int row, int col)
|
|
||||||
{
|
|
||||||
return dfs(row, col, maxMove);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
int
|
int findPaths(int m, int n, int maxMove, int startRow,
|
||||||
findPaths(int m, int n, int maxMove, int startRow, int startColumn) const
|
int startColumn) const {
|
||||||
{
|
|
||||||
return dp(m, n, maxMove).get(startRow, startColumn);
|
return dp(m, n, maxMove).get(startRow, startColumn);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -15,21 +15,9 @@
|
||||||
struct ListNode {
|
struct ListNode {
|
||||||
int val;
|
int val;
|
||||||
ListNode *next;
|
ListNode *next;
|
||||||
ListNode()
|
ListNode() : val(0), next(nullptr) {}
|
||||||
: val(0)
|
ListNode(int x) : val(x), next(nullptr) {}
|
||||||
, next(nullptr)
|
ListNode(int x, ListNode *next) : val(x), next(next) {}
|
||||||
{
|
|
||||||
}
|
|
||||||
ListNode(int x)
|
|
||||||
: val(x)
|
|
||||||
, next(nullptr)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
ListNode(int x, ListNode* next)
|
|
||||||
: val(x)
|
|
||||||
, next(next)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
@ -38,16 +26,10 @@ struct middle_t {
|
||||||
ListNode *node;
|
ListNode *node;
|
||||||
std::size_t size;
|
std::size_t size;
|
||||||
|
|
||||||
middle_t(ListNode* node, std::size_t size)
|
middle_t(ListNode *node, std::size_t size) : node(node), size(size) {}
|
||||||
: node(node)
|
|
||||||
, size(size)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
middle_t
|
middle_t find_middle(ListNode *head) {
|
||||||
find_middle(ListNode* head)
|
|
||||||
{
|
|
||||||
ListNode *slow = head;
|
ListNode *slow = head;
|
||||||
ListNode *fast = head->next;
|
ListNode *fast = head->next;
|
||||||
|
|
||||||
|
@ -60,12 +42,12 @@ find_middle(ListNode* head)
|
||||||
return {slow, size};
|
return {slow, size};
|
||||||
}
|
}
|
||||||
|
|
||||||
ListNode* reverse_list(ListNode* tail)
|
ListNode *reverse_list(ListNode *tail) {
|
||||||
{
|
|
||||||
ListNode *previous = nullptr;
|
ListNode *previous = nullptr;
|
||||||
ListNode *next = nullptr;
|
ListNode *next = nullptr;
|
||||||
|
|
||||||
for (ListNode* current = tail; current != nullptr; previous = current, current = next) {
|
for (ListNode *current = tail; current != nullptr;
|
||||||
|
previous = current, current = next) {
|
||||||
next = current->next;
|
next = current->next;
|
||||||
current->next = previous;
|
current->next = previous;
|
||||||
}
|
}
|
||||||
|
@ -73,8 +55,7 @@ ListNode* reverse_list(ListNode* tail)
|
||||||
return previous;
|
return previous;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool compare_lists(ListNode* left, ListNode* right, std::size_t size)
|
bool compare_lists(ListNode *left, ListNode *right, std::size_t size) {
|
||||||
{
|
|
||||||
for (auto i = 0u; i < size + 1; i++) {
|
for (auto i = 0u; i < size + 1; i++) {
|
||||||
if (left->val != right->val) {
|
if (left->val != right->val) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -90,8 +71,7 @@ bool compare_lists(ListNode* left, ListNode* right, std::size_t size)
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
bool isPalindrome(ListNode* head)
|
bool isPalindrome(ListNode *head) {
|
||||||
{
|
|
||||||
// find middle of the linked list
|
// find middle of the linked list
|
||||||
auto mid = find_middle(head);
|
auto mid = find_middle(head);
|
||||||
|
|
||||||
|
@ -106,9 +86,7 @@ public:
|
||||||
#pragma region Testing utilities
|
#pragma region Testing utilities
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
ListNode*
|
ListNode *construct_list(const std::vector<int> &values) {
|
||||||
construct_list(const std::vector<int>& values)
|
|
||||||
{
|
|
||||||
ListNode *head = new ListNode(values.front());
|
ListNode *head = new ListNode(values.front());
|
||||||
ListNode *tail = head;
|
ListNode *tail = head;
|
||||||
|
|
||||||
|
@ -120,8 +98,7 @@ construct_list(const std::vector<int>& values)
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
|
|
||||||
void destroy_list(ListNode* linked_list)
|
void destroy_list(ListNode *linked_list) {
|
||||||
{
|
|
||||||
if (linked_list == nullptr) {
|
if (linked_list == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -129,8 +106,7 @@ void destroy_list(ListNode* linked_list)
|
||||||
delete linked_list;
|
delete linked_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool test_find_middle(const std::vector<int>& values, int mid_value)
|
bool test_find_middle(const std::vector<int> &values, int mid_value) {
|
||||||
{
|
|
||||||
auto linked_list = construct_list(values);
|
auto linked_list = construct_list(values);
|
||||||
|
|
||||||
auto mid = find_middle(linked_list);
|
auto mid = find_middle(linked_list);
|
||||||
|
@ -140,8 +116,7 @@ bool test_find_middle(const std::vector<int>& values, int mid_value)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool test_isPalindrome(const std::vector<int>& values)
|
bool test_isPalindrome(const std::vector<int> &values) {
|
||||||
{
|
|
||||||
auto linked_list = construct_list(values);
|
auto linked_list = construct_list(values);
|
||||||
|
|
||||||
Solution s;
|
Solution s;
|
||||||
|
@ -154,8 +129,7 @@ bool test_isPalindrome(const std::vector<int>& values)
|
||||||
} // namespace
|
} // namespace
|
||||||
#pragma endregion // Testing utilities
|
#pragma endregion // Testing utilities
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
// find_middle tests
|
// find_middle tests
|
||||||
assert(test_find_middle(std::vector{1, 2, 2, 1}, 2));
|
assert(test_find_middle(std::vector{1, 2, 2, 1}, 2));
|
||||||
assert(test_find_middle(std::vector{1, 2, 3, 2, 1}, 3));
|
assert(test_find_middle(std::vector{1, 2, 3, 2, 1}, 3));
|
||||||
|
|
|
@ -9,8 +9,7 @@
|
||||||
* };
|
* };
|
||||||
*/
|
*/
|
||||||
class Solution {
|
class Solution {
|
||||||
ListNode* update_tail(ListNode* tail, ListNode* node) const
|
ListNode *update_tail(ListNode *tail, ListNode *node) const {
|
||||||
{
|
|
||||||
if (tail != nullptr) {
|
if (tail != nullptr) {
|
||||||
tail->next = node;
|
tail->next = node;
|
||||||
}
|
}
|
||||||
|
@ -18,8 +17,7 @@ class Solution {
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ListNode* partition(ListNode* head, int x)
|
ListNode *partition(ListNode *head, int x) {
|
||||||
{
|
|
||||||
ListNode *left_head = nullptr;
|
ListNode *left_head = nullptr;
|
||||||
ListNode *right_head = nullptr;
|
ListNode *right_head = nullptr;
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
vector<int> getRow(int rowIndex)
|
vector<int> getRow(int rowIndex) {
|
||||||
{
|
|
||||||
vector<int> result;
|
vector<int> result;
|
||||||
|
|
||||||
result.push_back(1);
|
result.push_back(1);
|
||||||
for (auto k = 0; k < rowIndex; k++) {
|
for (auto k = 0; k < rowIndex; k++) {
|
||||||
auto next = static_cast<int>(
|
auto next = static_cast<int>(static_cast<long>(result.back()) *
|
||||||
static_cast<long>(result.back()) * (rowIndex - k) / (k + 1));
|
(rowIndex - k) / (k + 1));
|
||||||
result.push_back(next);
|
result.push_back(next);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
class Solution {
|
class Solution {
|
||||||
int getN(const vector<int>& previousRow, int n)
|
int getN(const vector<int> &previousRow, int n) {
|
||||||
{
|
|
||||||
if (n == 0 || n == previousRow.size()) {
|
if (n == 0 || n == previousRow.size()) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -8,8 +7,7 @@ class Solution {
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
vector<vector<int>> generate(int numRows)
|
vector<vector<int>> generate(int numRows) {
|
||||||
{
|
|
||||||
if (numRows <= 0) {
|
if (numRows <= 0) {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,8 +4,7 @@
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
std::map<char, int> build_freqs(const std::string& input)
|
std::map<char, int> build_freqs(const std::string &input) {
|
||||||
{
|
|
||||||
std::map<char, int> freqs;
|
std::map<char, int> freqs;
|
||||||
|
|
||||||
for (auto c : input) {
|
for (auto c : input) {
|
||||||
|
@ -15,8 +14,7 @@ std::map<char, int> build_freqs(const std::string& input)
|
||||||
return freqs;
|
return freqs;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool subtract(std::map<char, int> available, const std::string& message)
|
bool subtract(std::map<char, int> available, const std::string &message) {
|
||||||
{
|
|
||||||
for (auto c : message) {
|
for (auto c : message) {
|
||||||
available[c]--;
|
available[c]--;
|
||||||
|
|
||||||
|
@ -28,19 +26,18 @@ bool subtract(std::map<char, int> available, const std::string& message)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} // namespace
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
bool canConstruct(const std::string& ransomNote, const std::string& magazine)
|
bool canConstruct(const std::string &ransomNote,
|
||||||
{
|
const std::string &magazine) {
|
||||||
auto available = build_freqs(magazine);
|
auto available = build_freqs(magazine);
|
||||||
return subtract(available, ransomNote);
|
return subtract(available, ransomNote);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
Solution s;
|
Solution s;
|
||||||
|
|
||||||
assert(!s.canConstruct("a", "b"));
|
assert(!s.canConstruct("a", "b"));
|
||||||
|
|
|
@ -3,8 +3,7 @@
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
std::string removeStars(const std::string& s)
|
std::string removeStars(const std::string &s) {
|
||||||
{
|
|
||||||
std::vector<char> without_stars;
|
std::vector<char> without_stars;
|
||||||
|
|
||||||
for (auto c : s) {
|
for (auto c : s) {
|
||||||
|
|
|
@ -4,10 +4,7 @@
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
bool is_power_of_2(int n)
|
bool is_power_of_2(int n) { return (n & (n - 1)) == 0; }
|
||||||
{
|
|
||||||
return (n & (n - 1)) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
class permutations {
|
class permutations {
|
||||||
using values_t = typename std::vector<char>;
|
using values_t = typename std::vector<char>;
|
||||||
|
@ -18,23 +15,18 @@ class permutations {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
p_iter(values_t elements, bool last = false)
|
p_iter(values_t elements, bool last = false)
|
||||||
: elements(elements)
|
: elements(elements), last(last) {}
|
||||||
, last(last)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
const values_t &operator*() const { return elements; }
|
const values_t &operator*() const { return elements; }
|
||||||
|
|
||||||
p_iter& operator++()
|
p_iter &operator++() {
|
||||||
{
|
|
||||||
if (!std::next_permutation(elements.begin(), elements.end())) {
|
if (!std::next_permutation(elements.begin(), elements.end())) {
|
||||||
last = true;
|
last = true;
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const p_iter& other) const
|
bool operator==(const p_iter &other) const {
|
||||||
{
|
|
||||||
return last == other.last && elements == other.elements;
|
return last == other.last && elements == other.elements;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,9 +36,7 @@ class permutations {
|
||||||
values_t elements;
|
values_t elements;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
permutations(const values_t& input_ints)
|
permutations(const values_t &input_ints) : elements(input_ints) {
|
||||||
: elements(input_ints)
|
|
||||||
{
|
|
||||||
std::sort(elements.begin(), elements.end());
|
std::sort(elements.begin(), elements.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,8 +44,7 @@ public:
|
||||||
p_iter end() const { return {elements, true}; }
|
p_iter end() const { return {elements, true}; }
|
||||||
};
|
};
|
||||||
|
|
||||||
std::vector<char> to_vector(int n)
|
std::vector<char> to_vector(int n) {
|
||||||
{
|
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
return std::vector<char>(1, 0);
|
return std::vector<char>(1, 0);
|
||||||
}
|
}
|
||||||
|
@ -70,8 +59,7 @@ std::vector<char> to_vector(int n)
|
||||||
return digits;
|
return digits;
|
||||||
}
|
}
|
||||||
|
|
||||||
int to_number(const std::vector<char> digits)
|
int to_number(const std::vector<char> digits) {
|
||||||
{
|
|
||||||
int number = 0;
|
int number = 0;
|
||||||
|
|
||||||
for (auto digit : digits) {
|
for (auto digit : digits) {
|
||||||
|
@ -81,12 +69,11 @@ int to_number(const std::vector<char> digits)
|
||||||
return number;
|
return number;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} // namespace
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
bool reorderedPowerOf2(int n)
|
bool reorderedPowerOf2(int n) {
|
||||||
{
|
|
||||||
for (const auto &permutation : permutations(to_vector(n))) {
|
for (const auto &permutation : permutations(to_vector(n))) {
|
||||||
if (permutation.front() == 0) {
|
if (permutation.front() == 0) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -101,8 +88,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
Solution s;
|
Solution s;
|
||||||
|
|
||||||
assert(s.reorderedPowerOf2(1));
|
assert(s.reorderedPowerOf2(1));
|
||||||
|
|
|
@ -3,8 +3,7 @@
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
void rotate(std::vector<std::vector<int>>& matrix)
|
void rotate(std::vector<std::vector<int>> &matrix) {
|
||||||
{
|
|
||||||
for (std::size_t i = 0; i < matrix.size(); i++) {
|
for (std::size_t i = 0; i < matrix.size(); i++) {
|
||||||
for (std::size_t j = i; j < matrix.size(); j++) {
|
for (std::size_t j = i; j < matrix.size(); j++) {
|
||||||
std::swap(matrix[i][j], matrix[j][i]);
|
std::swap(matrix[i][j], matrix[j][i]);
|
||||||
|
|
|
@ -6,13 +6,13 @@
|
||||||
* TreeNode *right;
|
* TreeNode *right;
|
||||||
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||||
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||||
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
|
||||||
|
* right(right) {}
|
||||||
* };
|
* };
|
||||||
*/
|
*/
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
bool isSameTree(TreeNode* p, TreeNode* q)
|
bool isSameTree(TreeNode *p, TreeNode *q) {
|
||||||
{
|
|
||||||
if (p == nullptr && q == nullptr) {
|
if (p == nullptr && q == nullptr) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,23 +8,13 @@ class SnapshotArray {
|
||||||
std::vector<std::map<int, int>> arr;
|
std::vector<std::map<int, int>> arr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SnapshotArray(int length)
|
SnapshotArray(int length) : arr(length, std::map<int, int>{{0, 0}}) {}
|
||||||
: arr(length, std::map<int, int> { { 0, 0 } })
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void set(int index, int val)
|
void set(int index, int val) { arr[index][snap_id] = val; }
|
||||||
{
|
|
||||||
arr[index][snap_id] = val;
|
|
||||||
}
|
|
||||||
|
|
||||||
int snap()
|
int snap() { return snap_id++; }
|
||||||
{
|
|
||||||
return snap_id++;
|
|
||||||
}
|
|
||||||
|
|
||||||
int get(int index, int snap_id)
|
int get(int index, int snap_id) {
|
||||||
{
|
|
||||||
auto it = std::prev(arr[index].lower_bound(snap_id + 1));
|
auto it = std::prev(arr[index].lower_bound(snap_id + 1));
|
||||||
return it == arr[index].end() ? 0 : it->second;
|
return it == arr[index].end() ? 0 : it->second;
|
||||||
}
|
}
|
||||||
|
@ -38,8 +28,7 @@ public:
|
||||||
* int param_3 = obj->get(index,snap_id);
|
* int param_3 = obj->get(index,snap_id);
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
SnapshotArray arr(3);
|
SnapshotArray arr(3);
|
||||||
arr.set(0, 5);
|
arr.set(0, 5);
|
||||||
assert(arr.snap() == 0);
|
assert(arr.snap() == 0);
|
||||||
|
|
|
@ -7,9 +7,7 @@
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
template <typename T>
|
template <typename T> void print_matrix(const std::vector<std::vector<T>> &m) {
|
||||||
void print_matrix(const std::vector<std::vector<T>>& m)
|
|
||||||
{
|
|
||||||
for (const auto &row : m) {
|
for (const auto &row : m) {
|
||||||
for (auto x : row) {
|
for (auto x : row) {
|
||||||
std::cout << x << " ";
|
std::cout << x << " ";
|
||||||
|
@ -21,8 +19,7 @@ void print_matrix(const std::vector<std::vector<T>>& m)
|
||||||
std::cout << "\n";
|
std::cout << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T> class diagonal {
|
||||||
class diagonal {
|
|
||||||
std::vector<std::vector<T>> &matrix;
|
std::vector<std::vector<T>> &matrix;
|
||||||
std::size_t x;
|
std::size_t x;
|
||||||
std::size_t y;
|
std::size_t y;
|
||||||
|
@ -39,30 +36,24 @@ class diagonal {
|
||||||
using reference = T &;
|
using reference = T &;
|
||||||
using iterator_category = std::random_access_iterator_tag;
|
using iterator_category = std::random_access_iterator_tag;
|
||||||
|
|
||||||
diagonal_iter(std::vector<std::vector<T>>& matrix,
|
diagonal_iter(std::vector<std::vector<T>> &matrix, std::size_t x,
|
||||||
std::size_t x,
|
|
||||||
std::size_t y)
|
std::size_t y)
|
||||||
: m(matrix)
|
: m(matrix), x(x), y(y) {}
|
||||||
, x(x)
|
|
||||||
, y(y)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator!=(const diagonal_iter& rhs) const
|
bool operator!=(const diagonal_iter &rhs) const {
|
||||||
{
|
|
||||||
return m != rhs.m || x != rhs.x || y != rhs.y;
|
return m != rhs.m || x != rhs.x || y != rhs.y;
|
||||||
}
|
}
|
||||||
bool operator==(const diagonal_iter& rhs) const { return !(*this != rhs); }
|
bool operator==(const diagonal_iter &rhs) const {
|
||||||
|
return !(*this != rhs);
|
||||||
|
}
|
||||||
|
|
||||||
diagonal_iter& operator++()
|
diagonal_iter &operator++() {
|
||||||
{
|
|
||||||
x++;
|
x++;
|
||||||
y++;
|
y++;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
diagonal_iter operator--()
|
diagonal_iter operator--() {
|
||||||
{
|
|
||||||
x--;
|
x--;
|
||||||
y--;
|
y--;
|
||||||
return *this;
|
return *this;
|
||||||
|
@ -70,21 +61,18 @@ class diagonal {
|
||||||
|
|
||||||
reference operator*() const { return m[y][x]; }
|
reference operator*() const { return m[y][x]; }
|
||||||
|
|
||||||
diagonal_iter operator-(difference_type n) const
|
diagonal_iter operator-(difference_type n) const {
|
||||||
{
|
|
||||||
return diagonal_iter{m, x - n, y - n};
|
return diagonal_iter{m, x - n, y - n};
|
||||||
}
|
}
|
||||||
int operator-(const diagonal_iter &rhs) const { return x - rhs.x; }
|
int operator-(const diagonal_iter &rhs) const { return x - rhs.x; }
|
||||||
|
|
||||||
diagonal_iter operator+(difference_type n) const
|
diagonal_iter operator+(difference_type n) const {
|
||||||
{
|
|
||||||
return diagonal_iter{m, x + n, y + n};
|
return diagonal_iter{m, x + n, y + n};
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator<(const diagonal_iter &rhs) const { return x < rhs.x; }
|
bool operator<(const diagonal_iter &rhs) const { return x < rhs.x; }
|
||||||
|
|
||||||
diagonal_iter& operator=(const diagonal_iter& rhs)
|
diagonal_iter &operator=(const diagonal_iter &rhs) {
|
||||||
{
|
|
||||||
if (this != &rhs) // not a self-assignment
|
if (this != &rhs) // not a self-assignment
|
||||||
{
|
{
|
||||||
this->m = rhs.m;
|
this->m = rhs.m;
|
||||||
|
@ -97,16 +85,11 @@ class diagonal {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
diagonal(std::vector<std::vector<T>> &matrix, std::size_t x, std::size_t y)
|
diagonal(std::vector<std::vector<T>> &matrix, std::size_t x, std::size_t y)
|
||||||
: matrix(matrix)
|
: matrix(matrix), x(x), y(y) {}
|
||||||
, x(x)
|
|
||||||
, y(y)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
diagonal_iter begin() const { return diagonal_iter{matrix, x, y}; }
|
diagonal_iter begin() const { return diagonal_iter{matrix, x, y}; }
|
||||||
|
|
||||||
diagonal_iter end() const
|
diagonal_iter end() const {
|
||||||
{
|
|
||||||
auto max_x = matrix[y].size();
|
auto max_x = matrix[y].size();
|
||||||
auto max_y = matrix.size();
|
auto max_y = matrix.size();
|
||||||
|
|
||||||
|
@ -116,8 +99,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T> class diagonals {
|
||||||
class diagonals {
|
|
||||||
std::vector<std::vector<T>> &_matrix;
|
std::vector<std::vector<T>> &_matrix;
|
||||||
|
|
||||||
class diagonals_iter {
|
class diagonals_iter {
|
||||||
|
@ -126,22 +108,15 @@ class diagonals {
|
||||||
std::size_t y;
|
std::size_t y;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
diagonals_iter(std::vector<std::vector<T>>& matrix,
|
diagonals_iter(std::vector<std::vector<T>> &matrix, std::size_t x,
|
||||||
std::size_t x,
|
|
||||||
std::size_t y)
|
std::size_t y)
|
||||||
: m(matrix)
|
: m(matrix), x(x), y(y) {}
|
||||||
, x(x)
|
|
||||||
, y(y)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator!=(const diagonals_iter& rhs) const
|
bool operator!=(const diagonals_iter &rhs) const {
|
||||||
{
|
|
||||||
return m != rhs.m || x != rhs.x || y != rhs.y;
|
return m != rhs.m || x != rhs.x || y != rhs.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
diagonals_iter& operator++()
|
diagonals_iter &operator++() {
|
||||||
{
|
|
||||||
if (y != 0) {
|
if (y != 0) {
|
||||||
// iterating through diagonals down the first column
|
// iterating through diagonals down the first column
|
||||||
y++;
|
y++;
|
||||||
|
@ -163,10 +138,7 @@ class diagonals {
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
diagonals(std::vector<std::vector<T>>& matrix)
|
diagonals(std::vector<std::vector<T>> &matrix) : _matrix(matrix) {}
|
||||||
: _matrix(matrix)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
diagonals_iter begin() { return diagonals_iter{_matrix, 0, 0}; }
|
diagonals_iter begin() { return diagonals_iter{_matrix, 0, 0}; }
|
||||||
diagonals_iter end() { return diagonals_iter{_matrix, 0, _matrix.size()}; }
|
diagonals_iter end() { return diagonals_iter{_matrix, 0, _matrix.size()}; }
|
||||||
};
|
};
|
||||||
|
@ -175,8 +147,8 @@ public:
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
std::vector<std::vector<int>> diagonalSort(std::vector<std::vector<int>> mat)
|
std::vector<std::vector<int>>
|
||||||
{
|
diagonalSort(std::vector<std::vector<int>> mat) {
|
||||||
for (auto d : diagonals(mat)) {
|
for (auto d : diagonals(mat)) {
|
||||||
std::sort(d.begin(), d.end());
|
std::sort(d.begin(), d.end());
|
||||||
}
|
}
|
||||||
|
@ -185,24 +157,19 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
static void
|
static void test_case_1() {
|
||||||
test_case_1()
|
|
||||||
{
|
|
||||||
// Input: mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]
|
// Input: mat = [[3,3,1,1],[2,2,1,2],[1,1,1,2]]
|
||||||
// Output: [[1,1,1,1],[1,2,2,2],[1,2,3,3]]
|
// Output: [[1,1,1,1],[1,2,2,2],[1,2,3,3]]
|
||||||
|
|
||||||
Solution s;
|
Solution s;
|
||||||
assert((s.diagonalSort(std::vector{std::vector{3, 3, 1, 1},
|
assert((s.diagonalSort(std::vector{std::vector{3, 3, 1, 1},
|
||||||
std::vector{2, 2, 1, 2},
|
std::vector{2, 2, 1, 2},
|
||||||
std::vector { 1, 1, 1, 2 } })
|
std::vector{1, 1, 1, 2}}) ==
|
||||||
== std::vector { std::vector { 1, 1, 1, 1 },
|
std::vector{std::vector{1, 1, 1, 1}, std::vector{1, 2, 2, 2},
|
||||||
std::vector { 1, 2, 2, 2 },
|
|
||||||
std::vector{1, 2, 3, 3}}));
|
std::vector{1, 2, 3, 3}}));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void test_case_2() {
|
||||||
test_case_2()
|
|
||||||
{
|
|
||||||
// Input: mat =
|
// Input: mat =
|
||||||
// [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]]
|
// [[11,25,66,1,69,7],[23,55,17,45,15,52],[75,31,36,44,58,8],[22,27,33,25,68,4],[84,28,14,11,5,50]]
|
||||||
// Output:
|
// Output:
|
||||||
|
@ -213,16 +180,15 @@ test_case_2()
|
||||||
std::vector{23, 55, 17, 45, 15, 52},
|
std::vector{23, 55, 17, 45, 15, 52},
|
||||||
std::vector{75, 31, 36, 44, 58, 8},
|
std::vector{75, 31, 36, 44, 58, 8},
|
||||||
std::vector{22, 27, 33, 25, 68, 4},
|
std::vector{22, 27, 33, 25, 68, 4},
|
||||||
std::vector { 84, 28, 14, 11, 5, 50 } })
|
std::vector{84, 28, 14, 11, 5, 50}}) ==
|
||||||
== std::vector { std::vector { 5, 17, 4, 1, 52, 7 },
|
std::vector{std::vector{5, 17, 4, 1, 52, 7},
|
||||||
std::vector{11, 11, 25, 45, 8, 69},
|
std::vector{11, 11, 25, 45, 8, 69},
|
||||||
std::vector{14, 23, 25, 44, 58, 15},
|
std::vector{14, 23, 25, 44, 58, 15},
|
||||||
std::vector{22, 27, 31, 36, 50, 66},
|
std::vector{22, 27, 31, 36, 50, 66},
|
||||||
std::vector{84, 28, 75, 33, 55, 68}}));
|
std::vector{84, 28, 75, 33, 55, 68}}));
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main() {
|
||||||
{
|
|
||||||
test_case_1();
|
test_case_1();
|
||||||
test_case_2();
|
test_case_2();
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,8 @@ class Solution {
|
||||||
|
|
||||||
struct spiral_indices {
|
struct spiral_indices {
|
||||||
spiral_indices(const std::vector<std::vector<int>> &matrix)
|
spiral_indices(const std::vector<std::vector<int>> &matrix)
|
||||||
: x_bounds{-1, static_cast<int>(matrix.size() ? matrix[0].size() : 0)},
|
: x_bounds{-1,
|
||||||
|
static_cast<int>(matrix.size() ? matrix[0].size() : 0)},
|
||||||
y_bounds{-1, static_cast<int>(matrix.size())} {}
|
y_bounds{-1, static_cast<int>(matrix.size())} {}
|
||||||
|
|
||||||
bool done() const { return !in_bounds(idx); }
|
bool done() const { return !in_bounds(idx); }
|
||||||
|
@ -71,7 +72,8 @@ class Solution {
|
||||||
public:
|
public:
|
||||||
std::vector<std::vector<int>> generateMatrix(int n) {
|
std::vector<std::vector<int>> generateMatrix(int n) {
|
||||||
auto length = static_cast<std::size_t>(n);
|
auto length = static_cast<std::size_t>(n);
|
||||||
std::vector<std::vector<int>> spiral{length, std::vector<int>(length, 0)};
|
std::vector<std::vector<int>> spiral{length,
|
||||||
|
std::vector<int>(length, 0)};
|
||||||
|
|
||||||
int i = 1;
|
int i = 1;
|
||||||
for (spiral_indices idx{spiral}; !idx.done(); ++idx) {
|
for (spiral_indices idx{spiral}; !idx.done(); ++idx) {
|
||||||
|
@ -91,7 +93,8 @@ int main() {
|
||||||
(s.generateMatrix(2) == std::vector<std::vector<int>>{{1, 2}, {4, 3}}));
|
(s.generateMatrix(2) == std::vector<std::vector<int>>{{1, 2}, {4, 3}}));
|
||||||
|
|
||||||
for (auto &&row : s.generateMatrix(3)) {
|
for (auto &&row : s.generateMatrix(3)) {
|
||||||
for (auto x : row) std::cout << x << " ";
|
for (auto x : row)
|
||||||
|
std::cout << x << " ";
|
||||||
std::cout << "\n";
|
std::cout << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
class Solution {
|
class Solution {
|
||||||
int right(const std::vector<int>& row, int size, int idx) const
|
int right(const std::vector<int> &row, int size, int idx) const {
|
||||||
{
|
|
||||||
if (idx >= size) {
|
if (idx >= size) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -8,8 +7,7 @@ class Solution {
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
int uniquePaths(int m, int n)
|
int uniquePaths(int m, int n) {
|
||||||
{
|
|
||||||
std::vector<int> bottom(n, 1);
|
std::vector<int> bottom(n, 1);
|
||||||
|
|
||||||
for (int y = m - 2; y >= 0; y--) {
|
for (int y = m - 2; y >= 0; y--) {
|
||||||
|
|
|
@ -11,26 +11,15 @@ class todo : public std::exception {
|
||||||
std::string cause;
|
std::string cause;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
todo()
|
todo() : cause("Not yet implemented!") {}
|
||||||
: cause("Not yet implemented!")
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
todo(std::string&& excuse)
|
todo(std::string &&excuse) : cause("Not yet implemented: " + excuse) {}
|
||||||
: cause("Not yet implemented: " + excuse)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual const char* what() const throw()
|
virtual const char *what() const throw() { return cause.c_str(); }
|
||||||
{
|
|
||||||
return cause.c_str();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
auto expected_size(int first) -> std::optional<std::size_t> {
|
auto expected_size(int first) -> std::optional<std::size_t> {
|
||||||
static constexpr std::array<int, 4> HEADERS = {
|
static constexpr std::array<int, 4> HEADERS = {0, 6, 14, 30};
|
||||||
0, 6, 14, 30
|
|
||||||
};
|
|
||||||
|
|
||||||
for (auto i = 0; i < 4; i++) {
|
for (auto i = 0; i < 4; i++) {
|
||||||
auto mask_length = 1 + i + (i > 0);
|
auto mask_length = 1 + i + (i > 0);
|
||||||
|
@ -44,14 +33,13 @@ auto expected_size(int first) -> std::optional<std::size_t> {
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} // namespace
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
static constexpr int CONTINUATION_BYTE = 2;
|
static constexpr int CONTINUATION_BYTE = 2;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
auto validUtf8(const std::vector<int>& data) -> bool
|
auto validUtf8(const std::vector<int> &data) -> bool {
|
||||||
{
|
|
||||||
for (auto i = 0; i < data.size();) {
|
for (auto i = 0; i < data.size();) {
|
||||||
auto expected_length = expected_size(data[i]);
|
auto expected_length = expected_size(data[i]);
|
||||||
if (!expected_length.has_value()) {
|
if (!expected_length.has_value()) {
|
||||||
|
@ -60,7 +48,8 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i + *expected_length > data.size()) {
|
if (i + *expected_length > data.size()) {
|
||||||
// std::cout << "unexpected length of size " << data.size() << " ≠ " << *expected_length << "\n";
|
// std::cout << "unexpected length of size " << data.size() << "
|
||||||
|
// ≠ " << *expected_length << "\n";
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,20 +78,17 @@ public:
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
TEST(examples, valid)
|
TEST(examples, valid) {
|
||||||
{
|
|
||||||
Solution s;
|
Solution s;
|
||||||
ASSERT_TRUE(s.validUtf8(std::vector{197, 130, 1}));
|
ASSERT_TRUE(s.validUtf8(std::vector{197, 130, 1}));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(examples, invalid)
|
TEST(examples, invalid) {
|
||||||
{
|
|
||||||
Solution s;
|
Solution s;
|
||||||
ASSERT_FALSE(s.validUtf8(std::vector{235, 140, 4}));
|
ASSERT_FALSE(s.validUtf8(std::vector{235, 140, 4}));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(valid, ascii_byte)
|
TEST(valid, ascii_byte) {
|
||||||
{
|
|
||||||
Solution s;
|
Solution s;
|
||||||
ASSERT_TRUE(s.validUtf8(std::vector{64}));
|
ASSERT_TRUE(s.validUtf8(std::vector{64}));
|
||||||
}
|
}
|
||||||
|
@ -112,8 +98,7 @@ TEST(invalid, just_one_byte) {
|
||||||
ASSERT_FALSE(s.validUtf8(std::vector{2 << 7}));
|
ASSERT_FALSE(s.validUtf8(std::vector{2 << 7}));
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char **argv) {
|
||||||
{
|
|
||||||
::testing::InitGoogleTest(&argc, argv);
|
::testing::InitGoogleTest(&argc, argv);
|
||||||
return RUN_ALL_TESTS();
|
return RUN_ALL_TESTS();
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,8 +19,7 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return std::all_of(counter.begin(), counter.end(), [](auto c) {
|
return std::all_of(counter.begin(), counter.end(),
|
||||||
return c == 0;
|
[](auto c) { return c == 0; });
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,8 +3,7 @@
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
bool isValid(const std::string& s)
|
bool isValid(const std::string &s) {
|
||||||
{
|
|
||||||
std::vector<char> st;
|
std::vector<char> st;
|
||||||
|
|
||||||
for (auto c : s) {
|
for (auto c : s) {
|
||||||
|
|
|
@ -7,24 +7,10 @@ struct TreeNode {
|
||||||
int val;
|
int val;
|
||||||
TreeNode *left;
|
TreeNode *left;
|
||||||
TreeNode *right;
|
TreeNode *right;
|
||||||
TreeNode()
|
TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||||
: val(0)
|
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||||
, left(nullptr)
|
|
||||||
, right(nullptr)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
TreeNode(int x)
|
|
||||||
: val(x)
|
|
||||||
, left(nullptr)
|
|
||||||
, right(nullptr)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
TreeNode(int x, TreeNode *left, TreeNode *right)
|
TreeNode(int x, TreeNode *left, TreeNode *right)
|
||||||
: val(x)
|
: val(x), left(left), right(right) {}
|
||||||
, left(left)
|
|
||||||
, right(right)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
@ -34,25 +20,15 @@ struct TreeNodeHandle {
|
||||||
int x;
|
int x;
|
||||||
int y;
|
int y;
|
||||||
|
|
||||||
TreeNodeHandle(TreeNode* node, int x, int y)
|
TreeNodeHandle(TreeNode *node, int x, int y) : node(node), x(x), y(y) {}
|
||||||
: node(node)
|
|
||||||
, x(x)
|
|
||||||
, y(y)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator==(const TreeNodeHandle& rhs) const
|
bool operator==(const TreeNodeHandle &rhs) const {
|
||||||
{
|
|
||||||
return node == rhs.node && x == rhs.x && y == rhs.y;
|
return node == rhs.node && x == rhs.x && y == rhs.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
int value() const
|
int value() const { return node->val; }
|
||||||
{
|
|
||||||
return node->val;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool operator<(const TreeNodeHandle& rhs) const
|
bool operator<(const TreeNodeHandle &rhs) const {
|
||||||
{
|
|
||||||
if (y != rhs.y) {
|
if (y != rhs.y) {
|
||||||
return y < rhs.y;
|
return y < rhs.y;
|
||||||
}
|
}
|
||||||
|
@ -71,8 +47,7 @@ class verticals {
|
||||||
class verticals_iter {
|
class verticals_iter {
|
||||||
std::deque<TreeNodeHandle> queue;
|
std::deque<TreeNodeHandle> queue;
|
||||||
|
|
||||||
void advance()
|
void advance() {
|
||||||
{
|
|
||||||
auto &n = queue.front();
|
auto &n = queue.front();
|
||||||
|
|
||||||
if (n.node->left != nullptr) {
|
if (n.node->left != nullptr) {
|
||||||
|
@ -80,61 +55,49 @@ class verticals {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (n.node->right != nullptr) {
|
if (n.node->right != nullptr) {
|
||||||
queue.push_back(TreeNodeHandle(n.node->right, n.x + 1, n.y + 1));
|
queue.push_back(
|
||||||
|
TreeNodeHandle(n.node->right, n.x + 1, n.y + 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
verticals_iter(std::deque<TreeNodeHandle> queue)
|
verticals_iter(std::deque<TreeNodeHandle> queue) : queue(queue) {
|
||||||
: queue(queue)
|
|
||||||
{
|
|
||||||
if (queue.front().node == nullptr) {
|
if (queue.front().node == nullptr) {
|
||||||
queue.pop_front();
|
queue.pop_front();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(const verticals_iter& other) const
|
bool operator!=(const verticals_iter &other) const {
|
||||||
{
|
|
||||||
return queue != other.queue;
|
return queue != other.queue;
|
||||||
}
|
}
|
||||||
|
|
||||||
verticals_iter operator++()
|
verticals_iter operator++() {
|
||||||
{
|
|
||||||
advance();
|
advance();
|
||||||
queue.pop_front();
|
queue.pop_front();
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
TreeNodeHandle& operator*()
|
TreeNodeHandle &operator*() { return queue.front(); }
|
||||||
{
|
|
||||||
return queue.front();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
verticals(TreeNode* root)
|
verticals(TreeNode *root) : root(root) {}
|
||||||
: root(root)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
verticals_iter begin() const
|
verticals_iter begin() const {
|
||||||
{
|
|
||||||
return verticals_iter(std::deque{TreeNodeHandle(root, 0, 0)});
|
return verticals_iter(std::deque{TreeNodeHandle(root, 0, 0)});
|
||||||
}
|
}
|
||||||
|
|
||||||
verticals_iter end() const
|
verticals_iter end() const {
|
||||||
{
|
|
||||||
std::deque<TreeNodeHandle> q;
|
std::deque<TreeNodeHandle> q;
|
||||||
return verticals_iter(q);
|
return verticals_iter(q);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
} // namespace
|
||||||
|
|
||||||
class Solution {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
std::vector<std::vector<int>> verticalTraversal(TreeNode* root)
|
std::vector<std::vector<int>> verticalTraversal(TreeNode *root) {
|
||||||
{
|
|
||||||
std::map<int, std::vector<TreeNodeHandle>> traversals;
|
std::map<int, std::vector<TreeNodeHandle>> traversals;
|
||||||
|
|
||||||
int min_x = 0;
|
int min_x = 0;
|
||||||
|
@ -172,8 +135,7 @@ public:
|
||||||
|
|
||||||
namespace _tests {
|
namespace _tests {
|
||||||
|
|
||||||
TreeNode* construct_tree(const std::vector<int>& values, std::size_t i = 0)
|
TreeNode *construct_tree(const std::vector<int> &values, std::size_t i = 0) {
|
||||||
{
|
|
||||||
if (i >= values.size() || values[i] == -1) {
|
if (i >= values.size() || values[i] == -1) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
@ -185,8 +147,7 @@ TreeNode* construct_tree(const std::vector<int>& values, std::size_t i = 0)
|
||||||
return tree;
|
return tree;
|
||||||
}
|
}
|
||||||
|
|
||||||
void destruct_tree(TreeNode* ptr)
|
void destruct_tree(TreeNode *ptr) {
|
||||||
{
|
|
||||||
if (ptr == nullptr) {
|
if (ptr == nullptr) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -196,46 +157,49 @@ void destruct_tree(TreeNode* ptr)
|
||||||
delete ptr;
|
delete ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} // namespace _tests
|
||||||
|
|
||||||
TEST(examples, first)
|
TEST(examples, first) {
|
||||||
{
|
|
||||||
Solution s;
|
Solution s;
|
||||||
|
|
||||||
auto t = _tests::construct_tree(std::vector{3, 9, 20, -1, -1, 15, 7});
|
auto t = _tests::construct_tree(std::vector{3, 9, 20, -1, -1, 15, 7});
|
||||||
EXPECT_EQ(s.verticalTraversal(t), (std::vector { std::vector { 9 }, std::vector { 3, 15 }, std::vector { 20 }, std::vector { 7 } }));
|
EXPECT_EQ(s.verticalTraversal(t),
|
||||||
|
(std::vector{std::vector{9}, std::vector{3, 15}, std::vector{20},
|
||||||
|
std::vector{7}}));
|
||||||
_tests::destruct_tree(t);
|
_tests::destruct_tree(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(examples, second)
|
TEST(examples, second) {
|
||||||
{
|
|
||||||
Solution s;
|
Solution s;
|
||||||
|
|
||||||
auto t = _tests::construct_tree(std::vector{1, 2, 3, 4, 5, 6, 7});
|
auto t = _tests::construct_tree(std::vector{1, 2, 3, 4, 5, 6, 7});
|
||||||
EXPECT_EQ(s.verticalTraversal(t), (std::vector { std::vector { 4 }, std::vector { 2 }, std::vector { 1, 5, 6 }, std::vector { 3 }, std::vector { 7 } }));
|
EXPECT_EQ(s.verticalTraversal(t),
|
||||||
|
(std::vector{std::vector{4}, std::vector{2}, std::vector{1, 5, 6},
|
||||||
|
std::vector{3}, std::vector{7}}));
|
||||||
_tests::destruct_tree(t);
|
_tests::destruct_tree(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(examples, third)
|
TEST(examples, third) {
|
||||||
{
|
|
||||||
Solution s;
|
Solution s;
|
||||||
|
|
||||||
auto t = _tests::construct_tree(std::vector{1, 2, 3, 4, 6, 5, 7});
|
auto t = _tests::construct_tree(std::vector{1, 2, 3, 4, 6, 5, 7});
|
||||||
EXPECT_EQ(s.verticalTraversal(t), (std::vector { std::vector { 4 }, std::vector { 2 }, std::vector { 1, 5, 6 }, std::vector { 3 }, std::vector { 7 } }));
|
EXPECT_EQ(s.verticalTraversal(t),
|
||||||
|
(std::vector{std::vector{4}, std::vector{2}, std::vector{1, 5, 6},
|
||||||
|
std::vector{3}, std::vector{7}}));
|
||||||
_tests::destruct_tree(t);
|
_tests::destruct_tree(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(submission, first)
|
TEST(submission, first) {
|
||||||
{
|
|
||||||
Solution s;
|
Solution s;
|
||||||
|
|
||||||
auto t = _tests::construct_tree(std::vector{3, 1, 4, 0, 2, 2});
|
auto t = _tests::construct_tree(std::vector{3, 1, 4, 0, 2, 2});
|
||||||
EXPECT_EQ(s.verticalTraversal(t), (std::vector { std::vector { 0 }, std::vector { 1 }, std::vector { 3, 2, 2 }, std::vector { 4 } }));
|
EXPECT_EQ(s.verticalTraversal(t),
|
||||||
|
(std::vector{std::vector{0}, std::vector{1}, std::vector{3, 2, 2},
|
||||||
|
std::vector{4}}));
|
||||||
_tests::destruct_tree(t);
|
_tests::destruct_tree(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char **argv) {
|
||||||
{
|
|
||||||
::testing::InitGoogleTest(&argc, argv);
|
::testing::InitGoogleTest(&argc, argv);
|
||||||
return RUN_ALL_TESTS();
|
return RUN_ALL_TESTS();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue