首页 > 编程语言 >Day 24 贪心算法part02| LeetCode 122.买卖股票的最佳时机II,55.跳跃游戏,45.跳跃游戏II,1005.K次取反后最大化的数组和

Day 24 贪心算法part02| LeetCode 122.买卖股票的最佳时机II,55.跳跃游戏,45.跳跃游戏II,1005.K次取反后最大化的数组和

时间:2024-10-07 15:32:58浏览次数:1  
标签:游戏 nums int max II length res 跳跃

122.买卖股票的最佳时机II

  • 局部最优:每天的正利润
  • 全局最优:每天的正利润之和

121. 买卖股票的最佳时机

  class Solution {
        public int maxProfit(int[] prices) {
            int res=0;
            for(int i=1;i< prices.length;i++)
            {
              if(prices[i]-prices[i-1]>0)
              {
                  res+=prices[i]-prices[i-1];
              }

            }
           return res;
        }
    }

55.跳跃游戏

55. 跳跃游戏

  • 看覆盖范围
 class Solution {
        public boolean canJump(int[] nums) {

            int cover=0;//覆盖下标
            int lens=nums.length;
            if(lens==1)return true;
            for(int i=0;i<=cover;i++)
            {
                //确保cover每次都是最大覆盖范围
                cover=Math.max(cover,i+nums[i]);
                if(cover>=lens-1)return true;

            }
            return false;
        }
    }

45.跳跃游戏II

45. 跳跃游戏 II

class Solution {
        public int jump(int[] nums) {
            if(nums.length==1)return 0;
            
                int cur_cover=0;
                int max_cover=0;//最大的覆盖范围
                int res=0;
                for(int i=0;i<nums.length;i++)
                {
                    max_cover=Math.max(i+nums[i],max_cover);
                    if(max_cover>=nums.length-1)
                    {
                        res++;
                        break;
                    }
                    if(i==cur_cover)
                    {
                        //当前终点不是数组终点
                       cur_cover=max_cover;
                      res++;
                        
                    }
                   
                }
                return res;
        }
    }

1005.K次取反后最大化的数组和

1005. K 次取反后最大化的数组和

 class Solution {
      
      void selectSort(int a[], int n)
        {
            for (int i = 0; i < n - 1; i++)
            {
                int max = i;
                for (int j = i + 1; j < n; j++)
                {
                    if (Math.abs(a[j]) > Math.abs(a[max]))//按绝对值大小比较
                    {
                        max = j;
                    }
                }
                if (max != i)
                {
                    int temp=a[i];
                    a[i]=a[max];
                    a[max]=temp;
                    //swap(a[i], a[max]);
                }
            }
        }
        public int largestSumAfterKNegations(int[] nums, int k) {

          //1.按绝对值降序排序
            selectSort(nums,nums.length);
            //2.从前往后将负数变为正数
            for(int i=0;i<nums.length;i++)
            {
                 if(nums[i]<0&&k>0)
                {
                    nums[i]= Math.abs(nums[i]);
                    k--;
                }
            }
            //3.处理剩余的k>0,反复转变绝对值最小的数,知道k==0
           if(k>0&&k%2!=0)//奇数
           {
              nums[nums.length-1]=-1*nums[nums.length-1];
           }
        
           int res=0;
           for(int i=0;i<nums.length;i++)
           {
               res+=nums[i];
           }
           return res ;
        }
    }

标签:游戏,nums,int,max,II,length,res,跳跃
From: https://www.cnblogs.com/FreeDrama/p/18450162

相关文章

  • 92. Reverse Linked List II
    92.ReverseLinkedListII在链表头添加一个虚拟源点,可以减少特判/***Definitionforsingly-linkedlist.*structListNode{*intval;*ListNode*next;*ListNode():val(0),next(nullptr){}*ListNode(intx):val(x),next(nullptr){......
  • windows服务器IIS服务修改已绑定的网站域名
    要在Windows服务器上的IIS(InternetInformationServices)中修改已绑定的网站域名,可以按照以下步骤操作:打开IIS管理器:打开“开始”菜单,输入“InternetInformationServices(IIS)管理器”,并打开它。或者通过“服务器管理器”中的“工具”菜单打开IIS管理器。......
  • 代码随想录算法训练营day7|● 454.四数相加II ● 383. 赎金信 ● 15. 三数之和 ●
    学习资料:https://programmercarl.com/0015.三数之和.html#其他语言版本学习记录:454.四数相加(hash_dict,前两个数一组遍历a+b,后两个数一组遍历找0-(a+b))点击查看代码classSolution:deffourSumCount(self,nums1:List[int],nums2:List[int],nums3:List[int],nums4:......
  • 游戏试玩全自动挂机撸金项目
    项目概述游戏试玩平台全自动挂机项目利用自动化脚本技术,项目特点自动化操作:通过脚本自动执行试玩任务,减少人工干预。设备需求电脑云手机:项目需要使用电脑云手机,以支持多窗口单IP的运行环境。单窗口单IP:每个窗口都需要独立的IP地址,以避免被平台检测到异常行为。......
  • 随机组句小游戏-V1.13版本
    玩法:随机抽取地点人物事件,来组句2024/10/03进行微调.\(代码/Code:\)#include<bits/stdc++.h>#include<windows.h>#defineSM_printf("%c%c%c%c%c%c",-64,-18,-278,-59,-319,-40);usingnamespacestd;ints1,s2,s3,xz1,bool01;stringcopy_;stringplace[105]......
  • 帝国CMS在IIS环境开启TAG伪静态后,中文TAG提示“TAG不存在”的最后解决方法!
    如果你尝试了多种解决方案仍然无法解决帝国CMSTAG伪静态中文提示“TAG不存在”的问题,可以尝试以下方案:解决方案打开 index.php 文件。修改变量获取方式。具体操作步骤打开 index.php 文件:打开 /e/tags/index.php 文件。修改变量获取方式:查找以下代码:......
  • 数组和函数实践:扫雷游戏
    ⽬录1.扫雷游戏分析和设计2.扫雷游戏的代码实现3.扫雷游戏的扩展1.扫雷游戏分析和设计1.1扫雷游戏的功能说明•使⽤控制台实现经典的扫雷游戏•游戏可以通过菜单实现继续玩或者退出游戏•扫雷的棋盘是9*9的格⼦•默认随机布置10个雷•可以排查雷   ......
  • C语言初阶,猜数字游戏
    这是适合萌新练习的小程序,在了解了循环语句后可以实现game函数被调用后执行猜数字环节,直到猜对,也可以改变while的条件设置猜的次数  代码从这里开始,如果选择1则调用game函数  代码就分享到这里,谢谢大家!......
  • [leetcode 92] 反转链表 II
    题目描述:https://leetcode.cn/problems/reverse-linked-list-ii给你单链表的头指针 head 和两个整数 left 和 right ,其中 left<=right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。 示例1:输入:head=[1,2,3,4,5],left=2,right......
  • 小游戏-扫雷简易版
    雷的个数和棋盘大小可以随意改变一、test.c(主程序)#include"game.h"voidmenu(){ printf("****************************\n"); printf("*****开始游戏-1*****\n"); printf("*****结束游戏-0*****\n"); printf("**************......