小编J. *_*Doe的帖子

使用单调堆栈背后的直觉

我正在解决LeetCode.com上的一个问题:

给定一个整数数组 A,找到 min(B) 的总和,其中 B 的范围是 A 的每个(连续)子数组。由于答案可能很大,返回模 10^9 + 7 的答案。

输入:[3, 1,2,4]
输出:17
解释:子数组是 [3], [1], [2], [4], [3,1], [1,2], [2,4], [3, 1,2]、[1,2,4]、[3,1,2,4]。最小值为 3、1、2、4、1、1、2、1、1、1。总和为 17。

一个高度赞成的解决方案如下:

class Solution {
public:
  int sumSubarrayMins(vector<int>& A) {
    stack<pair<int, int>> in_stk_p, in_stk_n;
    // left is for the distance to previous less element
    // right is for the distance to next less element
    vector<int> left(A.size()), right(A.size());

    //initialize
    for(int i = 0; i < A.size(); i++) …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm stack

11
推荐指数
1
解决办法
6864
查看次数

Official badge for GitHub actions

Does GitHub have an official 'badge' for their new 'actions' feature?

I came across this request on their official repo and there seems to be an official one:

https://github.com/{github_id}/{repository}/workflows/{workflow_name}/badge.svg
Run Code Online (Sandbox Code Playgroud)

as per this comment, but I am unable to get it to work. Is it actually working? When I use it, I get the below output:

未渲​​染的GitHub徽章

Note that I have replaced {github_id} with my username, {repository} with my repo name and {workflow_name} with the corresponding workflow name (removing the …

github badge github-actions

7
推荐指数
4
解决办法
649
查看次数

在 O(n) 时间内对列表中的数字方块进行排序?

给定一个按排序顺序排列的整数列表,例如[-9, -2, 0, 2, 3],我们必须对每个元素求平方并按排序顺序返回结果。因此,输出将是:[0, 4, 4, 9, 81]

我可以找出两种方法:

  1. O(NlogN)方法 - 我们将每个元素的平方插入哈希集中。然后将元素复制到列表中,排序然后返回。

  2. O(n)方法 - 如果输入元素有界限(例如 -100 到 -100),那么我们创建一个大小为 20000 的布尔列表(用于存储 -10000 到 10000)。对于每个输入元素,我们将相应的平方数标记为 true。例如,对于9输入中,我将81在布尔数组中标记为 true。然后遍历这个布尔列表并将所有 true 元素插入到返回列表中。请注意,在此我们做出一个假设 - 输入元素有一个界限。

有没有什么方法可以让我们及时做到这一点,O(n)即使不假设输入有任何界限

algorithm time-complexity

5
推荐指数
1
解决办法
5897
查看次数

使用回溯(而不是 DFS)背后的直觉

我正在 LeetCode.com 上解决单词搜索问题:

给定一个 2D 板和一个单词,查找该单词是否存在于网格中。

该单词可以由顺序相邻单元格的字母构成,其中“相邻”单元格是水平或垂直相邻的单元格。同一字母单元不得使用多次。

我根据网上帮助写的解决方案如下:

class Solution {
public:
    
    //compare this with Max Area of Island:
    //they 'look' similar, but this one uses a backtracking approach since we retract when we don't find a solution
    //In case of Max Area Of Island, we are not 'coming back' - technically we do come back due to recursion, but we don't track     
    //that since we don't acutally intend to do anything - we are just counting the …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm backtracking depth-first-search

4
推荐指数
1
解决办法
1710
查看次数

在O(1)中构造二叉树?

我的朋友在接受采访时被问到这个问题:

生成一个有限但任意大的二叉树O(1).该方法generate()应返回一个二进制树,其大小无限但有限.

在采访之后我们都对它进行了很长时间的考虑,但我们最多只能提出O(n)解决方案.

我们将如何产生O(1)?它甚至可能吗?还有更多的东西吗?

algorithm binary-tree time-complexity

4
推荐指数
1
解决办法
1091
查看次数

我应该增加还是减少反向迭代器?

如此处所示,向后迭代列表的一个好方法是使用rbegin(),如下所示:

list<DVFGfxObj*>::reverse_iterator iter = m_Objs.rbegin();
for( ; iter != m_Objs.rend(); ++iter) {
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,我不记得是否要++iter--iter。因为我们正在倒退,所以使用--iter,对我来说似乎也是合乎逻辑的。

我正在寻求一个直观的解释,以便我能够永远记住它。我不想每次都查一下。

c++ algorithm reverse-iterator

4
推荐指数
1
解决办法
853
查看次数