[LeetCode][Array] 135. Candy

2022年TI杯模拟电⼦系统设计专题邀请赛——李萨如图形演示装置 本次装置主要是对输入的正弦信号进行频率变换,并将原输入正弦信号作为 X 轴信号、变换后的正弦信号作为 Y 轴信号,绘制李萨如图形。采用 EK-TM4C123GXL 板卡作为主控;频率变换采用“锁向环+计数器”实现 1-5 倍频的频率变换控制;李萨如图形采用两个 AD通道采集原输入的正弦信号作为 X 轴信号和频率变换后的正弦信号作为 Y 轴信号,绘制李萨如图形。 阅读详情

Problem: https://leetcode.com/problems/candy/

Solution:
Brute force

  1. 创建数组存储糖果个数,初始值都为1
  2. 遍历数组,与前后值分别进行比较
  3. 多次遍历,直到所有值都满足条件
class Solution {
    public int candy(int[] ratings) {
        int len = ratings.length;
        int[] arr = new int[len];
        Arrays.fill(arr, 1);
        boolean flag = true;
        while(flag) {
            flag = false;
            // 注意此处,存在遍历一次无法将所有candy都满足条件的情况
            // test case:[1, 2, 87, 87, 87, 2, 1]
            for (int i = 0; i < len; i++) {
                if (i > 0 && ratings[i] > ratings[i-1] && arr[i] <= arr[i-1]) {
                    arr[i] = arr[i-1] + 1;
                    flag = true;
                }
                if (i < len - 1 && ratings[i] > ratings[i+1] && arr[i] <= arr[i+1]) {
                    arr[i] = arr[i+1] + 1;
                    flag = true;
                }
            }
        }
        int sum = 0;
        for(int a:arr) {
            sum += a;
        }
        return sum;
    }
}

tbc

【24届数字IC秋招总结】笔试面试篇——投递公司及时间节点汇总 从实习、提前批到秋招投递的有进展的公司进行汇总,关于各个公司的笔试和面试经历详见其他帖子。 阅读详情

相关推荐

Code Ocean 上传代码 手把手教程

对于一些学术期刊,录用前会让把代码上传到Code Ocean上,但上传过程可能会遇到一些问题,小编分享一下自己的上传经验。他们会进行验证,验证通过,将自动发布。点此运行即可,但是运行比较慢。

u014071947的博客 1万+

LeetCode | 1005. Maximize Sum Of Array After K Negations, 134. Gas Station, 135. Candy

Link: https://leetcode.com/problems/maximize-sum-of-array-after-k-negations/Given an integer array and an integer , modify the array in the following way:You should apply this process exactly times. You may choose the same index multiple times.Return th

weixin_43615320的博客 109

永久关闭Windows更新的5种方法

很多家用电脑,如果系统自动更新的话,会变得越来越卡顿,且硬件型号兼容也并不完美。那么我们该如何彻底关闭Win11的自动更新呢?以下准备了5种方法,您可以根据自身实际情况选择合适的方法!

qq_43652793的博客 1万+

LeetCode DAY29(1005. Maximize Sum Of Array After K Negations&134. Gas Station&135. Candy)

【代码】LeetCode DAY29(1005. Maximize Sum Of Array After K Negations&134. Gas Station&135. Candy)

weixin_48078699的博客 155

VS2019 C# MySQL 学生信息增删改查(一、增删)

VS2019 C# MySQL 学生信息增删改查 由于刚刚学习VS2019,入手的第一个例子是编写一个简单的增删改查。这里,我以学生信息为例,进行详细的过程描述以及代码实现。 一、 新建一个Windows窗体应用程序 1、创建新项目界面,如下图所示。 2、更改项目名称,当然不修改也可以。我这里的是“StudentInfoSystem”;项目存放位置路径根据自己需要进行更改;“将解决方案和项目放在同一目录中”前面的勾选框,选不选都可以;点击“创建”,如下图所示。 二、前期准备工作 1、将Form1属性中

青哥 1万+

IC设计新手必看:如何正确使用去耦电容(decap)避免电源噪声?

本文深入探讨了IC设计中去耦电容(decap)的应用技巧,从核心原理到布局实现,帮助新手工程师有效抑制电源噪声。文章详细解析了MIM、MOM等电容类型的特性与选择策略,并提供了先进的布局技巧和验证方法,特别适用于28nm以下先进工艺的电路设计挑战。

weixin_30555125的博客 122

Leecode135

Leecode135 使用双数组,分别对左右做局部最优,取较大值 import java.util.Arrays; /* 使用贪心算法:每个数左右两边各自局部最优,取两者的较大值,获得的就是全局最优 */ public class Solution2 { public static void main(String[] args) { int[] rating = {1,2,2}; int candy = new Solution2().candy(rating

qq_41904699的博客 109

安路TangDynasty和FD和JTAG驱动器安装

安路FPGA产品矩阵包含SALPHOENIX高性能、SALEAGLE高性价比和SALELF低功耗三大系列,广泛应用于网络通信、消费电子等领域。配套的TangDynasty(TD)集成开发环境支持标准设计输入,提供完整优化流程与调试工具。简要介绍了TD 6.2.1版本的安装步骤。

wangjie36的博客 1067

Leetcode135. 分发糖果-代码随想录

【代码】代码随想录 Leetcode135. 分发糖果。

meeiuliuus的博客 760

uni-app - 将 base64 图片编码转为 Blob 本地文件路径(把base64类型的图片,转换成blob二进制文件流)适用于 uni-app / vue.js / JavaScript

uni-app/vue.js/JavaScript将base64转Blob,uniappapp端如何将图片的base64编码转换成File对象或Blob,uni-app/vue.js/JavaScript中拿到base64转blob对象,uniapp图片base64格式转blob格式方法,uniapp上传图片压缩转base64码与base64转图片路径,Base64/Blob/File图片文件转换,url、base64、blob相互转换方法,读取本地文件为Base64并转成B

🏆 前端领域优质创作者 1万+

解决k8s核心组件calico pod资源不创建问题

解决k8s核心组件calico pod资源不创建问题 1.问题描述 由于K8S集群Master节点故障,导致calico网络组件虽然处于Running状态,但是一直无法准备就绪,如下图所示 calico-node-lpt7h运行在master节点,第一时间紧急处理方法就是将其pod删除,让K8S自动重建该pod,使整个集群快速恢复 但是就当我将calico-node-lpt7h pod删除后,该pod始终无法自建,就好像是从来没有这个pod似的 2.解决方法 期初排查时,我觉得可能是由于当前master

江晓龙的博客 4815

Day 34 | 1005. Maximize Sum Of Array After K Negations | 134. Gas Station | 135. Candy

Day 34 | 1005. Maximize Sum Of Array After K Negations | 134. Gas Station | 135. Candy

KKKKKKyle的博客 224

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

[LeetCode][Array]121. Best Time to Buy and Sell Stock

Problem: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ Solution1: 对数组进行遍历,一一比较 class Solution { public int maxProfit(int[] prices) { int max = 0; for(int i = 0; i ...

Renoler的博客 414

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

[Leetcode] 269. Alien Dictionary

Problems: https://leetcode.com/problems/alien-dictionary/ Solution: Topological Sort 建立拓扑图并寻找所有字符的入度(buildGraph),再寻找入度为0的字符取出,邻接字符入度减一(bfs) class Solution { public String alienOrder(String[] words...

Renoler的博客 354

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

[Leetcode] 937. Reorder Data in Log Files

Problems: https://leetcode.com/problems/reorder-data-in-log-files/ 思路: 把log分开考虑,从第二个string开始看是否是digit,分为digit和letter两个list letterList重写sort方法,从第二个string开始按照字母顺序进行排序,需要注意的是如果字母顺序完全相同,则考虑index的字母排序 Solu...

Renoler的博客 341

[Leetcode] 253. Meeting Rooms II

Problems: https://leetcode.com/problems/meeting-rooms-ii/ Solution1: 首先对不同intervals根据开始时间进行排序,再利用priority queue存储不同rooms的结束时间; (* priority queue只能帮助确定peek是最小(或最大)值,queue内其他元素排序未知) 对不同的intervals和peek进行...

Renoler的博客 280

[LeetCode][Array] 53. Maximum Array

Problems: https://leetcode.com/problems/maximum-subarray/ Solution1: Brute Force - Time Limit Exceeded class Solution { public int maxSubArray(int[] nums) { // brute force int ans ...

Renoler的博客 273

[LeetCode][String] 383. Ransom Note

Problem: https://leetcode.com/problems/ransom-note/ Solution: class Solution { public boolean canConstruct(String ransomNote, String magazine) { HashMap<Character, Integer> count = n...

Renoler的博客 216

[Leetcode][BFS/DFS]785. Is Graph Bipartite?

Problems: https://leetcode.com/problems/is-graph-bipartite/ Solution1: DFS 将相邻的点标上不同的颜色,判断依据为相邻两个node不可以颜色相同 利用stack存储邻接node class Solution { public boolean isBipartite(int[][] graph) { in...

Renoler的博客 213

[LeetCode][Array] 42. Trapping Rain Water

Problem: https://leetcode.com/problems/trapping-rain-water/ Solution1: 找出当前列的左边最高列和右边最高列,取较小的列与当前列进行比较,若当前列矮于较小列,则结果累加 class Solution { public int trap(int[] height) { int ans = 0; ...

Renoler的博客 204

[LeetCode] 74. Search a 2D Matrix

Problems: https://leetcode.com/problems/search-a-2d-matrix/ Solution1: class Solution { public boolean searchMatrix(int[][] matrix, int target) { // 矩阵为空,需要注意的是depth和width都需要判断 if(mat...

Renoler的博客 200

[Leetcode] 426. Convert Binary Search Tree to Sorted Doubly Linked List

Problem: https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/ Soluiton: 使用inorder traversal读取值,读出来的node和cur double linked 最后再把dummy node删除 class Solution { public...

Renoler的博客 200

[LeetCode][Array] 217. Contains Duplicate

ProblemL: https://leetcode.com/problems/contains-duplicate/ Solution1: class Solution { public boolean containsDuplicate(int[] nums) { // 暴力枚举 for(int i = 0; i < nums.length; i++...

Renoler的博客 193

[Leetcode] 724. Find Pivot Index

Problems: https://leetcode.com/problems/find-pivot-index/ class Solution { public int pivotIndex(int[] nums) { if(nums.length == 0) { return -1; } else { in...

Renoler的博客 189

[Leetcode] 528. Random Pick with Weight

还需要进一步理解 Solution1: class Solution { Random rand; int[] a; public Solution(int[] w) { this.rand = new Random(); this.a = new int[w.length]; a[0] = w[0]; for...

Renoler的博客 187

[Leetcode] 973. K Closest Points to Origin

Problems: https://leetcode.com/problems/k-closest-points-to-origin/ Solution: 重点在于如何绑定点坐标和点到原点的距离 class Solution { public int[][] kClosest(int[][] points, int K) { int len = points.length;...

Renoler的博客 187
上一篇: [LeetCode][Array] 287. Find the Duplicate Number
下一篇: [LeetCode] 5. Longest Palindromic Substring
Shlyan
博客等级 码龄9年 0粉丝 98原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值