leetcode 502. IPO(上市,3种方法)

【LeetCode】502. IPO 这是一个典型的贪心算法题目,通过优先队列和排序可以高效解决。关键是理解到每次都应该在当前可行的项目中选择利润最大的那个。 阅读详情

在这里插入图片描述
假设leetcode 即将上市,如何筹集资金的问题。
有两个数组profits和capital, 分别代表第 i 个项目有多少净利润 和 需要多少启动资金。
手上的原始资金是w,
用这个w的资金去启动项目,完成项目之后净利润会加到w上,再做下一个项目,
一共可以做k 个 不同 的项目。
问最后w最多能到多少(能融多少资)。

思路:

来看看作为一个Hard的题目,它到底难在哪。

目前融资受两条线的限制,一个是受手上有多少钱的限制,手上只有w, 只做得起capital <= w的项目。
另一条就是要从做得起的项目中选择净利润最大的。

题目中说做k个不同的项目,说明每个项目只能做一次,做完就删掉。

那目前的做法就是先选出captical <= w的项目,再从这些项目中选择profit最大的,把profit加到w,
再开始下一轮的操作。

从已有的项目中选择profit最大的这点容易想到用优先队列。如果只是这样就算Medium难度了,
现在加了一个问题,怎么从项目中选出capital <= w的,然后再把做过的删掉?

要想把做过项目的删掉,就要把项目放进优先队列里,也就是选择profit最大的项目然后删掉,这点比较容易做到,
那么选择capital <= w的呢,有一个方法,就是把项目按照capital从小到大排序,
从左到右遍历,把capital <= w的都放进优先队列,遇到>w的停止,取出profit最大的加到w,并且把这个项目删掉。

那你会问,如果下一个项目w变小了怎么办,是不是队列里面就有不满足capital<=w的了?是不是又要重新装队列?
题目这里降低了这个难度,规定profit是净利润,就是收回本金之外有这些利润,所以w只会变多。
队列不需要清空,只需要继续向右遍历数组。

class Solution {
    public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
        int n = profits.length;
        int idx = 0;
        Project[] projects = new Project[n];
        PriorityQueue<Project> pq = new PriorityQueue<>((a,b)->(b.profit-a.profit));

        for(int i = 0; i < n; i++) {
            projects[i] = new Project(profits[i], capital[i]);
        }
        //按capital排序
        Arrays.sort(projects, (a,b)->Integer.compare(a.capital, b.capital));

        //注意profit是纯利润,w只会越加越多,w不会变少,所以之前加的project后面一定也能干
        while(k > 0) {
            while(idx < n && projects[idx].capital <= w){
                pq.offer(projects[idx]);
                idx ++;
            }
            if(pq.isEmpty()) break; //没有可做的project了
            w += pq.poll().profit;
            k--;
        }
        return w;

    }

    class Project{
        int profit = 0;
        int capital = 0;
        public Project(int profit, int capital) {
            this.profit = profit;
            this.capital = capital;
        }
    }
}

但是上面的方法不是最快的,它耗时在哪里呢,在排序。
如何不排序就能做到两条线一增一减?
前面排序是为了更方便地选出capital <= w的项目。在装入队列时保证装进去的都是capital <= w的项目。

那换一种思路,装入队列的时候不限制,什么项目都可以,但是取出的时候要取出capital <= w的,
那最好是按capital从小往大取,一旦出现capital > w的项目就停止。
所以每次都取出capital最小的项目,想到了最小堆。所以把项目按照capital的顺序放入最小堆(优先队列1)。

每次取出capital <= w的项目,然后和前面方法一样,把profit放进最大堆(优先队列2),取出profit最大的加到w, 并删除这个profit。
取出profit加到w时,我们并不需要capital这个东西,所以profit最大堆(优先队列2)只需要保存profit,不需要保存整个项目。

所以Hard难在哪呢?一般我们只需要考虑一个维度,但是这里有两个维度一增一减,
思路要么是降维(一个维度排好序,变化的只有另一维度),要么是安排一增一减两条线(两个优先队列)。
净利润这个词降低了难度,w只增不减,所以已经放入优先队列的不需要再取出重装。

class Solution {
    public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
        int n = profits.length;
        int idx = 0;
        //capital升序
        PriorityQueue<Project> qProject = new PriorityQueue<>((a,b)->Integer.compare(a.capital, b.capital));
        //profit降序
        PriorityQueue<Integer> qProfit = new PriorityQueue<>((a,b)->(b-a));

        //把capital<=w的装进qProfit,剩下的排进qProject
        for(int i = 0; i < n; i++) {
            if(capital[i] <= w) {
                qProfit.offer(profits[i]);
            } else {
                qProject.offer(new Project(profits[i], capital[i]));
            }
        }

        while(k > 0) {
            //把qProject里面capital<=w的profit提出来放进qProfit
            while(!qProject.isEmpty() && qProject.peek().capital <= w) {
                qProfit.offer(qProject.poll().profit);
            }
            if(qProfit.isEmpty()) break; //没有项目可做
            w += qProfit.poll();
            k --;
        }
        return w;
    }

    class Project{
        int profit = 0;
        int capital = 0;
        public Project(int profit, int capital) {
            this.profit = profit;
            this.capital = capital;
        }
    }
}

那如果你说两个优先队列太麻烦了,就只想用一个profit的优先队列,
选capital <= w的项目时就遍历整个数组来找,那么,请参考如下方法。
它有一个trick, 就是初始资金比任何项目的capital都大时,就可以无视capital, 直接选k个最大的profit.

    public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
        int maxCapital = 0;
        for(int i = 0 ; i < capital.length; i++){
            maxCapital = Math.max(capital[i], maxCapital);
        }
        
        //如果原始资金很大,无视capital,直接取k个最大的profit
        if(w >= maxCapital){
            PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a,b)->Integer.compare(a,b));
            for (int p: profits) {
                maxHeap.add(p);
                if (maxHeap.size() > k) maxHeap.poll();    
            }
            for (int h: maxHeap) w += h; 
            return w;
        }
        
        int index; //保存最大profit所在的index
        int n = profits.length;
        for(int i = 0; i < Math.min(k, n); i++) {
            index = -1; 
            //每次都遍历数组,找出capital<=w的项目,并记下最大profit的index
            for(int j = 0; j < n; ++j) { 
                if (w >= capital[j] && (index == -1 || profits[index] < profits[j])){
                        index = j;
                }
            }
            if(index == -1) break;
            w += profits[index];
            //把已经使用过的项目capital置为无穷大,下次就不会再使用
            capital[index] = Integer.MAX_VALUE;                
        }
        return  w; 
    }   
502. IPO 为了以更高的价格将股票卖给风险投资公司,力扣希望在 IPO 之前开展一些项目以增加其资本。由于资源有限,它只能在 IPO 之前完成最多。本题属于贪心算法和优先队列(最大堆)的经典应用场景。核心思路是:在每一步选择中,总是选择当前资本。能够启动的项目中利润最大的项目,从而使得总资本增长最快。这种策略能够保证在有限的项目数。个项目的前提下,按任意顺序选择项目。对于每个项目,只有当。个不同项目后得到最大总资本的方式。时才能启动,启动项目后会获得。返回最终可获得的最大资本。内获得最大的总资本。 阅读详情

相关推荐

【贪心 堆 优先队列】502. IPO

假设 力扣(LeetCode)即将开始 IPO 。为了以更高的价格将股票卖给风险投资公司,力扣 希望在 IPO 之前开展一些项目以增加其资本。 由于资源有限,它只能在 IPO 之前完成最多 k 个不同的项目。帮助 力扣 设计完成最多 k 个不同项目后得到最大总资本的方式。 给你 n 个项目。对于每个项目 i ,它都有一个纯利润 profits[i] ,和启动该项目需要的最小资本 capital[i] 。 最初,你的资本为 w 。当你完成一个项目时,你将获得纯利润,且利润将被添加到你的总资本中。 总而言之,从

闻缺陷则喜何志丹 1089

2021-09-08 LeetCode-502(每日一题)

算法思路借鉴于官方题解,添加了详细注释用于理解,刷题打卡 写题解的过程用到了优先队列,随即想到其实现方法就是堆排序,翻书重新学习了一遍堆排序的算法,感觉以后二叉树系列的题可能会碰到0.0 typedef pair<int, int> dat; class Solution { public: int findMaximizedCapital(int k, int w, vector<int> &profits, vector<int> &capit

qq_45773844的博客 188

leetcode答案解析

自己写的leetcode答案,javascript版,大概100道,算法渣渣,看看就好。

LeetCode每日一题(502. IPO)

Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Capital, LeetCode would like to work on some projects to increase its capital before the IPO. Since it has limited resources, it can only finish at most k dist

wangjun861205的博客 500

VirtualBox虚拟机拖放文件总失败?手把手教你配置双向拖放与共享文件夹(Ubuntu版)

本文详细指导如何在VirtualBox的Ubuntu虚拟机中配置高效的数据交换方案。核心是安装VirtualBox Guest Additions,并在此基础上开启双向拖放与共享粘贴板功能,同时建立稳定的共享文件夹,彻底解决主机与虚拟机间文件传输失败的问题,构建无缝的跨系统工作环境。

weixin_29245767的博客 238

软件工程案例教程·韩万江【课后习题】

!!!!

weixin_57858263的博客 6446

Leetcode【IPO】

假设 力扣(LeetCode)即将开始。为了以更高的价格将股票卖给风险投资公司,力扣 希望在 IPO 之前开展一些项目以增加其资本。由于资源有限,它只能在 IPO 之前完成最多k个不同的项目。帮助 力扣 设计完成最多k个不同项目后得到最大总资本的方式。给你n个项目。对于每个项目i,它都有一个纯利润profits[i],和启动该项目需要的最小资本capital[i]。最初,你的资本为w。当你完成一个项目时,你将获得纯利润,且利润将被添加到你的总资本中。总而言之,从给定项目中选择k个不同项目的列表,以。

摸鱼的快乐你不懂 847

9篇6章7节:SEER 数据库的2025年数据变量多条件组合选择(2026年版)

软件中的病例筛选功能通过CaseSelection界面实现精准研究队列构建。该界面允许用户通过变量选择(Variable)、运算符定义(Operator)和取值设定(Values)三步操作创建筛选条件,支持种族、分期等关键变量的多条件组合。

MD Code 1470

LeetCode题练习与总结:IPO--502

本文详细介绍了如何通过贪心算法和优先队列实现力扣IPO前的项目选择,以最大化资本。涵盖了代码实现、时间复杂度分析及关键知识点总结,为解决类似问题提供了清晰的思路。

一直学习永不止步 750

【AI for Medecine】吴恩达医学图像AI专项课程(诊断、预测、治疗)

近期需要简单了解AI4Science上的一些应用,看到吴恩达大佬的专项课程,于是开始来个“全面”的了解--医学人工智能(诊断、预测、治疗)。

图挖掘领域,新晋砖家 ☞ 未来可期,欢迎和静静一起学习交流吖 796

车间数据孤岛终于破了!C#上位机+MQTT打通产线与MES,数据延迟从小时级降到秒级

本文针对工业上位机开发中常见的车间数据与企业管理系统(MES)割裂问题,提出基于C#和MQTT协议的完整解决方案。通过EMQX MQTT Broker实现数据实时双向传输,将质量追溯时间从2小时缩短至10秒,生产计划自动下发,实现秒级数据延迟。方案采用分层解耦架构,支持设备更换和系统升级,核心优势包括低延迟、高可靠性和易扩展性。文章详细介绍了MQTT协议选型理由、整体架构设计、技术选型(EMQX Broker和MqttNet客户端),并提供了C#实现代码和部署流程。该方案已在实际产线验证,可有效解决&quo

这里只有干货:从 Modbus 到 MES,从 YOLO 到工控机部署;65 篇现场排障案例,每篇都写到改哪一行。 495

Leetcode—— IPO(最大化投资资金)

1. IPO (1)贪心 + 优先队列 每完成一个任务都会使得总资金 w 增加或不变。因此对于所选的第 i 个任务而言,应该在所有未被选择且启动资金不超过 w 的所有任务里面选利润最大的。 每次决策前,将启动资金不超过当前总资金的任务加入集合,再在里面取利润最大的任务 取最大的过程可以使用优先队列(根据利润排序的大根堆),而将启动资金不超过当前总资金的任务加入集合的操作,可以利用总资金在整个处理过程递增,而先对所有任务进行预处理排序来实现。 具体步骤 1.根据 profits 和 capital

Yawn 388

通过实例讲解瀑布模型

在电子商城开发中,瀑布模型如同 “工业时代的流水线”—— 能通过标准化流程交付高质量、高合规性的系统,但也可能因缺乏灵活性而错失市场机会。其优势与缺点本质上源于同一核心特征:严格的阶段性与文档驱动。决策建议若开发传统型、长周期、需求固定的电子商城(如企业内部采购平台),可优先选择瀑布模型,并通过 “需求预验证 + 模块化设计” 降低风险;若开发创新型、短周期、需快速迭代的电商平台(如社交电商、跨境电商),建议采用敏捷模型或混合模型(如瀑布 + Scrum),平衡规范性与响应速度。

跟佟格湾一起学习计算机知识吧~ 281

Java实现 LeetCode 502 IPO(LeetCode:我疯起来连自己都卖)

502. IPO 假设 力扣(LeetCode)即将开始其 IPO。为了以更高的价格将股票卖给风险投资公司,力扣 希望在 IPO 之前开展一些项目以增加其资本。 由于资源有限,它只能在 IPO 之前完成最多 k 个不同的项目。帮助 力扣 设计完成最多 k 个不同项目后得到最大总资本的方式。 给定若干个项目。对于每个项目 i,它都有一个纯利润 Pi,并且需要最小的资本 Ci 来启动相应的项目。最初,...

南 墙 1万+

机器学习势能(MLPs) 第一章 基础理论与核心概念 第二章 数据生成与训练集构建

密度泛函理论(DFT)作为现代计算材料科学的基石,通过求解Kohn-Sham方程在电子结构层面提供可靠的势能面信息。然而,其计算复杂度随体系电子数呈O(N3) 至O(N4) 增长,对于包含数百个原子的体系,单点计算往往需消耗数小时至数天的计算资源。这一瓶颈严重限制了分子动力学模拟的时间尺度,传统DFT-MD通常仅能覆盖皮秒量级,远不足以观测扩散、相变等慢动力学过程。经验力场通过解析函数形式描述原子间相互作用,实现了经典力学的计算效率,但其参数化过程高度依赖物理直觉与经验调参。

VectorShift的博客 199

如何通过conda安装HyPhy教程

本文介绍了在Ubuntu系统上通过conda安装分子进化分析工具HyPhy的详细步骤。首先创建conda环境并指定Python 3.8版本,然后通过bioconda通道安装HyPhy软件包。安装完成后可通过conda list命令验证安装。文中还特别指出可能遇到的libcrypto.so.1.1共享库缺失错误,并提供了解决方案的参考链接。该教程配有安装过程截图,帮助用户直观了解操作流程和验证安装结果。

猿小白的博客 8274

LeetCode 502:IPO 问题

摘要:LeetCode 502题为IPO问题,要求在给定初始资本和最多可做项目数的限制下,通过选择项目使最终资本最大化。解决方案采用贪心算法结合优先队列:1)将项目按启动资本排序;2)使用最大堆维护当前可负担的项目利润;3)每次选择利润最大的项目,更新资本。算法时间复杂度为O(n log n + k log n),空间复杂度O(n),高效解决了资源调度问题,适用于大规模数据。通过示例验证了算法的正确性。

m0_75126572的博客 908

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

LeetCode 502.IPO

502.IPO Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Capital, LeetCode would like to work on some projects to increase its capital before the IPO. S...

WXM 431

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

Leetcode 502、IPO

Problem Source : https://leetcode-cn.com/problems/ipo/ Solution Source : https://github.com/hujingbo98/algorithm/blob/master/source/leetcode/0502_IPO.cpp 502、IPO 假设 力扣(LeetCode)即将开始 IPO。为了以更高的价格将股票卖给风险投资公司,力扣希望在 IPO 之前开展一些项目以增加其资本。由于资源有限,它只能在 IPO 之前完成最多 k.

博弈的博客 126

深度学习工具包 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&#39;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(&#39;DeepLearnToolbox&#39;));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(&#39;state&#39;,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&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,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 = &#39;sigm&#39;;%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, &#39;Too big error&#39;);```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(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;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)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;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, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% 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&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 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, &#39;Too big error&#39;);```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(&#39;state&#39;,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, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,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, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,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, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % 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, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % 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, &#39;Too big error&#39;);%% 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(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % 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, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

leetcode 502. IPO

假设 力扣(LeetCode)即将开始其 IPO。为了以更高的价格将股票卖给风险投资公司,力扣 希望在 IPO 之前开展一些项目以增加其资本。 由于资源有限,它只能在 IPO 之前完成最多 k 个不同的项目。帮助 力扣 设计完成最多 k 个不同项目后得到最大总资本的方式。 给定若干个项目。对于每个项目 i,它都有一个纯利润 Pi,并且需要最小的资本 Ci 来启动相应的项目。最初,你有 W 资本。...

d00172326的专栏 257

Leetcode 502. IPO C++

Leetcode 502. IPO 题目 假设 力扣(LeetCode)即将开始其 IPO。为了以更高的价格将股票卖给风险投资公司,力扣 希望在 IPO 之前开展一些项目以增加其资本。 由于资源有限,它只能在 IPO 之前完成最多 k 个不同的项目。帮助 力扣 设计完成最多 k 个不同项目后得到最大总资本的方式。 给定若干个项目。对于每个项目 i,它都有一个纯利润 Pi,并且需要最小的资本 Ci 来启动相应的项目。最初,你有 W 资本。当你完成一个项目时,你将获得纯利润,且利润将被添加到你的总资本中。 总而

weixin_43750513的博客 383

LeetCode-502-IPO

502. IPO 题目 假设 力扣(LeetCode)即将开始 IPO 。为了以更高的价格将股票卖给风险投资公司,力扣 希望在 IPO 之前开展一些项目以增加其资本。 由于资源有限,它只能在 IPO 之前完成最多 k 个不同的项目。帮助 力扣 设计完成最多 k 个不同项目后得到最大总资本的方式。 给你 n 个项目。对于每个项目 i ,它都有一个纯利润 profits[i] ,和启动该项目需要的最小资本 capital[i] 。 最初,你的资本为 w 。当你完成一个项目时,你将获得纯利润,且利润将被添加到你

qq_44633470的博客 198

leetcode.502.IPO

IPO 题目描述: 假设 力扣(LeetCode)即将开始 IPO 。为了以更高的价格将股票卖给风险投资公司,力扣 希望在 IPO 之前开展一些项目以增加其资本。 由于资源有限,它只能在 IPO 之前完成最多 k 个不同的项目。帮助 力扣 设计完成最多 k 个不同项目后得到最大总资本的方式。 给你 n 个项目。对于每个项目 i ,它都有一个纯利润 profits[i] ,和启动该项目需要的最小资本 capital[i] 。 最初,你的资本为 w 。当你完成一个项目时,你将获得纯利润,且利润将被添加到你的总资

weixin_51885188的博客 1024
上一篇: coco数据集训练nanodet详细流程
下一篇: leetcode 1675. Minimize Deviation in Array(最小化数组偏差)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值
⌂ 换网址