簡體   English   中英

動態規划:當我將 if 語句分成兩行時,為什么代碼會失敗?

[英]Dynamic Programming: Why does the code fail when I break the if statement up into 2 lines?

我正在研究這個問題https://structy.net/problems/min-change ,其中給定硬幣向量和目標數量,我需要返回滿足目標數量的最小硬幣數量。

可變島大小表示應提供給客戶的最小找零金額,currentSize 表示在給定他們選擇檢查哪個硬幣路徑的情況下達到正確找零所需的當前硬幣數量,如遞歸所示。

我在下面的粗體標題下有 2 個代碼塊,我的問題是什么使不工作的代碼塊條件不起作用?

我首先檢查當前硬幣路徑路線是否有效,如果它是第一個導致正確找零的有效硬幣路徑,那么我們持有的當前 miniumchange 是第一個有效路徑。

但是,如果我們已經有了一個有效的路徑,那么它會轉到第二個條件,我只得到當前路徑和總路徑的最小值,因為當前路徑返回正確的變化量。

在第一個塊中,如果第一個塊可以分配為

if((currentSize != -1 && islandSize == -1) || (currentSize != -1 && (currentSize + 1 < islandSize))) islandSize = currentSize + 1

由於 currentSize 在這兩種情況下都必須為真,因此可以進一步細分為

/// first valid size
if((currentSize != -1 && islandSize == -1) islandSize = currentSize + 1; 
/// the new minimum
if(currentSize != -1 && currentSize + 1 < islandSize) islandSize = currentSize + 1; 

由於第二個條件不會修改 islandSize 如果它已經小於 currentSize,代碼:

/// first valid size
if((currentSize != -1 && islandSize == -1) islandSize = currentSize + 1; 
// updates islandSize min if it there exists a smaller current size
if(currentSize != -1) islandSize = min(islandSize, currentSize + 1); 

如果這樣的路徑也小於 currentSize,則應該只更新 islandSize。

有人可以幫助我解決我在這里出錯的地方嗎? 如果我能更好地提出這個問題,我會喜歡這種批評。

工作代碼

unordered_map<int, int> memo;
int minChange(int amount, std::vector<int> coins) {
  if(memo.count(amount)) return memo[amount];
  if(amount == 0) return 0;
  if(amount < 0) return -1;
  int islandSize = -1;
  for(auto it : coins){
    int currentSize = minChange(amount - it, coins);
    memo[amount-it] = currentSize;
    if(currentSize != -1 && (islandSize == -1 || currentSize + 1 < islandSize)) islandSize = currentSize + 1;
  }
  // todo
  return islandSize;
}

無效代碼

int minChange(int amount, std::vector<int> coins) {
  if(memo.count(amount)) return memo[amount];
  if(amount == 0) return 0;
  if(amount < 0) return -1;
  int islandSize = -1;
  for(auto it : coins){
    int currentSize = minChange(amount - it, coins);
    memo[amount-it] = currentSize;
    if(currentSize != -1 && islandSize == -1 ) islandSize = currentSize + 1;
    if(currentSize != -1) islandSize = min(islandSize, currentSize + 1);
  }
  // todo
  return islandSize;
}

條件語句

if(currentSize != -1 && (islandSize == -1 || currentSize + 1 < islandSize))
{
    islandSize = currentSize + 1;
}

可以改寫為

if(currentSize != -1)
{
    if (islandSize == -1 || currentSize + 1 < islandSize)
    {
        islandSize = currentSize + 1;
    }
}

兩種說法

if(currentSize != -1 && islandSize == -1)
{
    islandSize = currentSize + 1;
}
if(currentSize != -1)
{
    islandSize = min(islandSize, currentSize + 1);
}

可以改寫為

if(currentSize != -1)
{
    if (islandSize == -1)
    {
        islandSize = currentSize + 1;
    }
    else
    {
        islandSize = min(islandSize, currentSize + 1);
    }
}

條件的邏輯只是不一樣:如果currentSize != -1為真,則第一個變體將僅在條件的第二部分為真時分配給islandSize 而在第二個將始終分配給islandSize

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM