Skip to content

Commit e37dce1

Browse files
committed
feat: ^_^
Signed-off-by: Luozhou (Siyuan Cao) <luozhou.csy@alibaba-inc.com>
1 parent 39f0885 commit e37dce1

21 files changed

+1129
-1
lines changed

152.乘积最大子数组.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* @lc app=leetcode.cn id=152 lang=typescript
3+
*
4+
* [152] 乘积最大子数组
5+
*
6+
* https://leetcode-cn.com/problems/maximum-product-subarray/description/
7+
*
8+
* algorithms
9+
* Medium (42.32%)
10+
* Likes: 1453
11+
* Dislikes: 0
12+
* Total Accepted: 212.5K
13+
* Total Submissions: 502.3K
14+
* Testcase Example: '[2,3,-2,4]'
15+
*
16+
* 给你一个整数数组 nums ,请你找出数组中乘积最大的连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。
17+
*
18+
*
19+
*
20+
* 示例 1:
21+
*
22+
* 输入: [2,3,-2,4]
23+
* 输出: 6
24+
* 解释: 子数组 [2,3] 有最大乘积 6。
25+
*
26+
*
27+
* 示例 2:
28+
*
29+
* 输入: [-2,0,-1]
30+
* 输出: 0
31+
* 解释: 结果不能为 2, 因为 [-2,-1] 不是子数组。
32+
*
33+
*/
34+
35+
export
36+
// @lc code=start
37+
function maxProduct(nums: number[]): number {
38+
let prevMax = 1
39+
let prevMin = 1
40+
let result = -Infinity
41+
42+
for (const n of nums) {
43+
const candidates = [n, prevMax * n, prevMin * n]
44+
prevMax = Math.max(...candidates)
45+
prevMin = Math.min(...candidates)
46+
result = Math.max(result, prevMax)
47+
}
48+
49+
return result
50+
}
51+
// @lc code=end

171.excel-表列序号.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* @lc app=leetcode.cn id=171 lang=typescript
3+
*
4+
* [171] Excel 表列序号
5+
*
6+
* https://leetcode-cn.com/problems/excel-sheet-column-number/description/
7+
*
8+
* algorithms
9+
* Easy (71.73%)
10+
* Likes: 301
11+
* Dislikes: 0
12+
* Total Accepted: 113.8K
13+
* Total Submissions: 158.6K
14+
* Testcase Example: '"A"'
15+
*
16+
* 给你一个字符串 columnTitle ,表示 Excel 表格中的列名称。返回该列名称对应的列序号。
17+
*
18+
*
19+
*
20+
* 例如,
21+
*
22+
*
23+
* ⁠ A -> 1
24+
* ⁠ B -> 2
25+
* ⁠ C -> 3
26+
* ⁠ ...
27+
* ⁠ Z -> 26
28+
* ⁠ AA -> 27
29+
* ⁠ AB -> 28
30+
* ⁠ ...
31+
*
32+
*
33+
*
34+
*
35+
* 示例 1:
36+
*
37+
*
38+
* 输入: columnTitle = "A"
39+
* 输出: 1
40+
*
41+
*
42+
* 示例 2:
43+
*
44+
*
45+
* 输入: columnTitle = "AB"
46+
* 输出: 28
47+
*
48+
*
49+
* 示例 3:
50+
*
51+
*
52+
* 输入: columnTitle = "ZY"
53+
* 输出: 701
54+
*
55+
* 示例 4:
56+
*
57+
*
58+
* 输入: columnTitle = "FXSHRXW"
59+
* 输出: 2147483647
60+
*
61+
*
62+
*
63+
*
64+
* 提示:
65+
*
66+
*
67+
* 1 <= columnTitle.length <= 7
68+
* columnTitle 仅由大写英文组成
69+
* columnTitle 在范围 ["A", "FXSHRXW"] 内
70+
*
71+
*
72+
*/
73+
74+
export
75+
// @lc code=start
76+
function titleToNumber(columnTitle: string): number {
77+
let result = 0
78+
for (const c of columnTitle) {
79+
result *= 26
80+
result += char2Number(c)
81+
}
82+
return result
83+
}
84+
85+
function char2Number(char: string) {
86+
return char.charCodeAt(0) - 'A'.charCodeAt(0) + 1
87+
}
88+
// @lc code=end

1822.数组元素积的符号.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* @lc app=leetcode.cn id=1822 lang=typescript
3+
*
4+
* [1822] 数组元素积的符号
5+
*
6+
* https://leetcode-cn.com/problems/sign-of-the-product-of-an-array/description/
7+
*
8+
* algorithms
9+
* Easy (80.11%)
10+
* Likes: 12
11+
* Dislikes: 0
12+
* Total Accepted: 21.6K
13+
* Total Submissions: 27K
14+
* Testcase Example: '[-1,-2,-3,-4,3,2,1]'
15+
*
16+
* 已知函数 signFunc(x) 将会根据 x 的正负返回特定值:
17+
*
18+
*
19+
* 如果 x 是正数,返回 1 。
20+
* 如果 x 是负数,返回 -1 。
21+
* 如果 x 是等于 0 ,返回 0 。
22+
*
23+
*
24+
* 给你一个整数数组 nums 。令 product 为数组 nums 中所有元素值的乘积。
25+
*
26+
* 返回 signFunc(product) 。
27+
*
28+
*
29+
*
30+
* 示例 1:
31+
*
32+
*
33+
* 输入:nums = [-1,-2,-3,-4,3,2,1]
34+
* 输出:1
35+
* 解释:数组中所有值的乘积是 144 ,且 signFunc(144) = 1
36+
*
37+
*
38+
* 示例 2:
39+
*
40+
*
41+
* 输入:nums = [1,5,0,2,-3]
42+
* 输出:0
43+
* 解释:数组中所有值的乘积是 0 ,且 signFunc(0) = 0
44+
*
45+
*
46+
* 示例 3:
47+
*
48+
*
49+
* 输入:nums = [-1,1,-1,1,-1]
50+
* 输出:-1
51+
* 解释:数组中所有值的乘积是 -1 ,且 signFunc(-1) = -1
52+
*
53+
*
54+
*
55+
*
56+
* 提示:
57+
*
58+
*
59+
* 1
60+
* -100
61+
*
62+
*
63+
*/
64+
65+
export
66+
// @lc code=start
67+
function arraySign(nums: number[]): number {
68+
let result = 1
69+
for (const n of nums) {
70+
if (n === 0) {
71+
return 0
72+
}
73+
if (n < 0) {
74+
result *= -1
75+
}
76+
}
77+
78+
return result
79+
}
80+
// @lc code=end

207.课程表.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* @lc app=leetcode.cn id=207 lang=typescript
3+
*
4+
* [207] 课程表
5+
*
6+
* https://leetcode-cn.com/problems/course-schedule/description/
7+
*
8+
* algorithms
9+
* Medium (54.07%)
10+
* Likes: 1089
11+
* Dislikes: 0
12+
* Total Accepted: 164.8K
13+
* Total Submissions: 304.8K
14+
* Testcase Example: '2\n[[1,0]]'
15+
*
16+
* 你这个学期必须选修 numCourses 门课程,记为 0 到 numCourses - 1 。
17+
*
18+
* 在选修某些课程之前需要一些先修课程。 先修课程按数组 prerequisites 给出,其中 prerequisites[i] = [ai, bi]
19+
* ,表示如果要学习课程 ai 则 必须 先学习课程  bi 。
20+
*
21+
*
22+
* 例如,先修课程对 [0, 1] 表示:想要学习课程 0 ,你需要先完成课程 1 。
23+
*
24+
*
25+
* 请你判断是否可能完成所有课程的学习?如果可以,返回 true ;否则,返回 false 。
26+
*
27+
*
28+
*
29+
* 示例 1:
30+
*
31+
*
32+
* 输入:numCourses = 2, prerequisites = [[1,0]]
33+
* 输出:true
34+
* 解释:总共有 2 门课程。学习课程 1 之前,你需要完成课程 0 。这是可能的。
35+
*
36+
* 示例 2:
37+
*
38+
*
39+
* 输入:numCourses = 2, prerequisites = [[1,0],[0,1]]
40+
* 输出:false
41+
* 解释:总共有 2 门课程。学习课程 1 之前,你需要先完成​课程 0 ;并且学习课程 0 之前,你还应先完成课程 1 。这是不可能的。
42+
*
43+
*
44+
*
45+
* 提示:
46+
*
47+
*
48+
* 1
49+
* 0
50+
* prerequisites[i].length == 2
51+
* 0 i, bi < numCourses
52+
* prerequisites[i] 中的所有课程对 互不相同
53+
*
54+
*
55+
*/
56+
57+
export
58+
// @lc code=start
59+
function canFinish(numCourses: number, prerequisites: number[][]): boolean {
60+
const children: Map<number, number[]> = new Map()
61+
const inDegrees: Record<number, number> = {}
62+
for (const [child, parent] of prerequisites) {
63+
children.set(parent, children.get(parent) || [])
64+
children.get(parent)!.push(child)
65+
inDegrees[child] = 1 + (inDegrees[child] || 0)
66+
}
67+
68+
const queue: number[] = []
69+
for (let i = 0; i < numCourses; i++) {
70+
if (!inDegrees[i]) {
71+
queue.push(i)
72+
}
73+
}
74+
75+
let learntCount = 0
76+
while (queue.length) {
77+
const currCourse = queue.shift()!
78+
learntCount += 1
79+
80+
for (const child of children.get(currCourse) || []) {
81+
inDegrees[child] -= 1
82+
if (inDegrees[child] === 0) queue.push(child)
83+
}
84+
}
85+
86+
return learntCount === numCourses
87+
}
88+
// @lc code=end

0 commit comments

Comments
 (0)