![](https://img-blog.csdnimg.cn/20200301175521116.jpeg?x-oss-process=image/resize,m_fixed,h_224,w_224)
算法技巧-前缀和
文章平均质量分 84
各种乱七八糟的前缀和的用法
memcpy0
希望探索文理结合的自由之路。
展开
-
LeetCode 统计美丽子字符串 II【质因子分解,前缀和,哈希表】困难
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-11-26 22:11:45 · 1276 阅读 · 0 评论 -
LeetCode 2433. 找出前缀异或的原始数组【位运算,数组】1366
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-11-01 06:54:41 · 288 阅读 · 0 评论 -
LeetCode 1402. 做菜顺序【排序,动态规划;贪心,前缀和,递推】1679
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-10-22 14:50:56 · 184 阅读 · 0 评论 -
LeetCode 2483. 商店的最少代价【字符串,前后缀分解】1494
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-10-16 17:24:17 · 115 阅读 · 0 评论 -
LeetCode 2256. 最小平均差【数组,前后缀分解】1394
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-10-16 15:35:03 · 115 阅读 · 0 评论 -
LeetCode LCR 191. 按规则计算统计结果【前后缀分解】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。为了方便在PC上运行调试、分享代码文件,我还建立了相关的。原创 2023-10-16 11:57:00 · 118 阅读 · 0 评论 -
LeetCode 2906. 构造乘积矩阵【前后缀分解,数组】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-10-16 11:30:14 · 490 阅读 · 0 评论 -
LeetCode 2731. 移动机器人【脑筋急转弯,排序,前缀和】1922
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-10-11 00:39:18 · 109 阅读 · 0 评论 -
LeetCode 2281. Sum of Total Strength of Wizards【贡献法,单调栈,前缀和的前缀和】2621
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-08-20 22:43:25 · 232 阅读 · 0 评论 -
LeetCode 2090. K Radius Subarray Averages【前缀和,滑动窗口,数组】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-06-21 23:11:53 · 538 阅读 · 0 评论 -
LeetCode 1542. Find Longest Awesome Substring【异或前缀和,哈希表,字符串】困难
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-06-20 22:01:07 · 990 阅读 · 0 评论 -
LeetCode 1915. Number of Wonderful Substrings【异或前缀和,位运算,哈希表,字符串】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-06-20 18:32:39 · 847 阅读 · 1 评论 -
LeetCode 1248. Count Number of Nice Subarrays【前缀和,哈希表;数学;滑动窗口】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-06-20 16:43:09 · 684 阅读 · 0 评论 -
LeetCode 1371. Find the Longest Substring Containing Vowels in Even Counts【异或前缀和,位运算,哈希表,字符串】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-06-20 15:02:51 · 82 阅读 · 0 评论 -
LeetCode 1177. Can Make Palindrome from Substring【异或前缀和,位运算,字符串】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-06-20 12:28:09 · 709 阅读 · 0 评论 -
LeetCode 1171. Remove Zero Sum Consecutive Nodes from Linked List【链表,递归;哈希表,前缀和】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-06-18 11:28:57 · 97 阅读 · 0 评论 -
LeetCode 1170. Compare Strings by Frequency of the Smallest Character【哈希表,前缀和】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-06-13 23:26:02 · 56 阅读 · 0 评论 -
LeetCode 2559. Count Vowel Strings in Ranges【前缀和,字符串】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-06-05 13:37:34 · 497 阅读 · 0 评论 -
LeetCode 2389. Longest Subsequence With Limited Sum【贪心,排序,前缀和,二分上界】简单
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-03-18 10:34:39 · 397 阅读 · 0 评论 -
LeetCode 2379. Minimum Recolors to Get K Consecutive Black Blocks【字符串,前缀和,滑动窗口】简单
LeetCode题解原创 2023-03-11 17:54:51 · 98 阅读 · 0 评论 -
LeetCode 1154. Day of the Year【模拟/前缀和】简单
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。为了方便在PC上运行调试、分享代码文件,我还建立了相关的仓库。在这一仓库中,你不仅可以看到LeetCode原题链接、题解代码、题解文章链接、同类.原创 2021-12-23 21:58:27 · 188 阅读 · 0 评论 -
LeetCode 629. K Inverse Pairs Array【动态规划/前缀和】困难
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。为了方便在PC上运行调试、分享代码文件,我还建立了相关的仓库。在这一仓库中,你不仅可以看到LeetCode原题链接、题解代码、题解文章链接、同类.原创 2021-11-12 16:43:00 · 166 阅读 · 0 评论 -
LeetCode 437. Path Sum III【递归/前缀和/哈希表/回溯】中等
You are given a binary tree in which each node contains an integer value.Find the number of paths that sum to a given value.The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child原创 2020-07-08 16:01:18 · 356 阅读 · 0 评论 -
LeetCode 2017. Grid Game【前缀和/数组】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。为了方便在PC上运行调试、分享代码文件,我还建立了相关的仓库:https://github.com/memcpy0/LeetCode-Conqu.原创 2021-10-01 00:19:33 · 223 阅读 · 0 评论 -
LeetCode 497. Random Point in Non-overlapping Rectangles【数学/前缀和/二分/随机化/蓄水池抽样】
You are given an array of non-overlapping axis-aligned rectangles rects where rects[i] = [ai, bi, xi, yi] indicates that (ai, bi) is the bottom-left corner point of the ith rectangle and (xi, yi) is the top-right corner point of the ith rectangle. Design a原创 2021-09-04 13:36:30 · 196 阅读 · 0 评论 -
CCF 202104-2 邻域均值【二维前缀和】
试题背景顿顿在学习了数字图像处理后,想要对手上的一副灰度图像进行降噪处理。不过该图像仅在较暗区域有很多噪点,如果贸然对全图进行降噪,会在抹去噪点的同时也模糊了原有图像。因此顿顿打算先使用邻域均值来判断一个像素是否处于较暗区域,然后仅对处于较暗区域的像素进行降噪处理。问题描述待处理的灰度图像长宽皆为 nnn 个像素,可以表示为一个 n×nn \times nn×n 大小的矩阵 ,其中每个元素是一个 [0,L)[0, L)[0,L) 范围内的整数,表示对应位置像素的灰度值。对于矩阵中任意一个元素 Ai原创 2021-08-31 23:23:53 · 579 阅读 · 1 评论 -
LeetCode 528. Random Pick with Weight【数学/前缀和/随机化/二分】中等
You are given an array of positive integers w where w[i] describes the weight of ith index (0-indexed).We need to call the function pickIndex() which randomly returns an integer in the range [0, w.length - 1]. pickIndex() should return原创 2021-08-31 17:05:47 · 250 阅读 · 0 评论 -
LeetCode 274. H-Index【排序,计数排序+前缀和】中等
Given an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper, return compute the researcher's h-index.According to the definition of h-index on Wikipedia: A scientist has an index h if h of t原创 2021-07-12 23:29:54 · 187 阅读 · 0 评论 -
LeetCode 1894. Find the Student that Will Replace the Chalk【数组/前缀和/二分】中等
There are n students in a class numbered from 0 to n - 1. The teacher will give each student a problem starting with the student number 0, then the student number 1, and so on until the teacher reaches the student number n - 1. After that, the teacher will原创 2021-06-16 00:39:46 · 264 阅读 · 0 评论 -
LeetCode 525. Contiguous Array【前缀和/哈希表】
Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1.Example 1:Input: nums = [0,1]Output: 2Explanation: [0, 1] is the longest contiguous subarray with an equal number of 0 and 1.Example 2:Inpu原创 2021-06-02 23:52:34 · 141 阅读 · 2 评论 -
1774. Can You Eat Your Favorite Candy on Your Favorite Day
You are given a (0-indexed) array of positive integers candiesCount where candiesCount[i] represents the number of candies of the ith type you have. You are also given a 2D array queries where queries[i] = [favoriteTypei, favoriteDayi, dailyCapi]原创 2021-06-01 06:23:38 · 330 阅读 · 1 评论 -
LeetCode C++ 1442. Count Triplets That Can Form Two Arrays of Equal XOR【前缀和/数学】中等
Given an array of integers arr.We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).Let's define a and b as follows: a = arr[i] ^ arr[i + 1] ^ ... ^ arr[j - 1] b = arr[j] ^ arr[j + 1] ^ ... ^ arr[k]Note th原创 2021-05-18 03:14:24 · 158 阅读 · 0 评论 -
LeetCode C++ 1310. XOR Queries of a Subarray【前缀和/STL】中等
Given the array arr of positive integers and the array queries where queries[i] = [Li, Ri], for each query i compute the XOR of elements from Li to Ri (that is, arr[Li] xor arr[Li+1] xor … xor arr[Ri] ). Return an array containing the result for原创 2021-05-12 01:27:08 · 174 阅读 · 1 评论 -
LeetCode C++ 1738. Find Kth Largest XOR Coordinate Value【堆/二维前缀和】中等
You are given a 2D matrix of size m x n, consisting of non-negative integers. You are also given an integer k.The value of coordinate (a, b) of the matrix is the XOR of all matrix[i][j] where 0 <= i <= a < m and 0 <= j <= b < n (0-indexe原创 2021-05-07 02:20:46 · 287 阅读 · 0 评论 -
LeetCode C++ 554. Brick Wall【哈希表/前缀和】中等
There is a rectangular brick wall in front of you with n rows of bricks. The ith row has some number of bricks each of the same height (i.e., one unit) but they can be of different widths. The total width of each row is the same.Draw a vertical line from原创 2021-05-07 00:24:45 · 195 阅读 · 0 评论 -
LeetCode C++ 1732. Find the Highest Altitude【Array/前缀和】简单
There is a biker going on a road trip. The road trip consists of n + 1 points at different altitudes. The biker starts his trip on point 0 with altitude equal 0.You are given an integer array gain of length n where gain[i] is the net gain in altitude betw原创 2021-05-04 01:18:57 · 147 阅读 · 0 评论 -
LeetCode C++ 1184. Distance Between Bus Stops【Array/前缀和】简单
A bus has n stops numbered from 0 to n - 1 that form a circle. We know the distance between all pairs of neighboring stops where distance[i] is the distance between the stops number i and (i + 1) % n.The bus goes along both directions原创 2021-01-18 22:25:19 · 177 阅读 · 1 评论 -
LeetCode 面试题 17.05. Find Longest Subarray LCCI【哈希表/前缀和】中等
Given an array filled with letters and numbers, find the longest subarray with an equal number of letters and numbers.Return the subarray. If there are more than one answer, return the one which has the smallest index of its left endpoint. If there i原创 2021-01-06 00:20:45 · 247 阅读 · 0 评论 -
LeetCode C++ 1176. Diet Plan Performance【前缀和/滑动窗口】简单
A dieter consumes calories[i] calories on the i-th day. Given an integer k, for every consecutive sequence of k days (calories[i], calories[i+1], ..., calories[i+k-1] for all 0 <= i <= n-k), they look at T, the total calories con原创 2020-12-18 21:48:21 · 314 阅读 · 1 评论 -
LeetCode C++ 1365. How Many Numbers Are Smaller Than the Current Number【排序/前缀和】简单
Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] < nums[i] . Return the answer in an array.Example 1:Inp原创 2020-10-26 19:59:28 · 169 阅读 · 0 评论