首页 文章 精选 留言 我的

精选列表

搜索[匹配算法],共10000篇文章
优秀的个人博客,低调大师

leetcode算法题解(Java版)-4-动态规划(杨辉三角问题)

一、简单模拟 题目描述 Say you have an array for which the i th element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again). 思路 肯定是低价买,高价卖出。但是有个限制就是在买进的时候,必须卖出手上的股票。 最低价买,最高价的时候卖。在递增数列中,末项减首项=每项与后一项之差的和。这里不需要考虑交易次数最小,所以可以这么写: 代码 public class Solution { public int maxProfit(int[] prices) { int len=prices.length; int maxPro=0; for(int i=1;i<len;i++){ if(prices[i]>prices[i-1]){ maxPro+=prices[i]-prices[i-1]; } } return maxPro; } } 二、模拟的升级版 题目描述 Say you have an array for which the i th element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. 思路 这题跟上一题不一样的地方在于,是能买卖一次了。 可以先固定局部最小值,后面不断维护跟新局部最大值和局部最小值的差,直到最后找到结果。 做了两道这种最大值和最小值差的问题,总结一下,因为最大值最小值都是在数组中变化的,所以可以考虑先固定出局部的最小值。 代码 import java.lang.Math; public class Solution { public int maxProfit(int[] prices) { int len=prices.length; if(prices==null||len==0){ return 0; } int min=prices[0]; int maxPro=0; for(int i=1;i<len;i++){ if(prices[i]<min){ min=prices[i]; } else{ maxPro=Math.max(prices[i]-min,maxPro); } } return maxPro; } } 三、动态规划 题目描述Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is11(i.e., 2 + 3 + 5 + 1 = 11). Note: Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle. 思路 经典的三角数组,动态规划问题 详细的请看代码。这里没有开辟新的存储,只是在最后一行上跟新维护每一行的最优解,最后最后一行的第一个数即为所求。 语法点:import java.util.ArrayList;ArrayList.get(index);ArrayList.set(index,value);import java.lang.Math;Math.min(value1,value2); 代码 import java.lang.Math; import java.util.ArrayList; public class Solution { public int minimumTotal(ArrayList<ArrayList<Integer>> triangle) { int len=triangle.size(); if(len==0){ return 0; } if(len==1){ int min=triangle.get(0).get(0); for(int i=0;i<triangle.get(0).size();i++){ if(min>triangle.get(0).get(i)){ min=triangle.get(0).get(i); } } return min; } for(int i=len-2;i>=0;i--){ for(int j=0;j<i+1;j++){ int min=Math.min(triangle.get(len-1).get(j),triangle.get(len-1).get(j+1)); triangle.get(len-1).set(j,triangle.get(i).get(j)+min); } } return (int)triangle.get(len-1).get(0); } } 四、动态规划(杨辉三角) 题目描述 Given an index k, return the k th row of the Pascal's triangle. For example, given k = 3,Return[1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 思路 又是个动态规划问题,一开始想复杂了,或者说是没有按计算机思维来思考:我在算C{n_m},就是在算组合数,想通过组合数来打印出数组。 其实是个动态规划,当前行的值是依赖于上一行的。题目要求用O(n)空间来求解,那就设置了一个动态数组,每次从后往前刷新这个数组,即为所求。 代码 import java.util.ArrayList; public class Solution { public ArrayList<Integer> getRow(int rowIndex) { ArrayList<Integer> res=new ArrayList<>(); res.add(1); for(int i=0;i<rowIndex;i++){ for(int j=i;j>0;j--){//注意要从后往前刷新,因为用的是一个动态数组,防止仍有用的数据被新值覆盖 res.set(j,res.get(j)+res.get(j- 1)); } res.add(1); } return res; } }

优秀的个人博客,低调大师

Pluto-基于Caffe的GPU多机多卡深度学习算法产品

作者 阿里巴巴-计算平台-机器学习团队 同Caffe的关系 完全兼容Caffe。Pluto基于开源库Caffe扩展而来,是Caffe的超集,完全兼容Caffe的配置和数据,使用过Caffe的同学几分钟就能用Pluto跑起多机多卡版程序。 Pluto的单机核心代码同开源社区版本保持同步,所以开源社区提供的一些新特性我们能够迅速merge到Pluto里面 新特性 我们来源于Caffe,但同时根据我们的用户需求我们提供了一些Pluto独有的新特性,帮助用户在模型训练中提高效率。 多机多卡,通过并行计算提高计算和收敛速度。 数据读取提供多lmdb支持。开源版本只能从一个lmdb读取训练数据,Pluto提供支持从多个lmdb按概率(可配置)读取数据,可以更好的支持用户训练数据的增量更新,不会因为训练数据增加而重新制作lmdb文件。 hdf5文件prefetch支持。因为hdf5文件读取时需要全部load进内存的特点,当以hdf5文件作为数据存储格式时数据读取时间同计算时间无法进行overlap。Pluto提供文件级的prefetch支持,通过独立线程在计算时preload下一份hdf5文件,缩短训练时间。 Sequential learning支持。Sequence learning对于语音识别和自然语言处理中存在时序关联的数据进行处理建模拥有天然的优势。Pluto中提供的Sequence learning的相关支持,包括常用的RNN/LSTM以及双向的LSTM,可以高效准确的进行自然场景图片描述(如Image Caption)和自然语言处理中的相关建模任务。 多种同步模式支持。用户可以配置多卡间的同步轮数间隔,默认每轮同步一次;也可以配置按时间同步,即每张卡独立的迭代一段时间后同步一次,适用于资源竞争激烈或快慢机明显的环境。 Sparse 数据格式支持。支持sparse格式的训练数据。 性能分析 实验环境 GPU Device: Tesla K40m with 12GiB display memory, two cards in each box Network: InfiniBand, GPU direct cuDNNv2:open Alexnet在Imagenet数据集上的实验结果 梯度平均在每轮计算都做一次梯度平均,可以获得同单机一模一样的收敛结果,即acc57.1%,计算和收敛加速比如下: 模型平均每张卡各自计算若干轮后做一次模型平均,会影响收敛结果,但是可以提高计算加速比,结果如下: 收敛曲线 Googlenet在Imagenet数据集上的实验结果: 模型平均 收敛曲线 如何使用 在阿里云机器学习平台PAI上,可以选择Caffe组件使用。 使用文档见: https://help.aliyun.com/document_detail/49571.html?spm=5176.doc42745.6.548.Dd4AFR#Caffe

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册