Segment Tree 线段树
线段树解决的是「区间和」的问题,且该「区间」会被修改
一个区间的例子是对于 nums = [1, 2, 3, 4, 5]
多次求某些区间的和,是不是首先想到了利用「前缀和」。
但是如果 nums 会被修改呢?比如:
- 把第 i 个元素修改成 x
- 把第 i 个元素增加 x
- 把区间 [i, j] 内的元素都增加 x
此时,如果我们再使用「前缀和」,就没那么高效了。因为每一次更新,前缀和数组必须也随之更新,时间复杂度为 O(n)。
线段树解决的是「区间和」的问题,且该「区间」会被修改
一个区间的例子是对于 nums = [1, 2, 3, 4, 5]
多次求某些区间的和,是不是首先想到了利用「前缀和」。
但是如果 nums 会被修改呢?比如:
此时,如果我们再使用「前缀和」,就没那么高效了。因为每一次更新,前缀和数组必须也随之更新,时间复杂度为 O(n)。
故事买卖类的问题主要使用动态规划进行解决,只是复杂程度不同罢了。
递归方程的建立考虑以下问题:
目的:能够获得的最大利润
联系:一共有n天需要记录,每天有两种状态,当天有股票或没股票
优化:是否需要记录每一天,还是只与上一天的结果有关。
二分搜索的关键在于:
[0, len-1]
这个闭区间,还是 [0, len)
这个开区间.while(left <= right)
;如果是开区间,则使用while(left < right)
。很好理解,考虑列表只有一个因素的情况,闭区间是[1,1]
需要等于搜索。if(nums[mid] == target) return mid;
判断返回。if (nums[mid] == target) right = mid - 1;
锁定左侧边界,返回值为:return nums[left] == target ? left : -1;
if (nums[mid] == target) left = mid + 1;
锁定右侧边界,返回值为:nums[right] == target ? right : -1;
堆排序(Heapsort)是指利用堆这种数据结构所设计的一种排序算法。堆积是一个近似完全二叉树的结构,并同时满足堆积的性质:即子结点的键值或索引总是小于(或者大于)它的父节点。堆排序可以说是一种利用堆的概念来排序的选择排序。分为两种方法:
堆排序的平均时间复杂度为 Ο(nlogn)。
In abraction, solving a backtracking problem is acutally iterating a decision tree. Every node of a tree has a valid answer and we traversing the tree to collect all the valid answers. 抽象地说,解决一个回溯问题,实际上就是遍历一棵决策树的过程,树的每个叶子节点存放着一个合法答案。你把整棵树遍历一遍,把叶子节点上的答案都收集起来,就能得到所有的合法答案。
This article is concluded from this article in Chinese.
Cheatsheet: Steps to solve binary tree questions
There are only two ways to storage the data, the Array and the Linked list. All the other data structures are developed based on them.
Arrays, due to their compact and contiguous storage, allow for random access, enabling quick retrieval of elements by index. Moreover, they are relatively space-efficient. However, because of their contiguous storage, memory space must be allocated all at once. This means that if you want to expand an array, you need to reallocate a larger block of memory and copy all the data to it, resulting in a time complexity of O(N). Additionally, if you want to insert or delete elements in the middle of an array, you must shift all the data behind it to maintain continuity, also resulting in a time complexity of O(N).
This blog records the solutions I made for the Dynamic Programming questions from Leetcode.
Firstly, find out the transformation function for the problem.
Then, from the starting point to calculate the results by iterating the array.