leetcode 1449. 數位成本和為目標值的最大數字(dp)

這是我參與更文挑戰的第12天
,活動詳情查看更文挑戰
image.png

題目

給你一個整數數組 cost 和一個整數 target 。請你返回滿足如下規則可以得到的 最大 整數:

給當前結果添加一個數位(i + 1)的成本為 cost[i] (cost 數組下標從 0 開始)。
總成本必須恰好等于 target 。
添加的數位中沒有數字 0 。
由于答案可能會很大,請你以字符串形式返回。

如果按照上述要求無法得到任何整數,請你返回 “0” 。

  • 示例 1:

輸入:cost = [4,3,2,5,6,7,2,5,5], target = 9
輸出:“7772”
解釋:添加數位 ‘7’ 的成本為 2 ,添加數位 ‘2’ 的成本為 3 。所以 “7772” 的代價為 23+ 31 = 9 。 “977” 也是滿足要求的數字,但 “7772” 是較大的數字。

 數字     成本1  ->   42  ->   33  ->   24  ->   55  ->   66  ->   77  ->   28  ->   59  ->   5
  • 示例 2:

輸入:cost = [7,6,5,5,5,6,8,7,8], target = 12
輸出:“85”
解釋:添加數位 ‘8’ 的成本是 7 ,添加數位 ‘5’ 的成本是 5 。“85” 的成本為 7 + 5 = 12 。

  • 示例 3:

輸入:cost = [2,4,6,2,4,6,4,4,4], target = 5
輸出:“0”
解釋:總成本是 target 的條件下,無法生成任何整數。

  • 示例 4:

輸入:cost = [6,10,15,40,40,40,40,40,40], target = 47
輸出:“32211”

解題思路

數組定義

  • dp[i][j]代表選擇前i個數位,成本為j時,組成最大整數的長度
  • log[i][j]記錄當前dp[i][j]的情況是由dp[i][log[i][j]]轉移而來的

狀態轉移

  • j-cost[i]<0
    當前總成本太小了,不足以取第i位,因此

dp[i+1][j]=dp[i][j];

  • j-cost[i]>=0
    成本足夠了,可以選擇是否取當前數位。判斷取當前數位或者不取的情況下,哪種情況下產生的數位更多,選擇產生數位最多的一種情況。
                  if (dp[i][j]>dp[i+1][j-cost[i]]+1){dp[i+1][j]=dp[i][j];log[i+1][j]=j;}else {dp[i+1][j]=dp[i+1][j-cost[i]]+1;log[i+1][j]=j-cost[i];}

代碼

class Solution {public String largestNumber(int[] cost, int target) {int[][] dp = new int[10][target + 1];int[][] log = new int[10][target + 1];for (int i=0;i<10;i++){Arrays.fill(dp[i],Integer.MIN_VALUE);}dp[0][0]=0;for (int i=0;i<9;i++){for (int j = 0; j <= target; j++) {if(j-cost[i]<0){dp[i+1][j]=dp[i][j];log[i+1][j]=j;}else{if (dp[i][j]>dp[i+1][j-cost[i]]+1){dp[i+1][j]=dp[i][j];log[i+1][j]=j;}else {dp[i+1][j]=dp[i+1][j-cost[i]]+1;log[i+1][j]=j-cost[i];}}}}StringBuilder res = new StringBuilder();int i=9,j=target;if(dp[i][j]<0)  return "0";while (i>0){if(log[i][j]==j){i--;}else{j=log[i][j];res.append(i);}}return res.toString();}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390654.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390654.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390654.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

風能matlab仿真_風能產量預測—深度學習項目

風能matlab仿真DL DATATHON- AI4ImpactDL DATATHON- AI4影響 Published by Team AI Traders — Suyash Lohia, Nguyen Khoi Phan, Nikunj Taneja, Naman Agarwal and Mihir GuptaAI交易員團隊發布 -Suyash Lohia&#xff0c;Nguyen Khoi Phan&#xff0c;Nikonj Taneja&#x…

android JNI調用(Android Studio 3.0.1)(轉)

最近回頭復習了一下android 的jni調用&#xff0c;卻發現按以前的方法調用失敗&#xff0c;一怒之下就重新摸索&#xff0c;碰了幾次壁&#xff0c;發現網上好多教程都不能成功調用&#xff0c;于是記錄一下現在AS版本成功好用的調用方法。 這里設定你的ndk已經下載并且設置沒問…

安卓源碼 代號,標簽和內部版本號

SetupSecurityPortingTuningCompatibilityReference轉到源代碼Getting Started OverviewCodelines, Branches, and ReleasesCodenames, Tags, and Build NumbersProject RolesBrand GuidelinesLicensesFAQSite UpdatesDownloading and Building RequirementsEstablishing a Bui…

git 列出標簽_Git標簽介紹:如何在Git中列出,創建,刪除和顯示標簽

git 列出標簽Tagging lets developers mark important checkpoints in the course of their projects development. For instance, software release versions can be tagged. (Ex: v1.3.2) It essentially allows you to give a commit a special name(tag).通過標記&#xff…

leetcode 278. 第一個錯誤的版本(二分)

題目 你是產品經理&#xff0c;目前正在帶領一個團隊開發新的產品。不幸的是&#xff0c;你的產品的最新版本沒有通過質量檢測。由于每個版本都是基于之前的版本開發的&#xff0c;所以錯誤的版本之后的所有版本都是錯的。 假設你有 n 個版本 [1, 2, …, n]&#xff0c;你想找…

騰訊哈勃_用Python的黑客統計資料重新審視哈勃定律

騰訊哈勃Simple OLS Regression, Pairs Bootstrap Resampling, and Hypothesis Testing to observe the effect of Hubble’s Law in Python.通過簡單的OLS回歸&#xff0c;配對Bootstrap重采樣和假設檢驗來觀察哈勃定律在Python中的效果。 In this post, we will revisit Hub…

JAVA中動態編譯的簡單使用

一、引用庫 pom文件中申明如下&#xff1a; <dependencies><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><…

程序員實用小程序_我從閱讀《實用程序員》中學到了什么

程序員實用小程序In short: old but gold.簡而言之&#xff1a;古老而又黃金。 Published in 1999, The Pragmatic Programmer is a book about how to become a Pragmatic Programmer. Which really means a ‘Good Programmer’. 《實用程序員》于1999年出版&#xff0c;是一…

leetcode 5786. 可移除字符的最大數目(二分法)

題目 給你兩個字符串 s 和 p &#xff0c;其中 p 是 s 的一個 子序列 。同時&#xff0c;給你一個元素 互不相同 且下標 從 0 開始 計數的整數數組 removable &#xff0c;該數組是 s 中下標的一個子集&#xff08;s 的下標也 從 0 開始 計數&#xff09;。 請你找出一個整數…

如何使用Picterra的地理空間平臺分析衛星圖像

From April-May 2020, Sentinel-Hub had organized the third edition of their custom script competition. The competition was organized in collaboration with the Copernicus EU Earth Observation programme, the European Space Agency and AI4EO consortium.從2020年…

df -l查看本地文件系統

df -l, --locallimit listing to local file systems 轉載于:https://www.cnblogs.com/jonathanyue/p/9301222.html

在Packet Tracer中路由器靜態路由配置

實驗目標&#xff1a;<1>掌握靜態路由的配置方法和技巧<2>掌握通過靜態路由方式實現網絡的連通性<3>熟悉廣域網線纜的鏈接方式技術原理&#xff1a;<1>路由器屬于網絡層設備&#xff0c;能夠根據IP包頭的信息&#xff0c;選擇一條最佳路徑&#xff0c;…

python示例_帶有示例的Python功能指南

python示例Python函數簡介 (Introduction to Functions in Python) A function allows you to define a reusable block of code that can be executed many times within your program.函數允許您定義一個可重用的代碼塊&#xff0c;該代碼塊可以在程序中多次執行。 Function…

leetcode 852. 山脈數組的峰頂索引(二分查找)

題目 符合下列屬性的數組 arr 稱為 山脈數組 &#xff1a; arr.length > 3 存在 i&#xff08;0 < i < arr.length - 1&#xff09;使得&#xff1a; arr[0] < arr[1] < … arr[i-1] < arr[i] arr[i] > arr[i1] > … > arr[arr.length - 1] 給你由…

hopper_如何利用衛星收集的遙感數據輕松對蚱hopper中的站點進行建模

hopper建筑學與數據科學 (Architectonics and Data Science) Understanding the site and topography are crucial first step of any architectural project. Site modelling can become very daunting, expensive, or just cumbersome, often having to use various software…

Git 倉庫代碼遷移步驟記錄

遷移遠程倉庫 // 克隆舊倉庫鏡像 git clone --mirror [oldRepoUrl]// 添加新倉庫地址 cd the_repo git remote add [remoteName] [newRepoUrl]// 推到新的遠程庫 git push -f --tags [remoteName] refs/heads/*:refs/heads/* 復制代碼中括號中的名稱需根據自己項目需求替換 更新…

TensorFlow MNIST 入門 代碼

其實就是按照TensorFlow中文教程的內容一步步跟著敲的代碼。 不過在運行代碼的時候遇到代碼中加載不到MNIST數據資源&#xff0c;似乎是被墻了&#xff08;(⊙﹏⊙)b&#xff09; 于是自己手動下載了數據包&#xff0c;放到 MNIST_data/ 文件夾下&#xff0c;代碼就能正常運轉了…

JavaScript中的基本表單驗證

In the past, form validation would occur on the server, after a person had already entered in all of their information and pressed the submit button. 過去&#xff0c;表單驗證會在一個人已經輸入了所有信息并按下“提交”按鈕之后在服務器上進行。 If the informa…

leetcode 877. 石子游戲(dp)

題目 亞歷克斯和李用幾堆石子在做游戲。偶數堆石子排成一行&#xff0c;每堆都有正整數顆石子 piles[i] 。 游戲以誰手中的石子最多來決出勝負。石子的總數是奇數&#xff0c;所以沒有平局。 亞歷克斯和李輪流進行&#xff0c;亞歷克斯先開始。 每回合&#xff0c;玩家從行的…

es6的Map()構造函數

普通的object對象是鍵值對的集合&#xff0c;但對于它的鍵卻有著嚴苛的要求&#xff0c;必須是字符串&#xff0c;這給我們平時帶來很多的不方便 Map函數類似于對象&#xff0c;但它是一個更加完美的簡直對集合&#xff0c;鍵可以是任意類型 set()方法可以向map實例對象中添加一…