java greedy solution
\nclass Solution {\n    public int[] maxNumber(int[] A, int[] B, int k) {\n        if(A.length==0)return get(B,k,true);\n        if(B.length==0)return get(A,k,t
```\nclass Solution {\n    public int[] maxNumber(int[] A, int[] B, int k) {\n        if(A.length==0)return get(B,k,true);\n        if(B.length==0)return get(A,k,true);\n        int res[]=new int[k];\n        for(int i=0;i<k;i++){\n            int A1[]=get(A,i,false);\n            int A2[]=get(B,k-i,false);\n            if(A1==null||A2==null)continue;\n            int newA[]=merge(A1,A2);\n            if(compare(res,newA)){\n                res=newA;\n            }\n        }\n        return res;\n    }\n    \n    public boolean compare(int A[],int B[]){\n        for(int i=0;i<A.length;i++){\n            if(B[i]==A[i])continue;\n            if(B[i]>A[i])return true;\n            else return false;\n        }\n        return false;\n    }\n    \n    public int[] merge(int A[],int B[]){\n        int res[]=new int[A.length+B.length-2];\n        int i=0,j=0,k=0;\n        while(i<A.length-1&&j<B.length-1){\n            if(A[i]>B[j]){\n                res[k++]=A[i++];\n            }else if(B[j]>A[i]){\n                res[k++]=B[j++];\n            }else{//equal\n                int ii=i,jj=j;\n                int prei=i,prej=j;\n                while(ii<A.length-1&&jj<B.length-1){\n                    while(ii<A.length-1&&A[ii]==A[prei])ii++;\n                    while(jj<B.length-1&&B[jj]==B[prej])jj++;\n                    if(ii<A.length-1&&jj<B.length-1&&A[ii]==B[jj]&&(ii-i==jj-j)){\n                        prei=ii;prej=jj;\n                        continue;\n                    }\n                    else break;\n                }\n                if(ii-i==jj-j){\n                    if(A[ii]>B[jj])res[k++]=A[i++];\n                    else res[k++]=B[j++];\n                }else if(ii-i<jj-j){\n                    if(A[ii]>B[prej])res[k++]=A[i++];\n                    else res[k++]=B[j++];\n                }else{\n                   if(B[jj]>A[prei])res[k++]=B[j++];\n                    else res[k++]=A[i++]; \n                }\n                \n            }\n        }\n        while(i<A.length-1)res[k++]=A[i++];\n        while(j<B.length-1)res[k++]=B[j++];\n        return res;\n    }\n    \n    public void print(int A[]){\n        for(int i:A)System.out.print(i+"  ");\n        System.out.println();\n    }\n    \n    public int[] get(int A[],int k,boolean flag){\n        if(k>A.length)return null;\n        Stack<Integer>stack=new Stack<>();\n        int res[]=new int[k];\n        for(int i=0;i<A.length;i++){\n            int remain=A.length-(i+1);\n            while(stack.size()!=0&&A[i]>stack.peek()&&stack.size()+remain>=k)stack.pop();\n            stack.push(A[i]);\n        }\n        int index=res.length-1;\n        while(stack.size()>k)stack.pop();\n        while(stack.size()!=0){\n            res[index--]=stack.pop();\n        }\n        if(flag)return res;\n        int ans[]=new int[res.length+1];\n        for(int i=0;i<res.length;i++)ans[i]=res[i];\n        ans[ans.length-1]=Integer.MIN_VALUE;\n        return ans;\n    }\n}\n\n```
[]