Skip to content

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

B1IidL/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import peakIndexInMountainArray from "../peak-index-in-a-mountain-array/index.ts";
2+
3+
export default peakIndexInMountainArray;

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ Step 2. Add the dependency
4545

4646
<summary>展开查看</summary>
4747

48+
https://leetcode.cn/problems/B1IidL
49+
50+
https://leetcode.cn/problems/number-of-subarrays-with-lcm-equal-to-k
51+
52+
https://leetcode.cn/problems/peak-index-in-a-mountain-array
53+
4854
https://leetcode.cn/problems/convert-the-temperature
4955

5056
https://leetcode.cn/problems/split-message-based-on-limit
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export default function subarrayLCM(nums: number[], k: number): number {
2+
let cnt = 0;
3+
const len = nums.length;
4+
5+
for (let i = 0; i < len; i++) {
6+
if (k % nums[i]) continue;
7+
let lcm = nums[i];
8+
for (let j = i; j < len; j++) {
9+
if (k % nums[j]) break;
10+
lcm = getLCM(lcm, nums[j]);
11+
12+
if (lcm === k) cnt += 1;
13+
}
14+
}
15+
16+
return cnt;
17+
}
18+
19+
function getGCD<T extends number | bigint>(a: T, b: T): T {
20+
if (b == 0) return a;
21+
return getGCD(b, (a % b) as T) as T;
22+
}
23+
24+
function getLCM<T extends number | bigint>(a: T, b: T): T {
25+
return ((a * b) / getGCD(a, b)) as T;
26+
}
27+
export { getGCD, getLCM };
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export default function peakIndexInMountainArray(arr: number[]): number {
2+
let left = 0;
3+
let right = arr.length - 1;
4+
5+
// 可以看做寻找 arr[i + 1] 小于 arr[i] 区间内的左边界,也就是第一个 arr[i + 1] 小于 arr[i] 的索引
6+
while (left <= right) {
7+
const mid = left + ((right - left) >> 1);
8+
9+
if (arr[mid] === arr[mid + 1]) {
10+
right = mid - 1;
11+
} else if (arr[mid] < arr[mid + 1]) {
12+
left = mid + 1;
13+
} else if (arr[mid] > arr[mid + 1]) {
14+
right = mid - 1;
15+
}
16+
}
17+
18+
// 题目数据保证 arr 是一个山脉数组,直接返回 left
19+
return left;
20+
}

0 commit comments

Comments
 (0)