-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday76.cpp
More file actions
87 lines (62 loc) · 2.36 KB
/
day76.cpp
File metadata and controls
87 lines (62 loc) · 2.36 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
/*
Good morning! Here's your coding interview problem for today.
This problem was asked by Google.
You are given an N by M 2D matrix of lowercase letters. Determine the minimum number of columns that can be removed to ensure that each row is ordered from top to bottom lexicographically. That is, the letter at each column is lexicographically later as you go down each row. It does not matter whether each row itself is ordered lexicographically.
For example, given the following table:
cba
daf
ghi
This is not ordered because of the a in the center. We can remove the second column to make it ordered:
ca
df
gi
So your function should return 1, since we only needed to remove 1 column.
As another example, given the following table:
abcdef
Your function should return 0, since the rows are already ordered (there's only one row).
As another example, given the following table:
zyx
wvu
tsr
Your function should return 3, since we would need to remove all the columns to order it.
*/
#include <gtest/gtest.h>
using namespace std;
/**
* Idea: If a row is not lexicographically ordered: we remove the column
*/
int longest_i_subs(vector<int> &subsq) {
vector<int> dp(subsq.size(), 1);
for (size_t i = 0; i < subsq.size(); i++) {
for (size_t j = 0; j < i; j++) {
if (subsq[j] < subsq[i]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
}
return *max_element(dp.begin(), dp.end());
}
TEST(LONGEST_INCREASING_SUSQ, longest_increasing_subsq) {
// Test case 1: Given example in the prompt
vector<int> arr1 = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15};
EXPECT_EQ(longest_i_subs(arr1), 6);
// Test case 2: Single element
vector<int> arr2 = {42};
EXPECT_EQ(longest_i_subs(arr2), 1);
// Test case 3: All elements increasing
vector<int> arr3 = {1, 2, 3, 4, 5};
EXPECT_EQ(longest_i_subs(arr3), 5);
// Test case 4: All elements decreasing
vector<int> arr4 = {5, 4, 3, 2, 1};
EXPECT_EQ(longest_i_subs(arr4), 1);
// Test case 5: Repeated elements
vector<int> arr5 = {2, 2, 2, 2, 2};
EXPECT_EQ(longest_i_subs(arr5), 1);
// Test case 6: Mixed positive and negative elements
vector<int> arr6 = {-1, 2, -3, 4, -5, 6, -7, 8};
EXPECT_EQ(longest_i_subs(arr6), 5); // Example sequence: -1, 2, 4, 6, 8
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}