Skip to content

Commit 505e27b

Browse files
committed
https://leetcode.cn/problems/number-of-subarrays-with-gcd-equal-to-k/
1 parent ba8a30e commit 505e27b

File tree

8 files changed

+53
-20
lines changed

8 files changed

+53
-20
lines changed

README.md

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

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

48+
https://leetcode.cn/problems/number-of-subarrays-with-gcd-equal-to-k/
49+
4850
https://leetcode.cn/problems/smallest-subarrays-with-maximum-bitwise-or/
4951

5052
https://leetcode.cn/problems/determine-if-two-events-have-conflict/

fraction-addition-and-subtraction/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { greatestCommonDivisor } from "../max-points-on-a-line/greatest_common_divisor.ts";
1+
import { gcd } from "../max-points-on-a-line/greatest_common_divisor.ts";
22
import { deduplication } from "./deduplication.ts";
33
import { Fraction } from "./Fraction.ts";
44
import { parseFraction } from "./parseFraction.ts";
@@ -18,18 +18,16 @@ export function fractionAdd(fractions: Fraction[]): Fraction {
1818
}
1919
export function simplifyFraction(fraction: Fraction) {
2020
const { molecular, denominator } = fraction;
21-
const gcd = greatestCommonDivisor(molecular, denominator);
21+
const g = gcd(molecular, denominator);
2222
return new Fraction({
2323
sign: fraction.sign,
24-
denominator: fraction.denominator / gcd,
25-
molecular: fraction.molecular / gcd,
24+
denominator: fraction.denominator / g,
25+
molecular: fraction.molecular / g,
2626
});
2727
}
2828
function fractionAddition(expression: string): string {
2929
const fractions = parseFraction(expression);
3030

31-
return FractionToString(
32-
simplifyFraction(fractionAdd(fractions)),
33-
);
31+
return FractionToString(simplifyFraction(fractionAdd(fractions)));
3432
}
3533
export default fractionAddition;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export function greatestCommonDivisor(a: number, b: number): number {
2-
return b != 0 ? greatestCommonDivisor(b, a % b) : a;
1+
export function gcd(a: number, b: number): number {
2+
return b != 0 ? gcd(b, a % b) : a;
33
}

mod.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { PrefixTreeSearchEach } from "./longest-word-in-dictionary/PrefixTreeSea
3333
import { PrefixTreeSearchPrefixEach } from "./longest-word-in-dictionary/PrefixTreeSearchPrefixEach.ts";
3434
import { PrefixTreeWithSum } from "./map-sum-pairs/PrefixTreeWithSum.ts";
3535
import { calculateStraightLineEquation } from "./max-points-on-a-line/calculateStraightLineEquation.ts";
36-
import { greatestCommonDivisor } from "./max-points-on-a-line/greatest_common_divisor.ts";
36+
import { gcd } from "./max-points-on-a-line/greatest_common_divisor.ts";
3737
import { uniqueStraightLineEquation } from "./max-points-on-a-line/uniqueStraightLineEquation.ts";
3838
import { bigintMax } from "./maximum-width-of-binary-tree/bigint_max.ts";
3939
import { bigintMin } from "./maximum-width-of-binary-tree/bigint_min.ts";
@@ -73,6 +73,7 @@ import { multiply_Mod } from "./super-pow/multiply_Mod.ts";
7373
import { pow_bigint_mod } from "./super-pow/pow_bigint_mod.ts";
7474
import { superPow_mod } from "./super-pow/superPow_mod.ts";
7575
import { traversal_bst_range } from "./tweet-counts-per-frequency/traversal_bst_range.ts";
76+
import { gcd as gcdBigint } from "./ugly-number-iii/index.ts";
7677
import { float64equals } from "./utils/float64equals.ts";
7778
import { PrefixTreeClear } from "./utils/PrefixTreeClear.ts";
7879
import { PrefixTreeForEach } from "./utils/PrefixTreeForEach.ts";
@@ -99,10 +100,11 @@ export { climbing_stairs_bigint } from "./climbing-stairs/climbing_stairs_bigint
99100
export { fibonacci_bigint } from "./fibonacci-number/fibonacci_bigint.ts";
100101
export { PrefixTreeWithSum };
101102
export { PrefixTreeClear };
102-
export { greatestCommonDivisor };
103+
export { gcd };
103104
export { calculateStraightLineEquation, uniqueStraightLineEquation };
104105
export { parseComplex };
105106
export {
107+
gcdBigint,
106108
left_rotate,
107109
TrieNode,
108110
TrieNodeCountWordsEqualTo,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export default function subarrayGCD(nums: number[], k: number): number {
2+
const n = nums.length;
3+
let l = 0,
4+
r = 0,
5+
f = nums[r],
6+
c = 0;
7+
while (r < n) {
8+
const e = gcd(f, nums[r]);
9+
if (e % k != 0) {
10+
r++;
11+
if (r == n) break;
12+
l = r;
13+
f = nums[r];
14+
continue;
15+
} else if (e == k) {
16+
let p = r,
17+
d = nums[r];
18+
while (p >= l) {
19+
const g = gcd(nums[p], d);
20+
if (g == k) {
21+
c += p - l + 1;
22+
break;
23+
}
24+
d = g;
25+
p--;
26+
}
27+
}
28+
f = e;
29+
r++;
30+
}
31+
return c;
32+
}
33+
export function gcd(a: number, b: number): number {
34+
return b != 0 ? gcd(b, a % b) : a;
35+
}

tree-of-coprimes/export.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,3 @@ package index
33
func GetCoprimes(nums []int, edges [][]int) []int {
44
return getCoprimes(nums, edges)
55
}
6-
func GreatestCommonDivisor(a int, b int) int {
7-
8-
return greatestCommonDivisor(a, b)
9-
}

tree-of-coprimes/index.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func getCoprimes(nums []int, edges [][]int) []int {
1919
}
2020
for i := 0; i < 51; i++ {
2121
for j := i; j < 51; j++ {
22-
prime[j][i] = greatestCommonDivisor(i, j) == 1
22+
prime[j][i] = Gcd(i, j) == 1
2323
prime[i][j] = prime[j][i]
2424
}
2525
}
@@ -66,9 +66,9 @@ func getCoprimes(nums []int, edges [][]int) []int {
6666
return results
6767
}
6868

69-
func greatestCommonDivisor(a int, b int) int {
69+
func Gcd(a int, b int) int {
7070
for b != 0 {
71-
return greatestCommonDivisor(b, a%b)
71+
return Gcd(b, a%b)
7272
}
7373
return a
7474
}

tree-of-coprimes/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { greatestCommonDivisor } from "../max-points-on-a-line/greatest_common_divisor.ts";
1+
import { gcd } from "../max-points-on-a-line/greatest_common_divisor.ts";
22

33
export default getCoprimes;
44
function getCoprimes(nums: number[], edges: number[][]): number[] {
@@ -17,7 +17,7 @@ function getCoprimes(nums: number[], edges: number[][]): number[] {
1717

1818
for (const i of Array(51).keys()) {
1919
for (let j = i; j < 51; j++) {
20-
prime[i][j] = prime[j][i] = greatestCommonDivisor(i, j) === 1;
20+
prime[i][j] = prime[j][i] = gcd(i, j) === 1;
2121
}
2222
}
2323
}

0 commit comments

Comments
 (0)