Skip to content

Commit 8db7dfe

Browse files
committed
feat: leetcode contest 302
1 parent da9dcd6 commit 8db7dfe

5 files changed

+86
-2
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
const numberOfPairs = function (nums) {
6+
const cnt = {}
7+
for (const a of nums) {
8+
cnt[a] ??= 0
9+
cnt[a]++
10+
}
11+
let p = 0; let l = 0
12+
for (const [k, v] of Object.entries(cnt)) {
13+
p += Math.floor(v / 2)
14+
l += v % 2
15+
}
16+
return [p, l]
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string[]} nums
3+
* @param {number[][]} queries
4+
* @return {number[]}
5+
*/
6+
const smallestTrimmedNumbers = function (nums, qs) {
7+
const n = nums.length; const m = qs.length
8+
const l = nums[0].length
9+
const idx = Array(n).fill(0).map((_, idx) => idx)
10+
const ans = []
11+
for (const [k, trim] of qs) {
12+
idx.sort((i, j) => {
13+
const s = nums[i].slice(l - trim, l)
14+
const t = nums[j].slice(l - trim, l)
15+
if (s === t) return i - j
16+
return s < t ? -1 : 1
17+
})
18+
ans.push(idx[k - 1])
19+
}
20+
return ans
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number[]} numsDivide
4+
* @return {number}
5+
*/
6+
const minOperations = function (nums, nd) {
7+
nums.sort((a, b) => a - b)
8+
const n = nums.length
9+
10+
const gcd = (a, b) => b ? gcd(b, a % b) : a
11+
let d = nd[0]
12+
for (let i = 1; i < nd.length; i++) d = gcd(d, nd[i])
13+
14+
let mi
15+
for (let i = 0; i < n; i++) {
16+
if (d % nums[i] === 0) {
17+
mi = i
18+
break
19+
}
20+
}
21+
if (mi == null) return -1
22+
return mi
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const maximumSum = function (nums) {
6+
const map = {}
7+
for (const x of nums) {
8+
let s = 0; let y = x
9+
while (y) {
10+
s += y % 10
11+
y = Math.floor(y / 10)
12+
}
13+
map[s] ??= []
14+
map[s].push(x)
15+
}
16+
let ans = -1
17+
for (const [k, v] of Object.entries(map)) {
18+
if (v.length < 2) continue
19+
v.sort((a, b) => (b - a))
20+
ans = Math.max(ans, v[0] + v[1])
21+
}
22+
return ans
23+
}

leetcode/残酷刷题/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
- [DP](#dp)
1212
- [二分图](#二分图)
1313
- [状态压缩](#状态压缩)
14-
- [LIS](#lis)
14+
- [LIS (最长上升子序列)](#lis-最长上升子序列)
1515
- [背包](#背包)
1616
- [subsequence 计数](#subsequence-计数)
1717
- [二分查找](#二分查找)
@@ -123,7 +123,7 @@
123123
- 1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix
124124
- 1452. People Whose List of Favorite Companies Is Not a Subset of Another List
125125

126-
#### LIS
126+
#### LIS (最长上升子序列)
127127

128128
- 300. Longest Increasing Subsequence
129129
- 1964. Find the Longest Valid Obstacle Course at Each Position

0 commit comments

Comments
 (0)