LeetCode刷题笔录Maximum Subarray

010Editor实战:5分钟破解Zip伪加密文件(附BUUCTF真题解析) 本文详细介绍了如何使用010Editor快速破解Zip伪加密文件,包括BUUCTF真题的完整解析。通过修改关键字节,轻松解除伪加密状态,适用于CTF竞赛和实际工作场景,提升文件处理效率。 阅读详情

Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

For example, given the array [−2,1,−3,4,−1,2,1,−5,4],
the contiguous subarray [4,−1,2,1] has the largest sum = 6.

click to show more practice.

More practice:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

动态规划,注意这里必须要有至少一个元素。因此条件就是对于A[i],要么结果里有,要么没有。

public class Solution {
    public int maxSubArray(int[] A) {
        int[] x = new int[A.length];
        x[0] = A[0];
        int max = x[0];
        for(int i = 1; i < A.length; i++){
            x[i] = Math.max(x[i - 1] + A[i], A[i]);
            max = Math.max(x[i], max);
        }

        return max;
    }
}

Divide and conquer的做法是把数组分成左右两边,然后:
1.找左边的max subarray
2.找右边的max subarray
3.找包含中间元素的max subarray
对于1和2,用递归就行了。需要写一个函数来找3
public int maxThroughMid(int[] arr, int low, int mid, int high){
        if(low == high)
            return arr[low];
        int leftMax = Integer.MIN_VALUE;
        int sum = 0;
        //find the max subarray on the left of the mid
        for(int i = mid; i >= low; i--){
            sum += arr[i];
            leftMax = maxOfTwo(sum, leftMax);
        }
        //the max on the right
        sum = 0;
        int rightMax = Integer.MIN_VALUE;
        for(int i = mid + 1; i <= high; i++){
            sum += arr[i];
            rightMax = maxOfTwo(sum, rightMax);
        }
        return leftMax + rightMax;
    }

代码就是这样:
public class Solution {
    public int maxSubArray(int[] A) {
        return maxSubArrayRecur(A, 0, A.length - 1);
    }
    
    public int maxSubArrayRecur(int[] arr, int low, int high){
        if(low == high){
            return arr[low];
        }
        int mid = (low + high) / 2;
        
        return maxOfThree(maxSubArrayRecur(arr, low, mid), maxSubArrayRecur(arr, low + 1, high), maxThroughMid(arr,low, mid, high));
    }
    
    public int maxOfThree(int a, int b, int c){
        return maxOfTwo(maxOfTwo(a, b), c);
    }
    
    public int maxOfTwo(int a, int b){
        if(a >= b)
            return a;
        else
            return b;
    }
    //calculate the max subarray contanning the element arr[mid]
    public int maxThroughMid(int[] arr, int low, int mid, int high){
        if(low == high)
            return arr[low];
        int leftMax = Integer.MIN_VALUE;
        int sum = 0;
        //find the max subarray on the left of the mid
        for(int i = mid; i >= low; i--){
            sum += arr[i];
            leftMax = maxOfTwo(sum, leftMax);
        }
        //the max on the right
        sum = 0;
        int rightMax = Integer.MIN_VALUE;
        for(int i = mid + 1; i <= high; i++){
            sum += arr[i];
            rightMax = maxOfTwo(sum, rightMax);
        }
        return leftMax + rightMax;
    }
}

但是online judge通不过,总是超时,先不管了。

SpringBoot多线程查询实战-查询库中所有数据多线程实现 但是,在数据量较大且服务器资源足够的情况下,多线程方法通常能够显著提高查询效率。此外,由于它是同步执行的,没有利用并行处理的能力,所以在处理大量数据时可能会比较慢。这个方法采用了分页查询的方式,每次只获取一部分数据,从而避免了内存溢出的问题。然而,它也是同步执行的,没有利用多线程或并行处理的能力。如果每次分页查询的数据量仍然很大,或者需要查询的轮数很多,那么这种方法的效率可能仍然会受到限制。​ ​ ​在比较这三种方式的效率时,我们需要考虑几个关键因素:数据量的大小、查询操作的复杂性、以及并行处理的能力。 阅读详情

相关推荐

dq轴坐标系下的电路方程

dq轴坐标系下的电路方程 dq轴坐标系是一种常见的电路方程,但是再dq轴坐标系下电路元件的电压电流dq轴存在一定的耦合,本文针对三相电路推导了dq轴坐标系下的电路电路方程进行一个推导,首先先列出来派克变换的公式 派克变换 [AdAqA0]=23[cos⁡(θ)cos⁡(θ−2π/3)cos⁡(θ+2π/3)−sin⁡(θ)−sin⁡(θ−2π/3)−sin⁡(θ+2π/3)1/21/21/2][AaAbAc]\left[\begin{array}{c}A_d\\A_q\\A_0 \end{array}\ri

Mr.Young的博客 1万+

LeetCode刷题笔录 Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. Fo

likecool21的专栏 1万+

不刷机!Jetson AGX Orin无损更新内核与设备树的两种安全方法(附备份恢复脚本)

本文详细介绍了Jetson AGX Orin在不刷机的情况下安全更新内核与设备树的两种方法,包括extlinux.conf动态加载和分区精准刷新方案。通过备份恢复脚本和工业级灾备方案,确保零数据丢失,适用于边缘AI和机器人等关键场景。

weixin_40192258的博客 307

LeetCode刷题笔录 Two Sum

Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the

likecool21的专栏 1万+

U-Boot 适配新型号 SPI Nor Flash 实战解析

本文详细解析了U-Boot适配新型号SPI Nor Flash的全过程,从JEDEC ID识别错误入手,深入源码分析Flash信息数据库,结合实际手册参数添加新设备支持。重点介绍了设备树同步和验证调试的关键步骤,并总结了常见问题与解决方案,为嵌入式开发者提供实用参考。

weixin_29170327的博客 197

YOLOv8魔改网络后训练全零?别慌,关掉AMP混合精度训练试试

本文探讨了YOLOv8魔改网络后训练出现全零指标的问题,指出AMP混合精度训练与自定义网络结构的不兼容性。通过分析梯度消失现象和AMP工作原理,提供了关闭AMP、调整学习率等解决方案,并建议预防性开发实践以避免类似问题。

weixin_28338005的博客 321

LeetCode刷题笔录 Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key.Th

likecool21的专栏 6928

Android SDK安装教程(超详细),从零基础入门到精通,从这篇开始_android sdk下载安装

Android SDK安装教程(超详细),从零基础入门到精通,从这篇开始_android sdk下载安装

Jinmindong 5010

Stanford Algorithms学习:Clustering 2

这是紧接上面的一道题,比较有趣 Question 2 In this question your task is again to run the clustering algorithm from lecture, but on a MUCH bigger graph. So big, in fact, that the distances (i.e., edge costs

likecool21的专栏 2567

Flatten 层的深度理解与运用

Flatten 层的深度理解与运用

你要拥有一个你喜欢并且热爱的人生 7029

YOLOv9原创自研:卷积魔改 | 一种新颖的风车形卷积(PConv)融合注意力机制,加强微弱小目标检测能力

为了进一步提升小目标检测能力,风车形卷积(PConv)融合注意力机制进一步提升小目标检测能力

①答疑群聊服务;②YOLO大模型知识问答系统;③计算机视觉论文生成智能体; 359

Stanford Algorithms学习:Clustering

第二周的第一个编程作业,是利用贪婪算法来实现一个clustering的问题,和ML里学的unsupervised learning差不多。 Question 1 In this programming problem and the next you'll code up the clustering algorithm from lecture for computing a

likecool21的专栏 2507

2 硬件工程师笔面试高频知识考点真题解析——电容

本文全面解析了电容在电子电路中的关键应用与技术要点。主要内容包括:1. 电容选型核心要素:介质材质(陶瓷/电解/钽电容等)、容值精度、耐压等级、温度特性、ESR参数等,不同应用场景(滤波/去耦/谐振等)的选型策略。2. 典型电路应用原理:详细阐述去耦电容布局规则、晶振负载电容作用、共射放大电路旁路电容功能,以及RC电路的滤波/定时等应用。3. 特殊问题解决方案:MLCC啸叫机理与对策、寄生电容影响与消除、高速信号线电容使用禁忌等工程实践难题。4. 安全规范要点:X/Y电容的区别与选用、电解/钽电容防反接措施

XU157303764的博客 1557

LeetCode 刷题笔录 Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link

likecool21的专栏 1806

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

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

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

LeetCode刷题笔录Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word:

likecool21的专栏 1296

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

LeetCode刷题笔录Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

likecool21的专栏 1109

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

LeetCode刷题笔录Unique Binary Search Trees

这里有个讲解(印度人口音真难懂)

likecool21的专栏 1043

LeetCode刷题笔录 Pascal's Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 这题比较简单

likecool21的专栏 1043

LeetCode刷题笔录Container With Most Water

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Fin

likecool21的专栏 1001

LeetCode刷题笔录 Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 随手写的一个,正常的读出每一位的数字然后反序: import java.lang.Math; public class Solution { public int revers

likecool21的专栏 941

LeetCode刷题笔录Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the tota

likecool21的专栏 878

LeetCode刷题笔录Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the f

likecool21的专栏 865

LeetCode刷题笔录Convert Sorted Array to Balanced Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这题可以kan'chu'lai

likecool21的专栏 855

求集合的交集/差集

先利用外部排序使两个磁盘文件有序,然后顺序读取两个文件,使用双指针求交集。在进阶 1 中,我们是通过双指针去做的,最坏情况下我们需要完整遍历。时,O(n log m) 可能明显优于 O(n + m)。的,这就使得我们可以通过从 1~1000 遍历来确定交集。,它们已经按非降序排序,请你返回两个数组的。和上一题的区别在于,这里需要找到重复的元素。返回一个数组,数组中的每个元素在。的每个字符串中都出现的共用字符(**注意:**列表中的整数可以按。较小,可以完整加载到内存,而。正整数组成的一个非空数组,按。

jyyyyx的算法之旅 111

LeetCode Hot 100 二叉树专题(二)3 题(Java)

本篇文章共 3 题:543. 二叉树的直径、102. 二叉树的层序遍历、108. 将有序数组转换为二叉搜索树。 每道题都统一按「① 题目分析 & 解题思路 → ② 常见解法(附代码)及讲解 → ③ 知识点总结」的顺序组织;代码使用 Java 编写,节点定义沿用 LeetCode 默认的 TreeNode。

2401_88612756的博客 352

LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)

LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等) 题目描述: 给定三个字符串 s1、s2、s3,请你帮忙验证 s3 是否是由 s1 和 s2 交错 组成的。 两个字符串 s 和 t 交错 的定义与过程如下,其中每个字符串都会被分割成若干 非空 子字符串: - s = s1 + s2 + ... + sn - t = t1 + t2 + ... + tm - |n - m| <= 1 - 交错 是 s1 + t

huayimenghan的博客 202
上一篇: LeetCode刷题笔录Climbing Stairs
下一篇: LeetCode刷题笔录 Remove Element
likecool21
博客等级 码龄14年 14粉丝 123原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值