File tree Expand file tree Collapse file tree 4 files changed +56
-0
lines changed
number-of-subarrays-with-lcm-equal-to-k
peak-index-in-a-mountain-array Expand file tree Collapse file tree 4 files changed +56
-0
lines changed Original file line number Diff line number Diff line change
1
+ import peakIndexInMountainArray from "../peak-index-in-a-mountain-array/index.ts" ;
2
+
3
+ export default peakIndexInMountainArray ;
Original file line number Diff line number Diff line change @@ -45,6 +45,12 @@ Step 2. Add the dependency
45
45
46
46
<summary >展开查看</summary >
47
47
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
+
48
54
https://leetcode.cn/problems/convert-the-temperature
49
55
50
56
https://leetcode.cn/problems/split-message-based-on-limit
Original file line number Diff line number Diff line change
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 } ;
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments