Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int makeArrayIncreasing(int[] arr1, int[] arr2) {
- int N = arr1.length, M = arr2.length;
- Arrays.sort(arr2);
- int[][] dp = new int[N][M+1];
- for (int j = 0; j < M; ++j) {
- dp[0][j] = 1;
- }
- dp[0][M] = 0;
- for (int i = 1; i < N; ++i) {
- Arrays.fill(dp[i], Integer.MAX_VALUE);
- }
- for (int i = 1; i < N; ++i) {
- for (int j = 0; j <= M; ++j) {
- for (int l = 0; l <= M; ++l) {
- if (j < M) {
- if ((l < j && arr2[l] < arr2[j]) ||
- (l == M && arr1[i-1] < arr2[j]))
- dp[i][j] = Math.min(dp[i][j], dp[i-1][l]+ 1) ;
- } else {
- if ((l < M && arr2[l] < arr1[i]) || (l == M && arr1[i-1] < arr1[i])) {
- dp[i][j] = Math.min(dp[i][j], dp[i-1][l]);
- }
- }
- }
- }
- }
- int ans = Integer.MAX_VALUE;
- for (int x:dp[N-1]) {
- ans = Math.min(ans, x);
- }
- return ans == Integer.MAX_VALUE ? -1 : ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement