Skip to content

Commit fd0709e

Browse files
committed
feat: leetcode contest 296
1 parent e1571fb commit fd0709e

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var minMaxGame = function(nums) {
6+
if (nums.length <= 1) return nums
7+
let a = []
8+
for (let i = 0; i < nums.length; i+=2) {
9+
if (i / 2 % 2 === 1) {
10+
a.push(Math.max(nums[i], nums[i+1]))
11+
} else {
12+
a.push(Math.min(nums[i], nums[i+1]))
13+
}
14+
}
15+
return minMaxGame(a)
16+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var partitionArray = function(nums, k) {
7+
nums.sort((a, b) => a - b)
8+
const n = nums.length
9+
let ans = 0
10+
for (let i = 0; i < nums.length; ) {
11+
let j = i
12+
while (j < n && nums[j] - nums[i] <= k) j++
13+
i = j, ans++
14+
}
15+
return ans
16+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number[][]} operations
4+
* @return {number[]}
5+
*/
6+
var arrayChange = function(nums, operations) {
7+
const cnt = {}
8+
for (let i = 0; i < nums.length; i++) {
9+
cnt[nums[i]] = [i]
10+
}
11+
console.log(cnt)
12+
for (const [x, y] of operations) {
13+
cnt[y] = cnt[x]
14+
delete cnt[x]
15+
}
16+
// console.log(cnt)
17+
let ans = []
18+
for (const [k, v] of Object.entries(cnt)) {
19+
for (const idx of v) ans[idx] = Number(k)
20+
}
21+
return ans
22+
};
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
const TextEditor = function () {
3+
this.s = ''
4+
this.t = ''
5+
}
6+
7+
/**
8+
* @param {string} text
9+
* @return {void}
10+
*/
11+
TextEditor.prototype.addText = function (text) {
12+
this.s += text
13+
}
14+
15+
/**
16+
* @param {number} k
17+
* @return {number}
18+
*/
19+
TextEditor.prototype.deleteText = function (k) {
20+
const d = Math.min(this.s.length, k)
21+
this.s = this.s.slice(0, this.s.length - d)
22+
return d
23+
}
24+
25+
/**
26+
* @param {number} k
27+
* @return {string}
28+
*/
29+
TextEditor.prototype.cursorLeft = function (k) {
30+
const l = Math.max(0, this.s.length - k)
31+
this.t = this.s.slice(l) + this.t
32+
this.s = this.s.slice(0, l)
33+
return this.s.slice(Math.max(this.s.length - 10, 0))
34+
}
35+
36+
/**
37+
* @param {number} k
38+
* @return {string}
39+
*/
40+
TextEditor.prototype.cursorRight = function (k) {
41+
const l = Math.min(k, this.t.length)
42+
this.s += this.t.slice(0, l)
43+
this.t = this.t.slice(l)
44+
return this.s.slice(Math.max(this.s.length - 10, 0))
45+
}
46+
47+
/**
48+
* Your TextEditor object will be instantiated and called as such:
49+
* var obj = new TextEditor()
50+
* obj.addText(text)
51+
* var param_2 = obj.deleteText(k)
52+
* var param_3 = obj.cursorLeft(k)
53+
* var param_4 = obj.cursorRight(k)
54+
*/

0 commit comments

Comments
 (0)