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.mzph.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实例对象中添加一…