Skip to content

Commit 0b9d5d8

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent f70862c commit 0b9d5d8

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

0300_longest_increasing_subsequence/lis.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Solution {
88
vector<int> dp(nums.size(), 1);
99
for (int i = 0; i < nums.size(); i++) {
1010
for (int j = 0; j < i; j++) {
11+
// nums[i] should be contained as the last element in subsequence.
1112
if (nums[j] < nums[i] && dp[j] + 1 > dp[i]) {
1213
dp[i] = dp[j] + 1;
1314
}

0354_russian_doll_envelopes/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O1 -o test russian_doll.c
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
5+
static int compare(const void *a, const void *b)
6+
{
7+
int wa = ((const int *)a)[0];
8+
int wb = ((const int *)b)[0];
9+
int ha = ((const int *)a)[1];
10+
int hb = ((const int *)b)[1];
11+
return wa == wb ? hb - ha : wa - wb;
12+
}
13+
14+
static int binary_search(int *nums, int lo, int hi, int target)
15+
{
16+
while (lo + 1 < hi) {
17+
int mid = lo + (hi - lo) / 2;
18+
if (nums[mid] < target) {
19+
lo = mid;
20+
} else {
21+
hi = mid;
22+
}
23+
}
24+
return hi;
25+
}
26+
27+
int maxEnvelopes(int** envelopes, int envelopesSize, int* envelopesColSize)
28+
{
29+
if (envelopesSize == 0) {
30+
return 0;
31+
}
32+
33+
int size = envelopesColSize[0];
34+
int i, *tmp = malloc(envelopesSize * size * sizeof(int));
35+
for (i = 0; i < envelopesSize; i++) {
36+
tmp[i * size] = envelopes[i][0];
37+
tmp[i * size + 1] = envelopes[i][1];
38+
}
39+
qsort(tmp, envelopesSize, size * sizeof(int), compare);
40+
41+
int piles = 0;
42+
int *heights = malloc(envelopesSize * sizeof(int));
43+
for (i = 0; i < envelopesSize; i++) {
44+
int pos = binary_search(heights, -1, piles, tmp[i * size + 1]);
45+
if (pos == piles) {
46+
piles++;
47+
}
48+
heights[pos] = tmp[i * size + 1];
49+
}
50+
return piles;
51+
}
52+
53+
int main(int argc, char **argv)
54+
{
55+
if (argc < 3 || argc % 2 == 0) {
56+
fprintf(stderr, "Usage: ./test w0 h0 w1 h1...");
57+
exit(-1);
58+
}
59+
60+
int i, size = (argc - 1) / 2;
61+
int *col_sizes = malloc(size * sizeof(int));
62+
int **envelopes = malloc(size * sizeof(int *));
63+
for (i = 0; i < size; i++) {
64+
col_sizes[i] = 2;
65+
envelopes[i] = malloc(col_sizes[i] * sizeof(int));
66+
envelopes[i][0] = atoi(argv[i * 2 + 1]);
67+
envelopes[i][1] = atoi(argv[i * 2 + 2]);
68+
}
69+
70+
printf("%d\n", maxEnvelopes(envelopes, size, col_sizes));
71+
return 0;
72+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
int maxEnvelopes(vector<vector<int>>& envelopes) {
8+
vector<int> piles;
9+
sort(envelopes.begin(), envelopes.end(), compare);
10+
for (const auto& e : envelopes) {
11+
int pos = binary_search(piles, -1, piles.size(), e[1]);
12+
if (pos == piles.size()) {
13+
piles.push_back(e[1]);
14+
}
15+
piles[pos] = e[1];
16+
}
17+
return piles.size();
18+
}
19+
private:
20+
static bool compare(const vector<int>& a, const vector<int>& b) {
21+
int wa = a[0];
22+
int wb = b[0];
23+
int ha = a[1];
24+
int hb = b[1];
25+
return wa == wb ? ha > hb : wa < wb;
26+
}
27+
28+
int binary_search(vector<int>& nums, int lo, int hi, int target) {
29+
while (lo + 1 < hi) {
30+
int mid = lo + (hi - lo) / 2;
31+
if (nums[mid] < target) {
32+
lo = mid;
33+
} else {
34+
hi = mid;
35+
}
36+
}
37+
return hi;
38+
}
39+
};

0 commit comments

Comments
 (0)