首页 > 其他分享 >leetcode第109场双周赛

leetcode第109场双周赛

时间:2023-07-23 23:25:05浏览次数:28  
标签:even 分数 int max 双周 109 long odd leetcode

题目传送门

6931. 访问数组中的位置使分数最大

题意
给你一个数组,初始你位于下标 1 处,你可以往后跳到数组任一下标,但不能往前跳。跳到哪个位置,即可获得下标对应的分数,但如果当前下标的数与跳之前下标的数奇偶性不同,那么你会失去分数 x。询问你能获得的最大分数?

思路
一眼动态规划,那么问题在于怎么规划?
我们贪心的考虑,要让分数最大肯定不能简单的选择奇数或者选择偶数,而是选择最大的跳,奇偶不同时仅存在分数 x 的亏损。那么对于每一个位置,我们都希望它是由前面某个位置跳过来,且能让当前位置取得最大值。又因为奇偶性的问题,所以我们可以记两个数 even 和 odd 来保存当前已经处理段结尾为偶数或者结尾为奇数时的最大值。当前数为偶数时更新 even, 当前数为奇数时更新 odd 。最终答案为 \(max(even, odd)\)

代码

#define ll long long
class Solution {
public:
	long long maxScore(vector<int>& a, int x) {
		int n = a.size();
		ll even, odd;
		even = a[0] + ((a[0] % 2 == 0) ? 0 : -x);
		odd = a[0] + ((a[0] % 2 == 0) ? -x : 0);
		for(int i = 1; i < n; ++ i){
			if(a[i] % 2){
				odd = a[i] + max(even - x, odd);
			}else{
				even = a[i] + max(even, odd - x);
			}
		}
		return max(odd, even);
	}
};

标签:even,分数,int,max,双周,109,long,odd,leetcode
From: https://www.cnblogs.com/Qiansui/p/17576165.html

相关文章

  • LeetCode 周赛上分之旅 #35 两题坐牢,菜鸡现出原形
    ⭐️本文已收录到AndroidFamily,技术和职场问题,请关注公众号[彭旭锐]和[BaguTreePro]知识星球提问。学习数据结构与算法的关键在于掌握问题背后的算法思维框架,你的思考越抽象,它能覆盖的问题域就越广,理解难度也更复杂。在这个专栏里,小彭与你分享每场LeetCode周赛的解题报告......
  • leetcode第 109 场双周赛
    6930.检查数组是否是好的-力扣(LeetCode)首先判断数组长度是不是最大值+1,然后排个序,判断0到n-2是不是都是1到最大值的一个排列,满足这些返回true就行了classSolution{public:boolisGood(vector<int>&num){intma=0;for(autoi:num){......
  • java实现leetcode 22 括号的生成
    Java实现Leetcode22-括号的生成本文将介绍如何使用Java实现Leetcode22题-括号的生成,并提供相应的代码示例。括号的生成是一个经典的递归问题,通过理解递归的思想和括号生成的规律,我们可以很容易地解决这个问题。题目描述给定一个整数n,表示生成括号的对数,编写一个函数来生成......
  • 【优先队列】【堆排序实现优先队列】[1054. 距离相等的条形码](https://leetcode.cn/p
    【优先队列】【堆排序实现优先队列】1054.距离相等的条形码在一个仓库里,有一排条形码,其中第i个条形码为barcodes[i]。请你重新排列这些条形码,使其中任意两个相邻的条形码不能相等。你可以返回任何满足该要求的答案,此题保证存在答案。示例1:输入:barcodes=[1,1,1,2,2,2]......
  • [LeetCode] 894. All Possible Full Binary Trees
    Givenaninteger n,return alistofallpossible fullbinarytrees with n nodes.Eachnodeofeachtreeintheanswermusthave Node.val==0.Eachelementoftheansweristherootnodeofonepossibletree.Youmayreturnthefinallistoftreesin......
  • leetcode-2582-easy
    PassthePillowTherearenpeoplestandinginalinelabeledfrom1ton.Thefirstpersoninthelineisholdingapillowinitially.Everysecond,thepersonholdingthepillowpassesittothenextpersonstandingintheline.Oncethepillowreachest......
  • Leetcode394. 字符串解码
    classSolution{public:stringdfs(strings,int&idx){stringstr;while(idx<s.size()){if(s[idx]==']'){idx++;returnstr;}......
  • leetcode 栈与队列 232 225
    目录基本介绍四个问题232225基本介绍栈,先进后出队列,先进先出四个问题C++中stack是容器么?我们使用的stack是属于哪个版本的STL?我们使用的STL中stack是如何实现的?stack提供迭代器来遍历stack空间么?首先大家要知道栈和队列是STL(C++标准库)里面的两个数据结构。C++标准......
  • [LeetCode] 1349. Maximum Students Taking Exam 参加考试的最大学生数
    Givena m *n matrix seats  thatrepresentseatsdistributions inaclassroom. Ifaseat is broken,itisdenotedby '#' characterotherwiseitisdenotedbya '.' character.Studentscanseetheanswersofthosesittingnexttothele......
  • codility 和 leetcode 对比
    根据网上的信息,codility和leetcode都是用于评估编程技能的在线平台,它们都提供了不同难度和类型的编程挑战,支持多种编程语言,并可以用于招聘和面试的过程中。不过,它们也有一些区别,比如:codility更专注于工程团队的技能评估,它提供了CodeCheck,CodeLive,和CodeEvent三个功......