# 023. Merge k Sorted Lists

Difficulty: Hard

Frequency: N/A

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

My solution:
Data structure:
Steps:
Complexity:
Runtime:
Space:
Test cases:
Corner cases:
Code:
```/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
private:
struct compare {
bool operator() (const ListNode* l, const ListNode* r) {
return l->val > r->val;
}
};
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
priority_queue<ListNode*, vector<ListNode*>, compare> pq;
for (ListNode* l : lists) if (l) pq.push(l);
while (!pq.empty()) {
cur->next = pq.top();
pq.pop();
cur = cur->next;
if (cur->next) pq.push(cur->next);
}
}
};
```

Another solution:
Data structure:
steps:
Complexity:
Runtime:
Space:
Code:

Things to learn:

# 004. Median of Two Sorted Arrays

Difficulty: Hard

Frequency: N/A

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).

My solution:
Data structure:
Steps:
Complexity:
Runtime:
Space:
Test cases:
Corner cases:
Code:
```class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int m = nums1.size(), n = nums2.size(), total = m + n;
if (total % 2 == 1) return findK(nums1, 0, m, nums2, 0, n, total / 2 + 1);
else return 0.5 * (findK(nums1, 0, m, nums2, 0, n, total / 2) +
findK(nums1, 0, m, nums2, 0, n, total / 2 + 1));

}
int findK(vector<int>& nums1, int start1, int m, vector<int>& nums2, int start2, int n, int k) {
if (m > n) return findK(nums2, start2, n, nums1, start1, m, k);
if (m == 0) return nums2[start2 + k - 1];
if (k == 1) return min(nums1[start1], nums2[start2]);
int a = min(k / 2, m);
int b = k - a;
if (nums1[start1 + a - 1] <= nums2[start2 + b - 1]) return findK(nums1, start1 + a, m - a, nums2, start2, n, k - a);
else return findK(nums1, start1, m, nums2, start2 + b, n - b, k - b);
}
};
```

Another solution:
Data structure:
steps:
Complexity:
Runtime:
Space:
Code:

Things to learn:

# 215. Kth Largest Element in an Array

Difficulty: Medium

Frequency: N/A

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

For example,
Given `[3,2,1,5,6,4]` and k = 2, return 5.

Note:
You may assume k is always valid, 1 ≤ k ≤ array’s length.

My solution:
Data structure:
Steps:
Complexity:
Runtime:
Space:
Test cases:
Corner cases:
Code:
```class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
priority_queue<int> q;
for (int n : nums) q.push(n);
for (int i = 1; i < k; i++) q.pop();
return q.top();
}
};
```

Another solution:
Data structure:
steps:
Complexity:
Runtime:
Space:
Code:

Things to learn:

# 240. Search a 2D Matrix II

Difficulty: Medium

Frequency: N/A

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

• Integers in each row are sorted in ascending from left to right.
• Integers in each column are sorted in ascending from top to bottom.

For example,

Consider the following matrix:

```[
[1,   4,  7, 11, 15],
[2,   5,  8, 12, 19],
[3,   6,  9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
```

Given target = `5`, return `true`.

Given target = `20`, return `false`.

My solution:
Data structure:
Steps:
Complexity:
Runtime:
Space:
Test cases:
Corner cases:
Code:
```class Solution {
public:
bool searchMatrix(vector<vector<int>>& matrix, int target) {
if (matrix.empty()) return false;
int m = matrix.size(), n = matrix[0].size();
int i = 0, j = n - 1;
while (i < m && j >= 0) {
if (matrix[i][j] == target) return true;
else if (matrix[i][j] < target) i++;
else j--;
}
return false;
}
};
```

Another solution:
Data structure:
steps:
Complexity:
Runtime:
Space:
Code:

Things to learn:

# 241. Different Ways to Add Parentheses

Difficulty: Medium

Frequency: N/A

Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are `+`,`-` and `*`.
Example 1Input: `"2-1-1"`.

```((2-1)-1) = 0
(2-(1-1)) = 2```

Output: `[0, 2]`
Example 2Input: `"2*3-4*5"`

```(2*(3-(4*5))) = -34
((2*3)-(4*5)) = -14
((2*(3-4))*5) = -10
(2*((3-4)*5)) = -10
(((2*3)-4)*5) = 10```

Output: `[-34, -14, -10, -10, 10]`

My solution:
Data structure:
Steps:
Complexity:
Runtime:
Space:
Test cases:
Corner cases:
Code:
```class Solution {
public:
vector<int> diffWaysToCompute(string input) {
vector<int> results;
for (int i = 0; i < input.size(); i++) {
char c = input[i];
if (!isdigit(c)) {
vector<int> left = diffWaysToCompute(input.substr(0, i));
vector<int> right = diffWaysToCompute(input.substr(i + 1));
for (int l : left) {
for (int r :right) {
switch(c) {
case '+': results.push_back(l + r); break;
case '-': results.push_back(l - r); break;
case '*': results.push_back(l * r); break;
}
}
}
}
}
if (results.empty()) results.push_back(stoi(input));
return results;
}
};
```

Another solution:
Data structure:
steps:
Complexity:
Runtime:
Space:
Code:

Things to learn: