diff options
Diffstat (limited to 'top-interview-questions/easy/design')
-rw-r--r-- | top-interview-questions/easy/design/01_shuffle_an_array.cc | 41 | ||||
-rw-r--r-- | top-interview-questions/easy/design/02_min_stack.cc | 56 |
2 files changed, 97 insertions, 0 deletions
diff --git a/top-interview-questions/easy/design/01_shuffle_an_array.cc b/top-interview-questions/easy/design/01_shuffle_an_array.cc new file mode 100644 index 0000000..5c40d60 --- /dev/null +++ b/top-interview-questions/easy/design/01_shuffle_an_array.cc | |||
@@ -0,0 +1,41 @@ | |||
1 | class Solution { | ||
2 | public: | ||
3 | Solution(vector<int>& nums) | ||
4 | : m_original(nums) {} | ||
5 | |||
6 | vector<int> reset() { | ||
7 | return m_original; | ||
8 | } | ||
9 | |||
10 | vector<int> shuffle() { | ||
11 | std::vector<int> shuffled = m_original; | ||
12 | for (size_t i = 0; i < shuffled.size(); ++i) { | ||
13 | const size_t j = random() % shuffled.size(); | ||
14 | std::swap(shuffled[i], shuffled[j]); | ||
15 | } | ||
16 | return shuffled; | ||
17 | } | ||
18 | |||
19 | private: | ||
20 | size_t random() { | ||
21 | uint64_t x = m_random; | ||
22 | |||
23 | // xorshift64 | ||
24 | x ^= x << 13; | ||
25 | x ^= x >> 7; | ||
26 | x ^= x << 17; | ||
27 | |||
28 | m_random = x; | ||
29 | return x; | ||
30 | } | ||
31 | |||
32 | std::vector<int> m_original; | ||
33 | std::uint64_t m_random = 16240738485554559101; | ||
34 | }; | ||
35 | |||
36 | /** | ||
37 | * Your Solution object will be instantiated and called as such: | ||
38 | * Solution* obj = new Solution(nums); | ||
39 | * vector<int> param_1 = obj->reset(); | ||
40 | * vector<int> param_2 = obj->shuffle(); | ||
41 | */ | ||
diff --git a/top-interview-questions/easy/design/02_min_stack.cc b/top-interview-questions/easy/design/02_min_stack.cc new file mode 100644 index 0000000..1bcb36a --- /dev/null +++ b/top-interview-questions/easy/design/02_min_stack.cc | |||
@@ -0,0 +1,56 @@ | |||
1 | class MinStack { | ||
2 | public: | ||
3 | MinStack() {} | ||
4 | |||
5 | void push(int val) { | ||
6 | const int minVal = (m_minStack != nullptr) ? std::min(m_minStack->val, val) : val; | ||
7 | Node* top = new Node{m_stack, val}; | ||
8 | Node* minTop = new Node{m_minStack, minVal}; | ||
9 | m_stack = top; | ||
10 | m_minStack = minTop; | ||
11 | } | ||
12 | |||
13 | void pop() { | ||
14 | assertNonEmpty(); | ||
15 | Node* top = m_stack; | ||
16 | Node* minTop = m_minStack; | ||
17 | m_stack = m_stack->next; | ||
18 | m_minStack = m_minStack->next; | ||
19 | delete top; | ||
20 | delete minTop; | ||
21 | } | ||
22 | |||
23 | int top() { | ||
24 | assertNonEmpty(); | ||
25 | return m_stack->val; | ||
26 | } | ||
27 | |||
28 | int getMin() { | ||
29 | assertNonEmpty(); | ||
30 | return m_minStack->val; | ||
31 | } | ||
32 | |||
33 | private: | ||
34 | struct Node { | ||
35 | Node* next; | ||
36 | int val; | ||
37 | }; | ||
38 | |||
39 | void assertNonEmpty() const { | ||
40 | if (m_stack == nullptr) { | ||
41 | throw "empty stack"; | ||
42 | } | ||
43 | } | ||
44 | |||
45 | Node* m_stack = nullptr; // The actual stack. | ||
46 | Node* m_minStack = nullptr; // Stack of mins. | ||
47 | }; | ||
48 | |||
49 | /** | ||
50 | * Your MinStack object will be instantiated and called as such: | ||
51 | * MinStack* obj = new MinStack(); | ||
52 | * obj->push(val); | ||
53 | * obj->pop(); | ||
54 | * int param_3 = obj->top(); | ||
55 | * int param_4 = obj->getMin(); | ||
56 | */ | ||