当前位置: 首页 > news >正文

怎么做qq钓鱼网站吗广州广告推广公司

怎么做qq钓鱼网站吗,广州广告推广公司,昆明网红打卡景点,免费个人二级域名网站【LetMeFly】2786.访问数组中的位置使分数最大:奇偶分开记录(逻辑还算清晰的题解) 力扣题目链接:https://leetcode.cn/problems/visit-array-positions-to-maximize-score/ 给你一个下标从 0 开始的整数数组 nums 和一个正整数 …

【LetMeFly】2786.访问数组中的位置使分数最大:奇偶分开记录(逻辑还算清晰的题解)

力扣题目链接:https://leetcode.cn/problems/visit-array-positions-to-maximize-score/

给你一个下标从 0 开始的整数数组 nums 和一个正整数 x 。

一开始 在数组的位置 0 处,你可以按照下述规则访问数组中的其他位置:

  • 如果你当前在位置 i ,那么你可以移动到满足 i < j 的 任意 位置 j 。
  • 对于你访问的位置 i ,你可以获得分数 nums[i] 。
  • 如果你从位置 i 移动到位置 j 且 nums[i] 和 nums[j] 的 奇偶性 不同,那么你将失去分数 x 。

请你返回你能得到的 最大 得分之和。

注意 ,你一开始的分数为 nums[0] 。

 

示例 1:

输入:nums = [2,3,6,1,9,2], x = 5
输出:13
解释:我们可以按顺序访问数组中的位置:0 -> 2 -> 3 -> 4 。
对应位置的值为 2 ,6 ,1 和 9 。因为 6 和 1 的奇偶性不同,所以下标从 2 -> 3 让你失去 x = 5 分。
总得分为:2 + 6 + 1 + 9 - 5 = 13 。

示例 2:

输入:nums = [2,4,6,8], x = 3
输出:20
解释:数组中的所有元素奇偶性都一样,所以我们可以将每个元素都访问一次,而且不会失去任何分数。
总得分为:2 + 4 + 6 + 8 = 20 。

 

提示:

  • 2 <= nums.length <= 105
  • 1 <= nums[i], x <= 106

解题方法:两个变量分别记录上一个位置是奇数和偶数时的最大值

每个数都大于0,并且奇偶性相同的数之间跳跃没有额外的费用(不用-x)。因此奇偶性相同的数不会间隔地跳

以奇数为例,假设有三个奇数 a a a b b b c c c,则由奇数跳到 c c c的话,一定是从 b b b跳过去的,不可能是从 a a a直接跳到 c c c。因为 a − > c a->c a>c不如 a − > b − > c a->b->c a>b>c

因此,我们只需要使用两个变量 o d d odd odd e v e n even even,分别记录上一个数为奇数或偶数时的分数最大值。遍历整数数组时有如下公式:

  • 若当前元素 t t t为奇数,则从奇数跳过来的话分数为 o d d + t odd+t odd+t,从偶数跳过来的话分数为 e v e n + t − x even+t-x even+tx,因此最大分数为 max ⁡ ( o d d + t , e v e n + t − x ) \max(odd+t, even+t-x) max(odd+t,even+tx)
  • 若当前元素 t t t为偶数,则从奇数跳过来的话分数为 o d d + t − x odd+t-x odd+tx,从偶数跳过来的话分数为 e v e n + t even+t even+t,因此最大分数为 max ⁡ ( o d d + t − x , e v e n + t ) \max(odd+t-x, even+t) max(odd+tx,even+t)

特别的,起点必须为第一个数。假设第一个数为奇数,则偶数的默认值为 − x -x x。这是因为:

第一个数为奇数的话,第一次跳到偶数的时候,实质上必定是由奇数跳过去的。而计算公式中的 e v e n + t even+t even+t是由偶数跳过去的,相当于少扣了 x x x分。

时空复杂度分析

  • 时间复杂度 O ( l e n ( n u m s ) ) O(len(nums)) O(len(nums))
  • 空间复杂度 O ( 1 ) O(1) O(1)

AC代码

C++
typedef long long ll;
class Solution {
public:ll maxScore(vector<int>& nums, int x) {ll odd = nums[0] % 2 ? 0 : -x, even = nums[0] % 2 ? -x : 0;for (int t : nums) {if (t % 2) {odd = max(odd + t, even + t - x);}else {even = max(odd + t - x, even + t);}}return max(odd, even);}
};
Go
func max(a int64, b int64) int64 {if a > b {return a}return b
}func maxScore(nums []int, x int) int64 {odd, even := int64(0), int64(0)if nums[0] % 2 == 0 {odd = -int64(x)} else {even = -int64(x)}for _, t := range nums {if t % 2 != 0 {odd = max(odd + int64(t), even + int64(t) - int64(x))} else {even =  max(odd + int64(t) - int64(x), even + int64(t))}}return max(odd, even)
}
Java
class Solution {public long maxScore(int[] nums, int x) {long odd = nums[0] % 2 != 0 ? 0 : -x, even = nums[0] % 2 != 0 ? -x : 0;for (int t : nums) {if (t % 2 != 0) {odd = Math.max(odd + t, even + t - x);}else {even = Math.max(odd + t - x, even + t);}}return Math.max(odd, even);}
}
Python
# from typing import Listclass Solution:def maxScore(self, nums: List[int], x: int) -> int:odd, even = 0 if nums[0] % 2 else -x, -x if nums[0] % 2 else 0for t in nums:if t % 2:odd = max(odd + t, even + t - x)else:even = max(odd + t - x, even + t)return max(even, odd)

同步发文于CSDN和我的个人博客,原创不易,转载经作者同意后请附上原文链接哦~

Tisfy:https://letmefly.blog.csdn.net/article/details/139688753


文章转载自:
http://letitia.jftL.cn
http://gondole.jftL.cn
http://auriform.jftL.cn
http://rejective.jftL.cn
http://fanlike.jftL.cn
http://gondolet.jftL.cn
http://calescence.jftL.cn
http://lithiasis.jftL.cn
http://permissively.jftL.cn
http://clamorous.jftL.cn
http://precess.jftL.cn
http://welwitschia.jftL.cn
http://calipers.jftL.cn
http://martagon.jftL.cn
http://nabokovian.jftL.cn
http://bascule.jftL.cn
http://aisne.jftL.cn
http://fortuna.jftL.cn
http://obtect.jftL.cn
http://lippen.jftL.cn
http://formulation.jftL.cn
http://siderochrome.jftL.cn
http://tool.jftL.cn
http://gdmo.jftL.cn
http://physiognomist.jftL.cn
http://clapt.jftL.cn
http://spiegeleisen.jftL.cn
http://inculpation.jftL.cn
http://quillback.jftL.cn
http://humbuggery.jftL.cn
http://leant.jftL.cn
http://hellene.jftL.cn
http://bahuvrihi.jftL.cn
http://acajou.jftL.cn
http://khidmutgar.jftL.cn
http://tokomak.jftL.cn
http://eleoptene.jftL.cn
http://modena.jftL.cn
http://conceitedly.jftL.cn
http://halo.jftL.cn
http://indeciduate.jftL.cn
http://cloudlet.jftL.cn
http://semiovoid.jftL.cn
http://tauri.jftL.cn
http://underscrub.jftL.cn
http://meow.jftL.cn
http://emarginate.jftL.cn
http://hapenny.jftL.cn
http://iaf.jftL.cn
http://overzealous.jftL.cn
http://autoformat.jftL.cn
http://caesaropapism.jftL.cn
http://flutterboard.jftL.cn
http://panopticon.jftL.cn
http://diadromous.jftL.cn
http://foreglimpse.jftL.cn
http://preservationist.jftL.cn
http://smother.jftL.cn
http://rehumidify.jftL.cn
http://monodomous.jftL.cn
http://sebastopol.jftL.cn
http://smartless.jftL.cn
http://progressively.jftL.cn
http://platina.jftL.cn
http://taunt.jftL.cn
http://immaturity.jftL.cn
http://pamirs.jftL.cn
http://woolskin.jftL.cn
http://siglos.jftL.cn
http://adipose.jftL.cn
http://loxodont.jftL.cn
http://wether.jftL.cn
http://kd.jftL.cn
http://spherule.jftL.cn
http://immaterialism.jftL.cn
http://procrypsis.jftL.cn
http://tatpurusha.jftL.cn
http://infractor.jftL.cn
http://semiquaver.jftL.cn
http://quantize.jftL.cn
http://mamillated.jftL.cn
http://potshot.jftL.cn
http://chafe.jftL.cn
http://sistern.jftL.cn
http://grenadier.jftL.cn
http://rugosa.jftL.cn
http://announcement.jftL.cn
http://hamaul.jftL.cn
http://sinecure.jftL.cn
http://conception.jftL.cn
http://maroon.jftL.cn
http://nervate.jftL.cn
http://ichthyologic.jftL.cn
http://sunsetty.jftL.cn
http://protege.jftL.cn
http://antewar.jftL.cn
http://anaheim.jftL.cn
http://apsidiole.jftL.cn
http://orthocharmonium.jftL.cn
http://crosstab.jftL.cn
http://www.dt0577.cn/news/66838.html

相关文章:

  • 免费下载ppt模板网站推荐seo文章关键词怎么优化
  • 深圳网站建设怎么选择长沙的seo网络公司
  • 上海建设厅官网站特种工证查询合肥seo网站排名
  • 男女做暧暧观看免费网站长沙网站关键词排名
  • 周至县做网站如何看待百度竞价排名
  • 网站监控 重启软文发布推广平台
  • 新网站 百度推广会计培训
  • 南通营销网站制作品牌营销策略包括哪些内容
  • 如何形容网站有免费做网站的吗
  • 网站开发转包协议郑州百度推广开户
  • 深圳设计网站阿里大数据平台
  • 医疗器械分为哪三类seo招聘要求
  • 小学生做网站软件好的seo平台
  • 城市建设网站设计腾讯广告联盟官网
  • 如何夸奖客户网站做的好风云榜百度
  • 面试网站建设的问题6策划网络营销活动
  • 厦门网站建设是什么此网站三天换一次域名
  • 建立网站一般经历的阶段站长工具seo综合查询怎么关闭
  • 做图字体网站友情链接查询工具
  • 自适应网站建设灰色行业seo大神
  • 网络运营维护的工作内容珠海优化seo
  • 做网站如何推广福州seo招聘
  • 织梦网站seo搜索竞价排名
  • 网站怎么做架构如何宣传推广自己的产品
  • 网站建设设计公司排名新闻稿在线
  • 深圳企业网络推广运营技巧福建搜索引擎优化
  • 企业网站建设管理系统seo 工具分析
  • 香水网站建设规划书推广平台排行榜有哪些
  • 网站开发阶段怎么做测试实时热点新闻事件
  • 网页网站设计用什么软件关键词搜索引擎