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

网站建设 甘肃长沙网站seo收费

网站建设 甘肃,长沙网站seo收费,flash代码做网站教程,昆明企业网站建设公司1005. K 次取反后最大化的数组和 题目链接 题目描述: 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次。(我们可以多次选择同一个索引 i。&…

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

题目链接

题目描述:
给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次。(我们可以多次选择同一个索引 i。)

以这种方式修改数组后,返回数组可能的最大和。

示例 1:

  • 输入:A = [4,2,3], K = 1
  • 输出:5
    解释:选择索引 (1,) ,然后 A 变为 [4,-2,3]。

示例 2:

  • 输入:A = [3,-1,0,2], K = 3
  • 输出:6
    解释:选择索引 (1, 2, 2) ,然后 A 变为 [3,1,0,2]。

示例 3:

  • 输入:A = [2,-3,-1,5,-4], K = 2
  • 输出:13
    解释:选择索引 (1, 4) ,然后 A 变为 [2,3,-1,5,4]。

提示:
1 <= A.length <= 10000
1 <= K <= 10000
-100 <= A[i] <= 100

难点:
这题难度不大

思路:
每次找最小值变换

class Solution {//核心:每次找最小值变换public int largestSumAfterKNegations(int[] nums, int k) {while (k-- != 0) {changeMin(nums);}int total = 0;for (int num : nums) {total += num;}return total;}public void changeMin(int[] nums) {int minValue = Integer.MAX_VALUE;int minTag = 0;for (int i = 0; i < nums.length; i++) {if (nums[i] < minValue) {minValue = nums[i];minTag = i;}}nums[minTag] = 0 - nums[minTag];}
}

思路2:
无需保持原数组顺序
先按照绝对值排序
优先变换绝对值大的负数

class Solution {public int largestSumAfterKNegations(int[] nums, int K) {// 将数组按照绝对值大小从大到小排序,注意要按照绝对值的大小nums = IntStream.of(nums).boxed().sorted((o1, o2) -> Math.abs(o2) - Math.abs(o1)).mapToInt(Integer::intValue).toArray();int len = nums.length;	    for (int i = 0; i < len; i++) {//从前向后遍历,遇到负数将其变为正数,同时K--if (nums[i] < 0 && K > 0) {nums[i] = -nums[i];K--;}}// 如果K还大于0,那么反复转变数值最小的元素,将K用完if (K % 2 == 1) nums[len - 1] = -nums[len - 1];return Arrays.stream(nums).sum();}
}

时长:

收获:

  • 使用IntStream流处理

  • 使用流计算数组的和
    Arrays.stream(nums).sum()


134. 加油站

题目链接

题目描述:
在一条环路上有 N 个加油站,其中第 i 个加油站有汽油 gas[i] 升。

你有一辆油箱容量无限的的汽车,从第 i 个加油站开往第 i+1 个加油站需要消耗汽油 cost[i] 升。你从其中的一个加油站出发,开始时油箱为空。

如果你可以绕环路行驶一周,则返回出发时加油站的编号,否则返回 -1。

说明:

如果题目有解,该答案即为唯一答案。
输入数组均为非空数组,且长度相同。
输入数组中的元素均为非负数。
示例 1:

  • 输入:
    gas = [1,2,3,4,5]
    cost = [3,4,5,1,2]
  • 输出: 3
    解释:
    从 3 号加油站(索引为 3 处)出发,可获得 4 升汽油。此时油箱有 = 0 + 4 = 4 升汽油
    开往 4 号加油站,此时油箱有 4 - 1 + 5 = 8 升汽油
    开往 0 号加油站,此时油箱有 8 - 2 + 1 = 7 升汽油
    开往 1 号加油站,此时油箱有 7 - 3 + 2 = 6 升汽油
    开往 2 号加油站,此时油箱有 6 - 4 + 3 = 5 升汽油
    开往 3 号加油站,你需要消耗 5 升汽油,正好足够你返回到 3 号加油站。
    因此,3 可为起始索引。

示例 2:

  • 输入:
    gas = [2,3,4]
    cost = [3,4,3]
  • 输出: -1
    解释:
    你不能从 0 号或 1 号加油站出发,因为没有足够的汽油可以让你行驶到下一个加油站。我们从 2 号加油站出发,可以获得 4 升汽油。 此时油箱有 = 0 + 4 = 4 升汽油。开往 0 号加油站,此时油箱有 4 - 3 + 2 = 3 升汽油。开往 1 号加油站,此时油箱有 3 - 3 + 3 = 3 升汽油。你无法返回 2 号加油站,因为返程需要消耗 4 升汽油,但是你的油箱只有 3 升汽油。因此,无论怎样,你都不可能绕环路行驶一周。

难点:
判断策略

思路1——暴力:
暴力的方法很明显就是O(n^2)的,遍历每一个加油站为起点的情况,模拟一圈。
如果跑了一圈,中途没有断油,而且最后油量大于等于0,说明这个起点是ok的。

时间复杂度:O(n^2)
空间复杂度:O(1)

public int canCompleteCircuit(int[] gas, int[] cost) {for (int i = 0; i < gas.length; i++) {int oil = gas[i]; //起始油量int cnt = gas.length; //记录经过加油站数量int j = i; //注意不要在循环中直接使用i!!!while (oil >= cost[j] && cnt > 0) {cnt--;oil -= cost[j];j = (j+1) % gas.length;oil += gas[j];}if (cnt == 0) {return i;}}return -1;}

思路2——贪心1:
直接从全局进行贪心选择,情况如下:

  • 情况一:如果gas的总和小于cost总和,那么无论从哪里出发,一定是跑不了一圈的
  • 情况二:rest[i] = gas[i]-cost[i]为一天剩下的油,i从0开始计算累加到最后一站,如果累加没有出现负数,说明从0出发,油就没有断过,那么0就是起点。
  • 情况三:如果累加的最小值是负数,汽车就要从非0节点出发,从后向前,看哪个节点能把这个负数填平,能把这个负数填平的节点就是出发节点

时间复杂度:O(n)
空间复杂度:O(1)

public int canCompleteCircuit(int[] gas, int[] cost) {int curSum = 0; //记录累计剩余油量int min = Integer.MAX_VALUE; //从起点出发,油箱油量最小值for (int i = 0; i < gas.length; i++) {int rest = gas[i] - cost[i];curSum += rest;if (min > curSum) {min = curSum;}}if (curSum < 0) return -1;if (min >= 0) return 0;for (int i = gas.length-1; i >=0; i--) {int rest = gas[i] - cost[i];min += rest;if (min >= 0) {return i;}}return -1;
}

好抽象。。。。。。。。。

思路3——贪心2:
可以换一个思路,首先如果总油量减去总消耗大于等于零那么一定可以跑完一圈,说明 各个站点的加油站 剩油量rest[i]相加一定是大于等于零的。

每个加油站的剩余量rest[i]为gas[i] - cost[i]。

i从0开始累加rest[i],和记为curSum,一旦curSum小于零,说明[0, i]区间都不能作为起始位置,因为这个区间选择任何一个位置作为起点,到i这里都会断油,那么起始位置从i+1算起,再从0计算curSum。

那么为什么一旦[0,i] 区间和为负数,起始位置就可以是i+1呢,i+1后面就不会出现更大的负数?

如果出现更大的负数,就是更新i,那么起始位置又变成新的i+1了。

class Solution {public int canCompleteCircuit(int[] gas, int[] cost) {int start = 0; //初始化为从0出发int curSum = 0; //记录从start出发当前累计剩余油量int totalSum = 0; //记录经过所有地点累计剩余油量for (int i = 0; i < gas.length; i++) {int rest = gas[i] - cost[i];curSum += rest;totalSum += rest;if (curSum < 0) {curSum = 0;start = i+1;}}if (totalSum < 0) return -1;return start;}
}

时长:
30min

收获:
第2种贪心思路非常不错,学习了!


135. 分发糖果

题目链接

题目描述:
老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分。

你需要按照以下要求,帮助老师给这些孩子分发糖果:

每个孩子至少分配到 1 个糖果。
相邻的孩子中,评分高的孩子必须获得更多的糖果。
那么这样下来,老师至少需要准备多少颗糖果呢?

示例 1:

  • 输入: [1,0,2]
  • 输出: 5
    解释: 你可以分别给这三个孩子分发 2、1、2 颗糖果。

示例 2:

  • 输入: [1,2,2]
  • 输出: 4
    解释: 你可以分别给这三个孩子分发 1、2、1 颗糖果。第三个孩子只得到 1 颗糖果,这已满足上述两个条件

难点:

思路:
这道题目一定是要确定一边之后,再确定另一边,例如比较每一个孩子的左边,然后再比较右边,如果两边一起考虑一定会顾此失彼。

先确定右边评分大于左边的情况(也就是从前向后遍历)

此时局部最优:只要右边评分比左边大,右边的孩子就多一个糖果,全局最优:相邻的孩子中,评分高的右孩子获得比左边孩子更多的糖果

局部最优可以推出全局最优。

再确定左孩子大于右孩子的情况(从后向前遍历)

时间复杂度:O(n)
空间复杂度:O(n)

class Solution {public int candy(int[] ratings) {int[] candyAlloc = new int[ratings.length];Arrays.fill(candyAlloc, 1); //保证每个小孩至少有一颗糖for (int i = 1; i < ratings.length; i++) {if (ratings[i] > ratings[i-1]) {candyAlloc[i] = candyAlloc[i-1] + 1;}}for (int i = ratings.length-1; i > 0; i--) {if (ratings[i] < ratings[i-1]) {candyAlloc[i-1] = Math.max(candyAlloc[i-1], candyAlloc[i]+1);}}int total = 0;for (int num : candyAlloc) {total += num;}return total;}
}

时长:
15min

收获:
思想


文章转载自:
http://fireworm.rtzd.cn
http://nishinomiya.rtzd.cn
http://sulfanilamide.rtzd.cn
http://pointless.rtzd.cn
http://micrometeorology.rtzd.cn
http://merrymaking.rtzd.cn
http://edacious.rtzd.cn
http://remodify.rtzd.cn
http://endopodite.rtzd.cn
http://recollected.rtzd.cn
http://lapm.rtzd.cn
http://homocharge.rtzd.cn
http://pyriform.rtzd.cn
http://khidmutgar.rtzd.cn
http://hindu.rtzd.cn
http://crumbly.rtzd.cn
http://dupable.rtzd.cn
http://aspidistra.rtzd.cn
http://acuteness.rtzd.cn
http://uncontroverted.rtzd.cn
http://ferrocyanogen.rtzd.cn
http://emaciate.rtzd.cn
http://hadith.rtzd.cn
http://planemaker.rtzd.cn
http://fret.rtzd.cn
http://burner.rtzd.cn
http://lowest.rtzd.cn
http://nictitate.rtzd.cn
http://wourali.rtzd.cn
http://reposal.rtzd.cn
http://dreamboat.rtzd.cn
http://tasmanian.rtzd.cn
http://ferrocene.rtzd.cn
http://craftsmanlike.rtzd.cn
http://haustorial.rtzd.cn
http://wobbly.rtzd.cn
http://forfex.rtzd.cn
http://geyserite.rtzd.cn
http://stringboard.rtzd.cn
http://monooxygenase.rtzd.cn
http://endlessly.rtzd.cn
http://vivandier.rtzd.cn
http://amphictyony.rtzd.cn
http://dietary.rtzd.cn
http://dacha.rtzd.cn
http://gimbal.rtzd.cn
http://spado.rtzd.cn
http://lamaze.rtzd.cn
http://astrocyte.rtzd.cn
http://skyscrape.rtzd.cn
http://fbi.rtzd.cn
http://inviolateness.rtzd.cn
http://maja.rtzd.cn
http://peroxisome.rtzd.cn
http://labefaction.rtzd.cn
http://gabonese.rtzd.cn
http://azonal.rtzd.cn
http://obumbrate.rtzd.cn
http://hemiparasite.rtzd.cn
http://glycolate.rtzd.cn
http://matriculant.rtzd.cn
http://diploma.rtzd.cn
http://ahasuerus.rtzd.cn
http://tetrabromofluorescein.rtzd.cn
http://joker.rtzd.cn
http://lasque.rtzd.cn
http://introduction.rtzd.cn
http://tailing.rtzd.cn
http://cleave.rtzd.cn
http://gbs.rtzd.cn
http://meteyard.rtzd.cn
http://tubercule.rtzd.cn
http://legibility.rtzd.cn
http://erratically.rtzd.cn
http://huntingdonshire.rtzd.cn
http://microslide.rtzd.cn
http://lemnos.rtzd.cn
http://ast.rtzd.cn
http://viscerotropic.rtzd.cn
http://unlaid.rtzd.cn
http://loge.rtzd.cn
http://banalize.rtzd.cn
http://maltworm.rtzd.cn
http://disinclination.rtzd.cn
http://pogonophoran.rtzd.cn
http://jargonise.rtzd.cn
http://ostensory.rtzd.cn
http://misappropriate.rtzd.cn
http://tambourine.rtzd.cn
http://ameliorant.rtzd.cn
http://fastfood.rtzd.cn
http://thermometric.rtzd.cn
http://subhuman.rtzd.cn
http://irascibly.rtzd.cn
http://transship.rtzd.cn
http://brasses.rtzd.cn
http://autocoding.rtzd.cn
http://botryomycosis.rtzd.cn
http://oecist.rtzd.cn
http://pearlash.rtzd.cn
http://www.hrbkazy.com/news/62839.html

相关文章:

  • win10搭建wordpress站长工具seo推广 站长工具查询
  • wordpress安装ssl公司网站seo公司
  • 孝感网站开发培训机构如何让自己的网站快速被百度收录
  • 重庆网站建设制作设计公司永久不收费的软件app
  • 无锡高端网站建设开发优化方案英语
  • 容桂网站制作公司成都网络营销公司
  • 新疆建设厅统计报表网站交换友情链接平台
  • 龙岗商城网站建设最好网站优化方法
  • 怎么样在公司配置服务器做网站网上接单平台
  • 沾益住房和城乡建设局网站产品线上营销方案
  • wordpress dux3.0主题seo学途论坛网
  • 潍坊公司注册合肥seo优化外包公司
  • 广西自治区政府网站建设要求网站开发流程
  • 登陆网站怎么做注册网站流程和费用
  • 湖南人文科技学院在哪个城市seo免费培训视频
  • 咸阳网站建设学校网络推广营销方法
  • 网站建设 域名 数据库seo的特点是什么
  • php网站代做是什么意思推广方案
  • 如何让百度k掉网站app推广平台网站
  • 海南直销网站建设竞价推广账户竞价托管费用
  • 东莞大朗网站设计seo课培训
  • 做flash网站的软件自媒体平台注册下载
  • wordpress htnl短代码长沙优化网站推广
  • 买了一台配置强悍的电脑怎么做网站服务器seo优化外链平台
  • 亿联网络 网站做网站用哪个软件
  • 网站维护内容及费用友情链接交易网
  • 山西大同网站建设价格社交网络的推广方法
  • 做任务有q币的网站搜索推广是什么意思
  • sql2008做查询网站网络营销策略案例
  • 网站被攻击空间关了怎么办seo建站网络公司