Skip to content

Commit 0399e12

Browse files
committed
fix test
1 parent 62b5ec7 commit 0399e12

48 files changed

Lines changed: 1423 additions & 1264 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
24 KB
Binary file not shown.

src/3_Stack_Queue/circular_queue.cpp

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -50,43 +50,6 @@
5050
#include <string>
5151
#include <vector>
5252

53-
namespace {
54-
struct TestRunner {
55-
int total = 0;
56-
int failed = 0;
57-
58-
void expectEqual(const std::vector<int> &got,
59-
const std::vector<int> &expected, const std::string &label) {
60-
++total;
61-
if (got == expected) {
62-
std::cout << "[PASS] " << label << "\n";
63-
return;
64-
}
65-
++failed;
66-
std::cout << "[FAIL] " << label << " expected=" << toString(expected)
67-
<< " got=" << toString(got) << "\n";
68-
}
69-
70-
void summary() const {
71-
std::cout << "Tests: " << total - failed << " passed, " << failed
72-
<< " failed, " << total << " total\n";
73-
}
74-
75-
private:
76-
static std::string toString(const std::vector<int> &values) {
77-
std::ostringstream oss;
78-
oss << "{";
79-
for (size_t i = 0; i < values.size(); ++i) {
80-
if (i > 0)
81-
oss << ", ";
82-
oss << values[i];
83-
}
84-
oss << "}";
85-
return oss.str();
86-
}
87-
};
88-
} // namespace
89-
9053
// Simple (Brute-force) Solution
9154
// Uses a vector to simulate the queue; on deQueue, it shifts elements.
9255
// Complexity: O(n) per deQueue operation due to element shifting.
@@ -174,6 +137,43 @@ alternativeSolution(int capacity, const std::vector<std::string> &operations) {
174137
return std::vector<int>(dq.begin(), dq.end());
175138
}
176139

140+
namespace {
141+
struct TestRunner {
142+
int total = 0;
143+
int failed = 0;
144+
145+
void expectEqual(const std::vector<int> &got,
146+
const std::vector<int> &expected, const std::string &label) {
147+
++total;
148+
if (got == expected) {
149+
std::cout << "[PASS] " << label << "\n";
150+
return;
151+
}
152+
++failed;
153+
std::cout << "[FAIL] " << label << " expected=" << toString(expected)
154+
<< " got=" << toString(got) << "\n";
155+
}
156+
157+
void summary() const {
158+
std::cout << "Tests: " << total - failed << " passed, " << failed
159+
<< " failed, " << total << " total\n";
160+
}
161+
162+
private:
163+
static std::string toString(const std::vector<int> &values) {
164+
std::ostringstream oss;
165+
oss << "{";
166+
for (size_t i = 0; i < values.size(); ++i) {
167+
if (i > 0)
168+
oss << ", ";
169+
oss << values[i];
170+
}
171+
oss << "}";
172+
return oss.str();
173+
}
174+
};
175+
} // namespace
176+
177177
// Test cases for correctness
178178
void test() {
179179
int capacity = 3;

src/3_Stack_Queue/k_least_numbers.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@
3939
// 1) Full Sort
4040
// Time: O(n log n)
4141
// Space: O(n)
42-
std::vector<int> findSmallestKNumbersSort(const std::vector<int>& input, int k) {
43-
if (k <= 0) return {};
42+
std::vector<int> findSmallestKNumbersSort(const std::vector<int> &input,
43+
int k) {
44+
if (k <= 0)
45+
return {};
4446

4547
std::vector<int> result = input;
4648
std::sort(result.begin(), result.end());
@@ -54,8 +56,10 @@ std::vector<int> findSmallestKNumbersSort(const std::vector<int>& input, int k)
5456
// 2) Max Heap (size k)
5557
// Time: O(n log k)
5658
// Space: O(k)
57-
std::vector<int> findSmallestKNumbersHeap(const std::vector<int>& input, int k) {
58-
if (k <= 0) return {};
59+
std::vector<int> findSmallestKNumbersHeap(const std::vector<int> &input,
60+
int k) {
61+
if (k <= 0)
62+
return {};
5963
if (k >= static_cast<int>(input.size())) {
6064
std::vector<int> result = input;
6165
std::sort(result.begin(), result.end());
@@ -84,9 +88,10 @@ std::vector<int> findSmallestKNumbersHeap(const std::vector<int>& input, int k)
8488
// 3) nth_element
8589
// Avg Time: O(n), Worst: O(n^2)
8690
// Space: O(n)
87-
std::vector<int> findSmallestKNumbersNthElement(const std::vector<int>& input,
91+
std::vector<int> findSmallestKNumbersNthElement(const std::vector<int> &input,
8892
int k) {
89-
if (k <= 0) return {};
93+
if (k <= 0)
94+
return {};
9095
if (k >= static_cast<int>(input.size())) {
9196
std::vector<int> result = input;
9297
std::sort(result.begin(), result.end());

src/3_Stack_Queue/max_queue.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,6 @@
3838
#include <string>
3939
#include <vector>
4040

41-
namespace {
42-
struct TestRunner {
43-
int total = 0;
44-
int failed = 0;
45-
46-
void expectEqual(int got, int expected, const std::string &label) {
47-
++total;
48-
if (got == expected) {
49-
std::cout << "[PASS] " << label << "\n";
50-
return;
51-
}
52-
++failed;
53-
std::cout << "[FAIL] " << label << " expected=" << expected
54-
<< " got=" << got << "\n";
55-
}
56-
57-
void summary() const {
58-
std::cout << "Tests: " << total - failed << " passed, " << failed
59-
<< " failed, " << total << " total\n";
60-
}
61-
};
62-
} // namespace
63-
6441
// Simple (Brute-force) Solution
6542
// Uses a deque for queue operations and scans the entire queue on each max()
6643
// call. Complexity: O(n) for max() operation.
@@ -159,6 +136,29 @@ class AlternativeMaxQueue {
159136
std::multiset<int> mset;
160137
};
161138

139+
namespace {
140+
struct TestRunner {
141+
int total = 0;
142+
int failed = 0;
143+
144+
void expectEqual(int got, int expected, const std::string &label) {
145+
++total;
146+
if (got == expected) {
147+
std::cout << "[PASS] " << label << "\n";
148+
return;
149+
}
150+
++failed;
151+
std::cout << "[FAIL] " << label << " expected=" << expected
152+
<< " got=" << got << "\n";
153+
}
154+
155+
void summary() const {
156+
std::cout << "Tests: " << total - failed << " passed, " << failed
157+
<< " failed, " << total << " total\n";
158+
}
159+
};
160+
} // namespace
161+
162162
// Test cases for correctness
163163
void test() {
164164
// Simulate a series of operations:

0 commit comments

Comments
 (0)