LeetCode //C - 1187. Make Array Strictly Increasing

1187. Make Array Strictly Increasing

Given two integer arrays arr1 and arr2, return the minimum number of operations (possibly zero) needed to make arr1 strictly increasing.

In one operation, you can choose two indices 0 <= i < arr1.length and 0 <= j < arr2.length and do the assignment arr1i = arr2j.

If there is no way to make arr1 strictly increasing, return -1.

Example 1:

Input: arr1 = 1,5,3,6,7, arr2 = 1,3,2,4

Output: 1

Explanation: Replace 5 with 2, then arr1 = 1, 2, 3, 6, 7.

Example 2:

Input: arr1 = 1,5,3,6,7, arr2 = 4,3,1

Output: 2

Explanation: Replace 5 with 3 and then replace 3 with 4. arr1 = 1, 3, 4, 6, 7.

Example 3:

Input: arr1 = 1,5,3,6,7, arr2 = 1,6,3,3

Output: -1

Explanation: You can't make arr1 strictly increasing.

Constraints:
  • 1 <= arr1.length, arr2.length <= 2000
  • 0 <= arr1i, arr2i <= 10^9

From: LeetCode

Link: 1187. Make Array Strictly Increasing


Solution:

Ideas:

dpj means after processing current prefix, using j replacements, the minimum possible last value. Smaller last value is better.

Code:
c 复制代码
#include <stdlib.h>
#include <limits.h>

#define INF 0x3f3f3f3f
#define LLINF 4000000000000000000LL

int cmpInt(const void* a, const void* b) {
    int x = *(int*)a, y = *(int*)b;
    return (x > y) - (x < y);
}

int upperBound(int* arr, int size, long long target) {
    int l = 0, r = size;
    while (l < r) {
        int m = l + (r - l) / 2;
        if ((long long)arr[m] <= target) l = m + 1;
        else r = m;
    }
    return l;
}

int makeArrayIncreasing(int* arr1, int arr1Size, int* arr2, int arr2Size) {
    qsort(arr2, arr2Size, sizeof(int), cmpInt);

    int m = 0;
    for (int i = 0; i < arr2Size; i++) {
        if (i == 0 || arr2[i] != arr2[i - 1]) {
            arr2[m++] = arr2[i];
        }
    }

    long long* dp = (long long*)malloc((arr1Size + 1) * sizeof(long long));
    long long* ndp = (long long*)malloc((arr1Size + 1) * sizeof(long long));

    for (int i = 0; i <= arr1Size; i++) dp[i] = LLINF;
    dp[0] = -1;

    for (int i = 0; i < arr1Size; i++) {
        for (int j = 0; j <= arr1Size; j++) ndp[j] = LLINF;

        for (int j = 0; j <= i; j++) {
            if (dp[j] == LLINF) continue;

            if ((long long)arr1[i] > dp[j]) {
                if (arr1[i] < ndp[j]) ndp[j] = arr1[i];
            }

            int idx = upperBound(arr2, m, dp[j]);
            if (idx < m) {
                if ((long long)arr2[idx] < ndp[j + 1]) {
                    ndp[j + 1] = arr2[idx];
                }
            }
        }

        long long* temp = dp;
        dp = ndp;
        ndp = temp;
    }

    for (int i = 0; i <= arr1Size; i++) {
        if (dp[i] != LLINF) {
            free(dp);
            free(ndp);
            return i;
        }
    }

    free(dp);
    free(ndp);
    return -1;
}
相关推荐
听风说ml9 小时前
socket编程 【C语言】
c语言·udp·tcp·socket编程
学逆向的11 小时前
C 局部静态变量
c语言·开发语言·网络安全
UIU11411 小时前
P1233 [ICPC 2001 Taejon R] 木棍加工
数据结构·c++·算法·动态规划
论文复现现场12 小时前
WRF、FEA 能用 L40S 加速吗?求解器检查、断点恢复与长期运行
java·数据库·算法·wrf·有限元分析·gpu计算·断点续算
ZhengEnCi12 小时前
用 AI 写代码为什么拼到最后还是拼人:算力瓶颈与领域专家提示词的本质差异
算法·架构
秋天的怀念_David13 小时前
第二十一夜 · 数字黑洞与西西弗斯的惩罚
java·程序人生·算法
碧海银沙音频科技研究院13 小时前
在PC电脑上用Whisper→Marian算法实现离线语音翻译功能
算法·whisper
niyongsheng14 小时前
3D 装箱算法工程化:数据结构、放置启发式与支撑规则
数据结构·算法·rust
j7~16 小时前
【穷举、深搜与回溯算法】一棵决策树讲透全排列与子集
数据结构·c++·算法·leetcode·回溯·递归
傅科摆 _ py17 小时前
DP,贪心,堆和栈,二分力扣复习
开发语言·python·算法