-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
189 lines (164 loc) · 4.28 KB
/
main.cpp
File metadata and controls
189 lines (164 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Source: https://leetcode.com/problems/find-smallest-common-element-in-all-rows
// Title: Find Smallest Common Element in All Rows
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Given an `m x n` matrix `mat` where every row is sorted in **strictly** **increasing** order, return the **smallest common element** in all rows.
//
// If there is no common element, return `-1`.
//
// **Example 1:**
//
// ```
// Input: mat = [[1,2,3,4,5],[2,4,5,8,10],[3,5,7,9,11],[1,3,5,7,9]]
// Output: 5
// ```
//
// **Example 2:**
//
// ```
// Input: mat = [[1,2,3],[2,3,4],[2,3,5]]
// Output: 2
// ```
//
// **Constraints:**
//
// - `m == mat.length`
// - `n == mat[i].length`
// - `1 <= m, n <= 500`
// - `1 <= mat[i][j] <= 10^4`
// - `mat[i]` is sorted in strictly increasing order.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <vector>
using namespace std;
// Use Sort, O(mn log(mn))
//
// Sort all indices
class Solution {
public:
int smallestCommonElement(vector<vector<int>>& mat) {
int m = mat.size();
int n = mat[0].size();
auto idxs = vector<pair<int, int>>(); // indexs of each row
for (auto i = 0; i < m; i++) {
for (auto j = 0; j < n; j++) {
idxs.push_back({i, j});
}
}
// Comp, a, b are valid indices
auto comp = [&](pair<int, int> a, pair<int, int> b) -> bool { //
return mat[a.first][a.second] < mat[b.first][b.second];
};
sort(idxs.begin(), idxs.end(), comp);
auto ans = 0;
auto count = 0;
for (auto [i, j] : idxs) {
auto val = mat[i][j];
if (val == ans) {
count++;
} else {
ans = val;
count = 1;
}
if (count == m) return ans;
}
return -1;
}
};
// Use Priority Queue, O(mn log(m))
class Solution2 {
public:
int smallestCommonElement(vector<vector<int>>& mat) {
int m = mat.size();
int n = mat[0].size();
auto idxs = vector<pair<int, int>>(); // indexs of each row
for (auto i = 0; i < m; i++) {
idxs.push_back({i, 0});
}
// Comp, a, b are valid indices
auto comp = [&](pair<int, int> a, pair<int, int> b) -> bool { //
return mat[a.first][a.second] > mat[b.first][b.second];
};
auto heap = priority_queue(comp, std::move(idxs));
auto ans = 0;
auto count = 0;
while (heap.size() > 0) {
auto item = heap.top();
heap.pop();
// Count
auto val = mat[item.first][item.second];
if (val == ans) {
count++;
} else {
ans = val;
count = 1;
}
if (count == m) return ans;
// Push back
item.second++;
if (item.second < n) {
heap.push(item);
}
}
return -1;
}
};
// Use counter, O(mn)
class Solution3 {
public:
int smallestCommonElement(vector<vector<int>>& mat) {
int m = mat.size();
// Count
auto counter = unordered_map<int, int>();
for (auto& vec : mat) {
for (auto val : vec) {
counter[val]++;
}
}
auto ans = -1;
for (auto [val, count] : counter) {
if (count == m) ans = val;
}
return ans;
}
};
// Use binary search, O(m log n)
//
// Find number of first row in other rows using binary search.
class Solution4 {
public:
int smallestCommonElement(vector<vector<int>>& mat) {
int m = mat.size();
for (auto val : mat[0]) {
auto ok = true;
for (auto j = 1; j < m; j++) {
if (!binary_search(mat[j].cbegin(), mat[j].cend(), val)) {
ok = false;
break;
}
}
if (ok) return val;
}
return -1;
}
};
// Use binary search, O(m log n)
//
// Use functional programming
class Solution5 {
public:
int smallestCommonElement(vector<vector<int>>& mat) {
int m = mat.size();
auto it = find_if(mat[0].cbegin(), mat[0].cend(), [&](const int val) -> bool {
auto find_val = [=](const vector<int>& vec) -> bool { //
return binary_search(vec.cbegin(), vec.cend(), val);
};
return all_of(mat.cbegin() + 1, mat.cend(), find_val);
});
return it != mat[0].cend() ? *it : -1;
}
};