Skip to content

Commit 6b25fc9

Browse files
committed
feat: problems for the next week
1 parent e41bef5 commit 6b25fc9

7 files changed

+188
-11
lines changed

.eslintrc.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,7 @@ module.exports = {
66
project: './tsconfig.json'
77
},
88
rules: {
9-
'@typescript-eslint/naming-convention': [
10-
'error',
11-
{
12-
selector: 'interface',
13-
format: ['PascalCase'],
14-
custom: {
15-
regex: '^I[A-Z]',
16-
match: true
17-
}
18-
}
19-
],
9+
'@typescript-eslint/naming-convention': 0,
2010
'@typescript-eslint/explicit-function-return-type': 0,
2111
'@typescript-eslint/strict-boolean-expressions': 0,
2212
'no-console': 0,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class SnapshotArray {
2+
// snap[i]: list of [snap_id, snap_value]
3+
_snap: Array<Array<[number, number]>>
4+
a: number[]
5+
cnt = 0
6+
changed: Set<number>
7+
constructor (length: number) {
8+
this._snap = Array(length).fill(0).map(() => [])
9+
this.a = Array(length).fill(0)
10+
this.changed = new Set(Array(length).fill(0).map((_, idx) => idx))
11+
}
12+
13+
set (index: number, val: number): void {
14+
this.a[index] = val
15+
this.changed.add(index)
16+
}
17+
18+
snap (): number {
19+
for (const idx of this.changed) {
20+
this._snap[idx].push([this.cnt, this.a[idx]])
21+
}
22+
this.changed.clear()
23+
return this.cnt++
24+
}
25+
26+
get (index: number, snap_id: number): number {
27+
const n = this._snap[index].length
28+
let l = 0; let r = n - 1
29+
while (l < r) {
30+
const mid = l + r + 1 >> 1
31+
if (this._snap[index][mid][0] <= snap_id) l = mid
32+
else r = mid - 1
33+
}
34+
return this._snap[index][l][1]
35+
}
36+
}
37+
38+
/**
39+
* Your SnapshotArray object will be instantiated and called as such:
40+
* var obj = new SnapshotArray(length)
41+
* obj.set(index,val)
42+
* var param_2 = obj.snap()
43+
* var param_3 = obj.get(index,snap_id)
44+
*/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function maximumSum (arr: number[]): number {
2+
const n = arr.length
3+
// f[i][0]: not use deletion, f[i][1]: has used deletion
4+
const f: number[][] = Array(n + 1).fill(0).map(() => [])
5+
f[0][0] = f[0][1] = 0
6+
let ans = -1e10
7+
for (let i = 1; i <= n; i++) {
8+
f[i][0] = Math.max(f[i - 1][0], 0) + arr[i - 1]
9+
f[i][1] = Math.max(f[i - 1][1], f[i - 2]?.[0] ?? 0, 0) + arr[i - 1]
10+
ans = Math.max(ans, f[i][0], f[i][1])
11+
}
12+
return ans
13+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function maxProduct (nums: number[]): number {
2+
const n = nums.length
3+
// f[i][0]: max, f[i][1]: min; both ends with nums[i], as neg multiply neg can be pos
4+
const f: number[][] = Array(n + 1).fill(0).map(() => [])
5+
f[0][0] = f[0][1] = 1
6+
let ans = -1e10
7+
for (let i = 1; i <= n; i++) {
8+
f[i][0] = Math.max(f[i - 1][0] * nums[i - 1], f[i - 1][1] * nums[i - 1], nums[i - 1])
9+
f[i][1] = Math.min(f[i - 1][0] * nums[i - 1], f[i - 1][1] * nums[i - 1], nums[i - 1])
10+
ans = Math.max(ans, f[i][0])
11+
}
12+
return ans
13+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function largestVariance (s: string): number {
2+
const n = s.length
3+
// 以 i 结尾的子数组的最大和
4+
const f: number[] = Array(n + 1).fill(0)
5+
// 以 i 结尾的并且一定包含 -1 的子数组的最大和
6+
const g: number[] = Array(n + 1).fill(0)
7+
let ans = 0
8+
for (let i = 0; i < 26; i++) {
9+
for (let j = 0; j < 26; j++) {
10+
if (j === i) continue
11+
12+
f[0] = 0
13+
g[0] = -1e10
14+
for (let k = 1; k <= n; k++) {
15+
const ch = s.charCodeAt(k - 1) - 'a'.charCodeAt(0)
16+
if (ch === i) {
17+
f[k] = Math.max(f[k - 1], 0) + 1
18+
g[k] = g[k - 1] + 1
19+
}
20+
else if (ch === j) {
21+
f[k] = Math.max(f[k - 1], 0) - 1
22+
g[k] = Math.max(g[k - 1], f[k - 1], 0) - 1
23+
}
24+
else {
25+
f[k] = f[k - 1]
26+
g[k] = g[k - 1]
27+
}
28+
29+
ans = Math.max(ans, g[k])
30+
}
31+
}
32+
}
33+
return ans
34+
};
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
export interface MyNode {
2+
val: string
3+
prev: MyNode | null
4+
next: MyNode | null
5+
}
6+
7+
class TextEditor {
8+
cursor: MyNode
9+
constructor () {
10+
this.cursor = {
11+
val: '#',
12+
prev: null,
13+
next: null
14+
}
15+
}
16+
17+
addText (text: string): void {
18+
for (const ch of text) {
19+
const newNode = {
20+
val: ch,
21+
prev: this.cursor,
22+
next: this.cursor.next
23+
}
24+
if (this.cursor.next) this.cursor.next.prev = newNode
25+
this.cursor.next = newNode
26+
this.cursor = this.cursor.next
27+
}
28+
}
29+
30+
deleteText (k: number): number {
31+
let cnt = 0
32+
while (k-- && this.cursor.prev) {
33+
this.cursor.prev.next = this.cursor.next
34+
if (this.cursor.next) this.cursor.next.prev = this.cursor.prev
35+
this.cursor = this.cursor.prev
36+
cnt++
37+
}
38+
return cnt
39+
}
40+
41+
getLast10 (): string {
42+
let p: MyNode | null = this.cursor; let cnt = 10
43+
const a = []
44+
while (cnt-- && p && p.val !== '#') {
45+
a.push(p.val)
46+
p = p.prev
47+
}
48+
return a.reverse().join('')
49+
}
50+
51+
cursorLeft (k: number): string {
52+
while (k-- && this.cursor.prev) {
53+
this.cursor = this.cursor.prev
54+
}
55+
return this.getLast10()
56+
}
57+
58+
cursorRight (k: number): string {
59+
while (k-- && this.cursor.next) {
60+
this.cursor = this.cursor.next
61+
}
62+
return this.getLast10()
63+
}
64+
}
65+
66+
/**
67+
* Your TextEditor object will be instantiated and called as such:
68+
* var obj = new TextEditor()
69+
* obj.addText(text)
70+
* var param_2 = obj.deleteText(k)
71+
* var param_3 = obj.cursorLeft(k)
72+
* var param_4 = obj.cursorRight(k)
73+
*/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function maxSubArray (nums: number[]): number {
2+
const n = nums.length
3+
const f = Array(n + 1).fill(0)
4+
let ans = -1e10
5+
for (let i = 1; i <= n; i++) {
6+
f[i] = Math.max(f[i - 1], 0) + nums[i - 1]
7+
ans = Math.max(ans, f[i])
8+
}
9+
return ans
10+
};

0 commit comments

Comments
 (0)