第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

最低調(diào)整成本

最低調(diào)整成本

揚(yáng)帆大魚 2023-12-21 10:48:31
我正在嘗試解決這個(gè)問題:給定一個(gè)整數(shù)數(shù)組,調(diào)整每個(gè)整數(shù),使每個(gè)相鄰整數(shù)的差不大于給定的數(shù)字目標(biāo)。如果調(diào)整前的數(shù)組為A,調(diào)整后的數(shù)組為B,則應(yīng)盡量減少`|之和。A[i]-B[i]|。您可以假設(shè)數(shù)組中的每個(gè)數(shù)字都是正整數(shù)且不大于 100。`我看到了 dp 解,但不太明白遞推方程。public static int MinAdjustmentCost(ArrayList<Integer> A, int target) {    // write your code here    if (A == null || A.size() == 0) {        return 0;    }    // D[i][v]: 把index = i的值修改為v,所需要的最小花費(fèi)    int[][] D = new int[A.size()][101];    int size = A.size();    for (int i = 0; i < size; i++) {        for (int j = 1; j <= 100; j++) {            D[i][j] = Integer.MAX_VALUE;            if (i == 0) {                // The first element.                D[i][j] = Math.abs(j - A.get(i));            } else {                for (int k = 1; k <= 100; k++) {                    // 不符合條件                     if (Math.abs(j - k) > target) {                        continue;                    }                    int dif = Math.abs(j - A.get(i)) + D[i - 1][k];                    D[i][j] = Math.min(D[i][j], dif);                }            }        }    }    int ret = Integer.MAX_VALUE;    for (int i = 1; i <= 100; i++) {        ret = Math.min(ret, D[size - 1][i]);    }    return ret;}有人可以向我解釋一下嗎?
查看完整描述

1 回答

?
撒科打諢

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊

您需要最小化調(diào)整成本,即增加/減少每個(gè)元素的值,使得每個(gè)相鄰元素之間的差異小于或等于target。dp 解決方案是嘗試所有可能的值并最小化有效值的成本(當(dāng)abs(A[i]-A[i-1]) <= target)


第一件事是將第一個(gè)元素調(diào)整為 1-100 的成本,如下所示:


 for (int i = 0; i < size; i++) {

        for (int j = 1; j <= 100; j++) {

            D[i][j] = Integer.MAX_VALUE; // fill with MAX_VALUE because we want to minimize

            if (i == 0) {

                // for the first element we just set the cost of adjusting A[i] to j

                D[i][j] = Math.abs(j - A.get(i));

            }

現(xiàn)在您需要D[0][j]將第一個(gè)元素調(diào)整為 的成本j。然后,對(duì)于每個(gè)其他元素,您再次循環(huán)(從k = 1到k = 100)其他元素并嘗試更改A[i]為j。然后檢查是否abs(k-j)有效(小于或等于target),然后您可以調(diào)整A[i]為j和A[i-1]以便k最小化D[i][j]。


這里的意思是改變到D[i][j]的成本,是改變到的成本。因此,對(duì)于每個(gè)并且如果它們有效(),那么您將它們添加在一起并最小化保存的值,以便您可以將其用于下一個(gè)元素,這是在此處完成的:A[i]jD[i-1][k]A[i-1]kkjabs(k-j)<=targetD[i][j]


else {

    for (int k = 1; k <= 100; k++) {

        // if abs(j-k) > target then changing A[i] to j isn't valid (when A[i-1] is k)

        if (Math.abs(j - k) > target) {

            continue;

        }

        // otherwise, calculate the the cost of changing A[i] to j and add to it the cost of changing A[i-1] to k

        int dif = Math.abs(j - A.get(i)) + D[i - 1][k];

        // minimize D[i][j]

        D[i][j] = Math.min(D[i][j], dif);

     }

}

最后,您需要在最后一個(gè)元素處從 1 循環(huán)到 100,并檢查哪個(gè)是所有元素中的最小值,這在此處完成:


int ret = Integer.MAX_VALUE;

for (int i = 1; i <= 100; i++) {

    ret = Math.min(ret, D[size - 1][i]);

}

我覺得如果把初始化代碼和DP計(jì)算代碼分開會(huì)更容易理解,例如:


// fill the initial values

for (int i = 0; i < size; ++i) {

    for (int j = 1; j <= 100; ++j) {

        // on the first element just save the cost of changing

        // A[i] to j

        if (i == 0) {

            DP[i][j] = abs(j-A.get(i));

        } else {

            // otherwise intialize with MAX_VALUE

            D[i][j] = Integer.MAX_VALUE;        

        }


    }

}

for (int i = 1; i < size; i++) {

    for (int j = 1; j <= 100; j++) {

        for (int k = 1; k <= 100; k++) {

            // if abs(j-k) isn't valid skip it

            if (Math.abs(j - k) > target) {

                continue;

            }

            // if it is valid, calculate the cost of changing A[i] to j

            // and add it to the cost of changing A[i-1] to k then minimize

            // over all values of j and k

            int dif = Math.abs(j - A.get(i)) + D[i - 1][k];

            D[i][j] = Math.min(D[i][j], dif);

        }

    }

}


// calculate the minimum cost at the end

int ret = Integer.MAX_VALUE;

for (int i = 1; i <= 100; i++) {

    ret = Math.min(ret, D[size - 1][i]);

}


查看完整回答
反對(duì) 回復(fù) 2023-12-21
  • 1 回答
  • 0 關(guān)注
  • 170 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)