动态规划法求解硬币找零(Java)

硬币找零问题详解及动态规划解法 动态规划提供了一种高效解决硬币找零问题的方式。通过保存已经计算出的结果,避免了大量重复计算,从而大大提高了算法的效率。这个思路不仅适用于硬币找零问题,还可以扩展到很多类似的最优化问题中。 阅读详情
算法描述:
动态规划算法通常用于求解具有某种最优性质的问题。在这类问题中,可能会有许多可行解。每一个解都对应于一个值,我们希望找到具有最优值的解。基本思想也是将待求解问题分解成若干个子问题,先求解子问题,并将子问题的结果保存下来,然后从这些子问题的解得到原问题的解。动态规划实质上是一种以空间换时间的技术,它在实现的过程中,不得不存储产生过程中的各种状态,所以它的空间复杂度要大于其它的算法。
问题描述:
现存在一堆面值为 V1、V2、V3 … 个单位的硬币,问最少需要多少个硬币才能找出总值为 T 个单位的零钱?
解题思路:
1,找出面值最接近T的硬币V
2,将f(T)问题的求解转换成f(T-V)+1问题的求解,以此出现递归
代码实现:
CoinChange类用于处理硬币找零的业务:
public class CoinChange {
	/**
	 * 获取找零硬币个数
	 * @param coinValue 硬币的面值  
	 * @param totalValue 需要找零的钱数
	 * @return
	 */
	public int coinNum(int[] coinValue,int totalValue){
		List<Integer> coins=new ArrayList<Integer>();
		coins.add(0);
		for(int i=1;i<=totalValue;i++){
			int coin=nearestCoin(i,coinValue);
			int coinNum=coins.get(i-coin)+1;
			coins.add(coinNum);
		}
		return coins.get(totalValue);
	}
	/**
	 * 获取最接近找零钱数的硬币面值
	 */
	private int nearestCoin(int value,int[] coinValues){
		int res=0;
		int nearest=Integer.MAX_VALUE;
		for(int coinValue:coinV
别再为跨页读写发愁了!STM32F407模拟I2C驱动24C256/512的避坑指南 本文详细解析了STM32F407模拟I2C驱动24C256/512的跨页写入问题,揭示了24C系列存储器'写满一页不会自动翻页'的关键机制,并提供了经过验证的分页写入算法和优化方案。通过GPIO时序优化、地址计算和错误处理等技巧,帮助开发者解决数据错乱、覆盖等常见问题,实现稳定可靠的EEPROM读写操作。 阅读详情

相关推荐

干货——Joplin私人笔记安装部署

安全、隐私、免费、不限制的私人笔记的选择和搭建

vincentho1024的专栏 7896

氮化镓GaN电机驱动实战:从器件原理到EPC驱动中心

电机驱动是工业自动化和电动交通的核心,功率器件的选择直接决定系统效率与可靠性。传统硅基IGBT和MOSFET在开关速度、损耗方面存在瓶颈,而氮化镓(GaN)作为宽禁带半导体,凭借高电子迁移率、低栅极电荷和近乎为零的反向恢复电荷,正成为电机驱动领域的热门选择。EPC建立电机驱动中心,提供完整的评估平台,覆盖从低压家电到中压伺服的应用场景。文章从GaN器件原理出发,对比传统硅器件,详解三相BLDC/PMSM驱动系统的搭建、栅极驱动与Layout设计、死区时间设置及参数计算实例,并分享测试中遇到的振铃、EMI等高

weixin_29168675的博客 336

SAP Gateway Foundation OData V4 服务管理深度解析,从 Service Repository 到 Service Group 的完整运行逻辑

本文解析了SAP S/4HANA中OData V4 API的技术架构,重点阐述Service Repository、Service与Service Group的三层管理体系。以SalesOrder和PurchaseOrder API为例,说明不同服务可能属于特定repository(如srvd_a2x)而非默认DEFAULT库。Service Group作为业务模型组合单元,支持跨服务引用和统一配置,其发布状态直接影响服务可用性。文中对比了后台管理事务/IWBEP/V4_ADMIN与网关端/IWFND/V4

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 104

第3.4章 机器人第三方库实战总结(四):G2O优化库从入门到实战(附SLAM实例代码)

自定义边同样是满足复杂应用需求的重要手段。自定义边时,需要明确边所连接的顶点类型以及边所表示的误差项的计算方式。假设我们要定义一条边,用于约束两个上述自定义顶点之间的相对位姿和传感器偏移量关系。我们可以这样定义边类:public:// 计算误差// 计算雅可比矩阵// 读盘和存盘函数,这里暂不实现。

行知SLAM 373

Android性能优化之Android Profiler

在移动应用开发中,打造一个流畅、省电且不卡顿的用户体验是至关重要的。然而,随着应用功能的日益复杂,内存泄漏、CPU暴涨、网络卡顿以及电量消耗过快等问题常常化身为隐形杀手,悄悄吞噬着用户体验。为了帮助开发者精准定位并解决这些性能瓶颈,谷歌在Android Studio中集成了一款强大的一站式性能分析工具Android Profiler。Android Profiler作为Android Studio内置的实时数据可视化工具,支持运行在Android 5.0及更高版本的设备上。

xiangzhihong8的专栏 208

SAP 实施:Waterfall vs. Agile —— 一场“结构性矛盾“的深刻比较先说结论:在 SAP 世界里,这从来不是“谁更好“的宗教问题,而是“你的项目 DNA 是什么“的战略选择。​

SAP 实施:Waterfall vs. Agile —— 一场"结构性矛盾"的深刻比较 先说结论:在 SAP 世界里,这从来不是"谁更好"的宗教问题,而是"你的项目 DNA 是什么"的战略选择。​ SAP 早已用 SAP Activate​ 给出了官方态度——它不是纯 Agile,也不是回头去搞旧 Waterfall,而是一个带质量门的混合架构。 1. 先回到本质:两者解决的是不同维度的风险 维度 Waterfall(传统 ASAP 路线)​ Agile(SCRUM/Kanban 式迭代)​

金牌架构师 344

详解FreeRTOS:低功耗Tickless模式(高级篇—7)

FreeRTOS提供了一个叫做Tickless的低功耗模式,本篇博文我们就来学习一下如何使用这个Tickless模式。

不脱发的程序猿 1905

CANbus收发器ADM3053BRWZ-REEL7应用原理图设计

ADM3053 在 CAN 协议控制器与物理层总线之间创建一个 完全隔离的接口。该器件具有限流和热关断特性,可防止输出短路。额定温 度范围为工业温度范围,提供 20 引脚、宽体 SOIC 封装。该器件采用 5 V 单电源供电, 提供完全隔离的 CAN 解决方案。ADM3053 是一款隔离式控制器区域网络 (CAN) 物理层收 发器,集成隔离 DC/DC 转换器,符合 ISO 11898 标准。该器件采用 ADI 公司的。Coupler® 技术,将双通道隔离器、 CAN 收发器和 ADI 公司的。

zhusl6688的博客 1274

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: 贪心法求解活动安排(java实现)
下一篇: Dijkstra算法求最短路径(java)
JavaMan_chen
博客等级 码龄19年 329粉丝 82原创
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值