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;
}
相关推荐
benchmark_cc8 分钟前
1000只ETF的5分钟K线如何批量获取?QuantDash分页策略与高性能Python实践
开发语言·人工智能·爬虫·python·算法·quantdash·量化数据源
sel_914 分钟前
【PEFT】参数高效微调(PEFT)技术详解:从原理到 LoRA/QLoRA 实战
人工智能·python·深度学习·算法·机器学习·参数高效微调
我星期八休息24 分钟前
Linux I/O多路转接—epoll
java·linux·运维·服务器·开发语言·jvm·算法
水饺编程1 小时前
第5章,[Win32 章节] :Polygon 函数和多边形填充模式
c语言·c++·windows·visual studio
wuminyu1 小时前
Java FFM处理网络读写事件源码剖析
java·linux·c语言·jvm·c++
阳明山水1 小时前
Mamba与两阶段XGBoost的融合架构:面向间歇性需求的双路径预测框架
人工智能·深度学习·算法·机器学习·架构
水上冰石2 小时前
【MuJoCo从入门到精通】第2章 第一个 MuJoCo 仿真
前端·人工智能·算法
wenyq72 小时前
LeetCode 2904. Shortest and Lexicographically Smallest Beautiful String
算法·leetcode
维克兜率天2 小时前
【维克】技术指标是什么:从均线到MACD的数学原理
人工智能·笔记·python·算法·量化
Messy create2 小时前
【储能系统三大核心】
网络·stm32·单片机·算法·能源