Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0b9d5d8

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parentf70862c commit0b9d5d8

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
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O1 -otest 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+
staticintcompare(constvoid*a,constvoid*b)
6+
{
7+
intwa= ((constint*)a)[0];
8+
intwb= ((constint*)b)[0];
9+
intha= ((constint*)a)[1];
10+
inthb= ((constint*)b)[1];
11+
returnwa==wb ?hb-ha :wa-wb;
12+
}
13+
14+
staticintbinary_search(int*nums,intlo,inthi,inttarget)
15+
{
16+
while (lo+1<hi) {
17+
intmid=lo+ (hi-lo) /2;
18+
if (nums[mid]<target) {
19+
lo=mid;
20+
}else {
21+
hi=mid;
22+
}
23+
}
24+
returnhi;
25+
}
26+
27+
intmaxEnvelopes(int**envelopes,intenvelopesSize,int*envelopesColSize)
28+
{
29+
if (envelopesSize==0) {
30+
return0;
31+
}
32+
33+
intsize=envelopesColSize[0];
34+
inti,*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+
intpiles=0;
42+
int*heights=malloc(envelopesSize*sizeof(int));
43+
for (i=0;i<envelopesSize;i++) {
44+
intpos=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+
returnpiles;
51+
}
52+
53+
intmain(intargc,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+
inti,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+
return0;
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+
usingnamespacestd;
4+
5+
classSolution {
6+
public:
7+
intmaxEnvelopes(vector<vector<int>>& envelopes) {
8+
vector<int> piles;
9+
sort(envelopes.begin(), envelopes.end(), compare);
10+
for (constauto& 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+
staticboolcompare(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+
intbinary_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)

[8]ページ先頭

©2009-2025 Movatter.jp