-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path321_Create_Maximum_Number.cpp
105 lines (104 loc) · 2.36 KB
/
321_Create_Maximum_Number.cpp
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
#include<iostream>
#include<vector>
using namespace std;
class Solution{
public:
void findMaxN(int *nums,int n,int *result,int len){
if(n==0)
return;
result[0]=nums[0];
int top=0;
for(int i=1;i<len;i++)
{
int a=nums[i];
while(top>=0&&result[top]<a&&len-i>=n-top)top--;
if(top<n-1)
{
top++;
result[top]=a;
}
}
}
void merge(int* nums1,int size1,int* nums2,int size2,int* result){
int i1=0,i2=0,j=0,k=size1+size2;
for(j=0;j<k;j++)
{
if((i1<size1&&i2<size2&&nums1[i1]>nums2[i2])|| i2>=size2)
result[j]=nums1[i1++];
else if((i1<size1&&i2<size2&&nums1[i1]<nums2[i2])||i1>=size1)
result[j]=nums2[i2++];
else if(i1<size1&&i2<size2&&nums1[i1]==nums2[i2])
{
if(size1-i1<=size2-i2)
{
if(memcmp(&nums1[i1],&nums2[i2],(size1-i1)*sizeof(int))<=0)
result[j]=nums2[i2++];
else
result[j]=nums1[i1++];
}
else
{
if(memcmp(&nums1[i1],&nums2[i2],(size2-i2)*sizeof(int))<0)
result[j]=(nums2[i2++]);
else
result[j]=nums1[i1++];
}
}
}
}
vector<int> maxNumber(vector<int>& nums1, vector<int>& nums2, int k) {
int i=0,n1=0,n2=0;
int *result = new int[k*4];
int *s1=result+k,*s2=s1+k,*temp=s2+k;
memset(result, 0, k*4*sizeof(int));
if(nums1.size()==0&&nums2.size()>0)
findMaxN(&nums2[0],k,result,nums2.size());
else if(nums2.size()==0&&nums1.size()>0)
findMaxN(&nums1[0],k,result,nums1.size());
else
{
for(i=0;i<=k;i++)
{
n1=nums1.size()>=i?i:nums1.size();
n2=k-n1;
if(n2>nums2.size())
continue;
findMaxN(&nums1[0],n1,s1,nums1.size());
findMaxN(&nums2[0],n2,s2,nums2.size());
merge(s1,n1,s2,n2,temp);
if(memcmp(&temp[0],&result[0],k*sizeof(int))>0)
memcpy(&result[0],&temp[0],k*sizeof(int));
}
}
vector<int> r(result,result+k);
delete []result;
return r;
}
};
int main()
{
vector<int> nums1,nums2;
nums1.push_back(3);
nums1.push_back(4);
nums1.push_back(6);
nums1.push_back(5);
nums2.push_back(9);
nums2.push_back(1);
nums2.push_back(2);
nums2.push_back(5);
nums2.push_back(8);
nums2.push_back(3);
//nums.push_back(8);
//nums.push_back(6);
vector<int> test;
test.push_back(1);
test.push_back(4);
test.push_back(5);
Solution s;
vector<int> result = s.maxNumber(nums1,nums2,5);
//vector<int> result = s.findMaxN(nums2,6);
for(int i=0;i<result.size();i++)
cout<<result.at(i)<<" ";
cout<<endl;
return 0;
}