钉钉
最近钉钉的新闻很多,源于新回归的 CEO 陈航(花名"无招")。
无招是钉钉创立初期的元老人物,但在 2021 年因为战略分歧问题,离开了阿里,并(带走了部分核心团队成员)进行创业。
在今年 3 月,阿里宣布收购无招的创业公司,于是无招正式回归阿里集团,并在今年 4 月,接任了钉钉 CEO 职位。
这位 CEO 的管理风格,一直以"硬核奋斗"文化著称。
在钉钉创业期团队曾穿 "BE CRAZY" T恤,日工作超 15 小时是常态。
以至于后来拼多多招聘,也特别倾向于挖角钉钉员工,因其"适应高强度工作"。
这位 CEO 回归之后,传出来了不少和"加班"相关的新闻:
-
8 月中旬,多名自称钉钉员工的网友爆料,在凌晨 12 点至 12 点半期间巡查办公区,发现工位空置率高,次日,他召集所有部门批评,质问"为何提前下班"
被官方辟谣。
-
8 月 20 日,博主爆料称钉钉计划优化多位高管(15 名 P8 + 4 名 P10),因他们"不主张加班",三个月内将被边缘化或裁员
再次被官方辟谣。
-
8 月 21 日凌晨 00:03,钉钉公众号发布推文《加班,做了个让大家能早点回家的东西》,宣布十周年新品"蕨",并多次强调"加班"关键词
然后,评论区炸了:
不管是小编想通过深夜发文,对外表达钉钉作息的真实情况,还是定时发送,蹭一波最近谣言的热度,自己玩自己公司的梗。
这波营销,我都给满分。
再多的评价,我就不好说了,欢迎大家评论区交流。
...
回归主题。
来一道和「阿里巴巴」相关的算法题。
题目描述
平台:LeetCode
题号:946
给定 pushed
和 popped
两个序列,每个序列中的值都不重复,只有当它们可能是在最初空栈上进行的推入 push
和弹出 pop
操作序列的结果时,返回 true
;否则,返回 false
。
示例 1:
scss
输入:pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
输出:true
解释:我们可以按以下顺序执行:
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
示例 2:
css
输入:pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
输出:false
解释:1 不能在 2 之前弹出。
提示:
- <math xmlns="http://www.w3.org/1998/Math/MathML"> 1 < = p u s h e d . l e n g t h < = 1000 1 <= pushed.length <= 1000 </math>1<=pushed.length<=1000
- <math xmlns="http://www.w3.org/1998/Math/MathML"> 0 < = p u s h e d [ i ] < = 1000 0 <= pushed[i] <= 1000 </math>0<=pushed[i]<=1000
pushed
的所有元素互不相同- <math xmlns="http://www.w3.org/1998/Math/MathML"> p o p p e d . l e n g t h = p u s h e d . l e n g t h popped.length = pushed.length </math>popped.length=pushed.length
popped
是pushed
的一个排列
栈
根据题意,利用元素各不相同,我们使用一个栈来处理 pushed
数组,每次将 <math xmlns="http://www.w3.org/1998/Math/MathML"> p u s h e d [ i ] pushed[i] </math>pushed[i] 放入栈中,然后比较当前栈顶元素是否与待弹出元素相同(使用变量 j
来代指当前待弹出元素下标),若相等则弹栈并进行 j
自增,当所有的元素处理完后,栈为空说明栈序列合法。
Java 代码:
Java
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
Deque<Integer> d = new ArrayDeque<>();
for (int i = 0, j = 0; i < pushed.length; i++) {
d.addLast(pushed[i]);
while (!d.isEmpty() && d.peekLast() == popped[j] && ++j >= 0) d.pollLast();
}
return d.isEmpty();
}
}
C++ 代码:
C++
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
deque<int> d;
for (int i = 0, j = 0; i < pushed.size(); i++) {
d.push_back(pushed[i]);
while (!d.empty() && d.back() == popped[j] && ++j >= 0) d.pop_back();
}
return d.empty();
}
};
Python 代码:
Python
class Solution:
def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
d = deque()
j = 0
for i in range(len(pushed)):
d.append(pushed[i])
while d and d[-1] == popped[j]:
d.pop()
j += 1
return not d
Typescript 代码:
Typescript
function validateStackSequences(pushed: number[], popped: number[]): boolean {
let n = pushed.length, he = 0, ta = 0
const stk: number[] = new Array<number>(n).fill(0)
for (let i = 0, j = 0; i < n; i++) {
stk[ta++] = pushed[i]
while (he < ta && stk[ta - 1] == popped[j] && ++j >= 0) ta--
}
return he == ta
};
- 时间复杂度: <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( n ) O(n) </math>O(n)
- 空间复杂度: <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( n ) O(n) </math>O(n)
双指针
我们也可以直接利用 pushed
充当栈,使用变量 idx
代指栈顶下标,变量 j
指向 popped
中待弹出的元素。
该做法好处无须额外空间,坏处是会修改入参数组。
Java 代码:
Java
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
int n = pushed.length, idx = 0;
for (int i = 0, j = 0; i < n; i++) {
pushed[idx++] = pushed[i];
while (idx > 0 && pushed[idx - 1] == popped[j] && ++j >= 0) idx--;
}
return idx == 0;
}
}
C++ 代码:
C++
class Solution {
public:
bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
int n = pushed.size(), idx = 0;
for (int i = 0, j = 0; i < n; i++) {
pushed[idx++] = pushed[i];
while (idx > 0 && pushed[idx - 1] == popped[j] && ++j >= 0) idx--;
}
return idx == 0;
}
};
Python 代码:
TypeScript
class Solution:
def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
n, idx = len(pushed), 0
j = 0
for i in range(n):
pushed[idx] = pushed[i]
idx += 1
while idx > 0 and pushed[idx - 1] == popped[j]:
j += 1
idx -= 1
return idx == 0
TypeScript 代码:
TypeScript
function validateStackSequences(pushed: number[], popped: number[]): boolean {
let n = pushed.length, idx = 0
for (let i = 0, j = 0; i < n; i++) {
pushed[idx++] = pushed[i]
while (idx > 0 && pushed[idx - 1] == popped[j] && ++j >= 0) idx--
}
return idx == 0
};
- 时间复杂度: <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( n ) O(n) </math>O(n)
- 空间复杂度: <math xmlns="http://www.w3.org/1998/Math/MathML"> O ( 1 ) O(1) </math>O(1)