Skip to content

Commit 352743c

Browse files
committed
feat: leetcode contest 295
1 parent 5f0449f commit 352743c

File tree

4 files changed

+272
-0
lines changed

4 files changed

+272
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} target
4+
* @return {number}
5+
*/
6+
var rearrangeCharacters = function(s, target) {
7+
let cnt = Array(26).fill(0)
8+
let cnt1 = Array(26).fill(0)
9+
for (let i = 0; i < s.length; i++) {
10+
const x = s.charCodeAt(i) - 'a'.charCodeAt(0)
11+
cnt[x]++
12+
}
13+
for (let i = 0; i < target.length; i++) {
14+
const x = target.charCodeAt(i) - 'a'.charCodeAt(0)
15+
cnt1[x]++
16+
}
17+
let ans = 1e10
18+
for (let i = 0; i < 26; i++) {
19+
if (cnt1[i] > 0) ans = Math.min(ans, Math.floor(cnt[i] / cnt1[i]))
20+
}
21+
return ans
22+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {string} sentence
3+
* @param {number} discount
4+
* @return {string}
5+
*/
6+
const discountPrices = function (s, d) {
7+
return s.split(' ').map(x => {
8+
// console.log(x)
9+
if (x[0] === '$' && x.length > 1) {
10+
let ok = true
11+
for (let i = 1; i < x.length; i++) {
12+
// console.log(x[i], x.charCodeAt(i), '0'.charCodeAt(i), '9'.charCodeAt(i))
13+
if ((x.charCodeAt(i) < '0'.charCodeAt(0)) || (x.charCodeAt(i) > '9'.charCodeAt(0))) {
14+
ok = false
15+
break
16+
}
17+
}
18+
// console.log(x, ok)
19+
if (ok) {
20+
return '$' + (parseFloat(x.slice(1)) * (100 - d) / 100).toFixed(2)
21+
}
22+
}
23+
return x
24+
}).join(' ')
25+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
function totalSteps (nums: number[]): number {
2+
interface MyNode {
3+
val: number
4+
next: MyNode | null
5+
prev: MyNode | null
6+
}
7+
const head: MyNode = {
8+
val: nums[0],
9+
next: null,
10+
prev: null
11+
}
12+
let p: MyNode | null = head
13+
for (let i = 1; i < nums.length; i++) {
14+
const node: MyNode = {
15+
val: nums[i],
16+
next: null,
17+
prev: p
18+
}
19+
p.next = node
20+
p = node
21+
}
22+
23+
let toDel: MyNode[] = []
24+
p = head
25+
while (p?.next) {
26+
if (p.val > p.next?.val) {
27+
toDel.push(p.next)
28+
}
29+
p = p.next
30+
}
31+
32+
let ans = 0
33+
while (toDel.length) {
34+
ans++
35+
const check: MyNode[] = []
36+
for (const q of toDel) {
37+
// console.log(ans, q.val)
38+
q.prev!.next = q.next
39+
if (q.next) q.next.prev = q.prev
40+
check.push(q.prev!)
41+
}
42+
toDel = []
43+
for (const q of check) {
44+
if (q.next?.val && q.val > q.next?.val) toDel.push(q.next)
45+
}
46+
}
47+
return ans
48+
};
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/**
2+
* @param {number[][]} grid
3+
* @return {number}
4+
*/
5+
const minimumObstacles = function (g) {
6+
const m = g.length; const n = g[0].length
7+
const dx = [0, 0, 1, -1]; const dy = [1, -1, 0, 0]
8+
const valid = (x, y) => x >= 0 && x < m && y >= 0 && y < n
9+
10+
const q = new Deque([[0, 0, 0]]); const v = {}
11+
while (q.size()) {
12+
const [i, j, cd] = q.shift()
13+
if (v[`${i}_${j}`]) continue
14+
v[`${i}_${j}`] = 1
15+
if (i === m - 1 && j === n - 1) return cd
16+
for (let k = 0; k < 4; k++) {
17+
const x = i + dx[k]; const y = j + dy[k]
18+
if (valid(x, y) && !v[`${x}_${y}`]) {
19+
if (g[x][y] === 0) q.unshift([x, y, cd])
20+
else q.push([x, y, cd + 1])
21+
}
22+
}
23+
}
24+
}
25+
26+
class CircularDeque {
27+
constructor (N) {
28+
this.prev = this.next = null
29+
this.begin = this.end = 0
30+
this.empty = true
31+
this.data = Array(N)
32+
}
33+
34+
isFull () {
35+
return this.end === this.begin && !this.empty
36+
}
37+
38+
isEmpty () {
39+
return this.empty
40+
}
41+
42+
push (val) {
43+
if (this.isFull()) { return false }
44+
this.empty = false
45+
this.data[this.end] = val
46+
this.end = (this.end + 1) % this.data.length
47+
return true
48+
}
49+
50+
front () {
51+
return this.isEmpty() ? void 0 : this.data[this.begin]
52+
}
53+
54+
back () {
55+
return this.isEmpty() ? void 0 : this.data[this.end - 1]
56+
}
57+
58+
pop () {
59+
if (this.isEmpty()) { return void 0 }
60+
const value = this.data[this.end - 1]
61+
this.end = (this.end - 1) % this.data.length
62+
if (this.end < 0) { this.end += this.data.length }
63+
if (this.end === this.begin) { this.empty = true }
64+
return value
65+
}
66+
67+
unshift (val) {
68+
if (this.isFull()) { return false }
69+
this.empty = false
70+
this.begin = (this.begin - 1) % this.data.length
71+
if (this.begin < 0) { this.begin += this.data.length }
72+
this.data[this.begin] = val
73+
return true
74+
}
75+
76+
shift () {
77+
if (this.isEmpty()) { return void 0 }
78+
const value = this.data[this.begin]
79+
this.begin = (this.begin + 1) % this.data.length
80+
if (this.end === this.begin) { this.empty = true }
81+
return value
82+
}
83+
84+
* values () {
85+
if (this.isEmpty()) { return void 0 }
86+
let i = this.begin
87+
do {
88+
yield this.data[i]
89+
i = (i + 1) % this.data.length
90+
} while (i !== this.end)
91+
}
92+
}
93+
class Deque {
94+
constructor (collection = []) {
95+
this.head = new CircularDeque(128)
96+
this.tail = new CircularDeque(128)
97+
this.tail.empty = this.head.empty = false
98+
this.tail.prev = this.head
99+
this.head.next = this.tail
100+
this._size = 0
101+
for (const item of collection) { this.push(item) }
102+
}
103+
104+
size () {
105+
return this._size
106+
}
107+
108+
push (val) {
109+
let last = this.tail.prev
110+
if (last.isFull()) {
111+
const inserted = new CircularDeque(128)
112+
this.tail.prev = inserted
113+
inserted.next = this.tail
114+
last.next = inserted
115+
inserted.prev = last
116+
last = inserted
117+
}
118+
last.push(val)
119+
this._size++
120+
}
121+
122+
back () {
123+
if (this._size === 0) { return }
124+
return this.tail.prev.back()
125+
}
126+
127+
pop () {
128+
if (this.head.next === this.tail) { return void 0 }
129+
const last = this.tail.prev
130+
const value = last.pop()
131+
if (last.isEmpty()) {
132+
this.tail.prev = last.prev
133+
last.prev.next = this.tail
134+
}
135+
this._size--
136+
return value
137+
}
138+
139+
unshift (val) {
140+
let first = this.head.next
141+
if (first.isFull()) {
142+
const inserted = new CircularDeque(128)
143+
this.head.next = inserted
144+
inserted.prev = this.head
145+
inserted.next = first
146+
first.prev = inserted
147+
first = inserted
148+
}
149+
first.unshift(val)
150+
this._size++
151+
}
152+
153+
shift () {
154+
if (this.head.next === this.tail) { return void 0 }
155+
const first = this.head.next
156+
const value = first.shift()
157+
if (first.isEmpty()) {
158+
this.head.next = first.next
159+
first.next.prev = this.head
160+
}
161+
this._size--
162+
return value
163+
}
164+
165+
front () {
166+
if (this._size === 0) { return void 0 }
167+
return this.head.next.front()
168+
}
169+
170+
* values () {
171+
let node = this.head.next
172+
while (node !== this.tail) {
173+
for (const value of node.values()) { yield value }
174+
node = node.next
175+
}
176+
}
177+
}

0 commit comments

Comments
 (0)