|
50 | 50 | #include <string> |
51 | 51 | #include <vector> |
52 | 52 |
|
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 | | - |
90 | 53 | // Simple (Brute-force) Solution |
91 | 54 | // Uses a vector to simulate the queue; on deQueue, it shifts elements. |
92 | 55 | // Complexity: O(n) per deQueue operation due to element shifting. |
@@ -174,6 +137,43 @@ alternativeSolution(int capacity, const std::vector<std::string> &operations) { |
174 | 137 | return std::vector<int>(dq.begin(), dq.end()); |
175 | 138 | } |
176 | 139 |
|
| 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 | + |
177 | 177 | // Test cases for correctness |
178 | 178 | void test() { |
179 | 179 | int capacity = 3; |
|
0 commit comments