Skip to content

Commit 7477384

Browse files
committed
reorganize files
1 parent 62b5ec7 commit 7477384

47 files changed

Lines changed: 1267 additions & 1266 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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:

src/3_Stack_Queue/min_in_stack.cpp

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
* for all operations including push, pop, top, and retrieving the minimum
99
* value.
1010
*
11-
* The implementation uses a modified stack mechanism where the pushed value is
12-
* encoded if it is less than the current minimum. This ensures that when an
13-
* element is popped, the previous minimum can be restored in constant time.
14-
*
1511
* ASCII Illustration:
1612
*
1713
* Stack: [ ... | encoded_value ]
@@ -37,39 +33,6 @@
3733
#include <stack>
3834
#include <string>
3935

40-
namespace {
41-
struct TestRunner {
42-
int total = 0;
43-
int failed = 0;
44-
45-
template <typename T>
46-
void expectEqual(const std::optional<T> &got,
47-
const std::optional<T> &expected, const std::string &label) {
48-
++total;
49-
if (got == expected) {
50-
std::cout << "[PASS] " << label << "\n";
51-
return;
52-
}
53-
++failed;
54-
std::cout << "[FAIL] " << label << " expected=" << toString(expected)
55-
<< " got=" << toString(got) << "\n";
56-
}
57-
58-
void summary() const {
59-
std::cout << "Tests: " << total - failed << " passed, " << failed
60-
<< " failed, " << total << " total\n";
61-
}
62-
63-
private:
64-
template <typename T>
65-
static std::string toString(const std::optional<T> &value) {
66-
if (!value.has_value())
67-
return "nullopt";
68-
return std::to_string(*value);
69-
}
70-
};
71-
} // namespace
72-
7336
// Template class for a stack that tracks the minimum element in O(1) time.
7437
template <typename T> class StackWithMin {
7538
public:
@@ -118,6 +81,39 @@ template <typename T> class StackWithMin {
11881
std::optional<T> min_value_;
11982
};
12083

84+
namespace {
85+
struct TestRunner {
86+
int total = 0;
87+
int failed = 0;
88+
89+
template <typename T>
90+
void expectEqual(const std::optional<T> &got,
91+
const std::optional<T> &expected, const std::string &label) {
92+
++total;
93+
if (got == expected) {
94+
std::cout << "[PASS] " << label << "\n";
95+
return;
96+
}
97+
++failed;
98+
std::cout << "[FAIL] " << label << " expected=" << toString(expected)
99+
<< " got=" << toString(got) << "\n";
100+
}
101+
102+
void summary() const {
103+
std::cout << "Tests: " << total - failed << " passed, " << failed
104+
<< " failed, " << total << " total\n";
105+
}
106+
107+
private:
108+
template <typename T>
109+
static std::string toString(const std::optional<T> &value) {
110+
if (!value.has_value())
111+
return "nullopt";
112+
return std::to_string(*value);
113+
}
114+
};
115+
} // namespace
116+
121117
// Test cases to ensure correctness of StackWithMin operations.
122118
void test() {
123119
TestRunner runner;

src/3_Stack_Queue/stack_push_pop_order.cpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
*
99
* ASCII Illustration:
1010
*
11-
* Push Order: 1 2 3 4 5
12-
* | | | | |
13-
* [Stack Simulation]
14-
* | | | | |
15-
* Example Valid Pop Order: 4 5 3 2 1
11+
* Push Order: 1 2 3 4 5
12+
* | | | | |
13+
* [Stack Simulation]
14+
* | | | | |
15+
* Example Valid Pop Order: 4 5 3 2 1
1616
*
1717
* Example:
1818
* Push Order: [1, 2, 3, 4, 5]
@@ -31,29 +31,6 @@
3131
#include <string>
3232
#include <vector>
3333

34-
namespace {
35-
struct TestRunner {
36-
int total = 0;
37-
int failed = 0;
38-
39-
void expectEqual(bool got, bool expected, const std::string &label) {
40-
++total;
41-
if (got == expected) {
42-
std::cout << "[PASS] " << label << "\n";
43-
return;
44-
}
45-
++failed;
46-
std::cout << "[FAIL] " << label << " expected=" << std::boolalpha
47-
<< expected << " got=" << got << "\n";
48-
}
49-
50-
void summary() const {
51-
std::cout << "Tests: " << total - failed << " passed, " << failed
52-
<< " failed, " << total << " total\n";
53-
}
54-
};
55-
} // namespace
56-
5734
// Simulation Solution: Efficient O(n) approach.
5835
bool isValidPopOrderSimulation(const std::vector<int> &push_order,
5936
const std::vector<int> &pop_order) {
@@ -130,6 +107,29 @@ bool isValidPopOrderRecursive(const std::vector<int> &push_order,
130107
return false;
131108
}
132109

110+
namespace {
111+
struct TestRunner {
112+
int total = 0;
113+
int failed = 0;
114+
115+
void expectEqual(bool got, bool expected, const std::string &label) {
116+
++total;
117+
if (got == expected) {
118+
std::cout << "[PASS] " << label << "\n";
119+
return;
120+
}
121+
++failed;
122+
std::cout << "[FAIL] " << label << " expected=" << std::boolalpha
123+
<< expected << " got=" << got << "\n";
124+
}
125+
126+
void summary() const {
127+
std::cout << "Tests: " << total - failed << " passed, " << failed
128+
<< " failed, " << total << " total\n";
129+
}
130+
};
131+
} // namespace
132+
133133
// Test cases to verify both solutions.
134134
void test() {
135135
std::vector<int> push_order = {1, 2, 3, 4, 5};

0 commit comments

Comments
 (0)