File tree Expand file tree Collapse file tree 4 files changed +113
-2
lines changed
number-of-different-subsequences-gcds Expand file tree Collapse file tree 4 files changed +113
-2
lines changed Original file line number Diff line number Diff line change @@ -49,6 +49,8 @@ Step 2. Add the dependency
49
49
50
50
<summary >展开查看</summary >
51
51
52
+ https://leetcode.cn/problems/number-of-different-subsequences-gcds/
53
+
52
54
https://leetcode-cn.com/problems/rearrange-characters-to-make-target-string/
53
55
54
56
https://leetcode-cn.com/problems/assign-cookies/
Original file line number Diff line number Diff line change @@ -11,7 +11,8 @@ export { default as countBy } from "https://cdn.skypack.dev/lodash@4.17.21/count
11
11
export { default as zip } from "https://cdn.skypack.dev/lodash@4.17.21/zip?dts" ;
12
12
export { default as isEqual } from "https://cdn.skypack.dev/lodash@4.17.21/isEqual?dts" ;
13
13
export { default as uniqBy } from "https://cdn.skypack.dev/lodash@4.17.21/uniqBy?dts" ;
14
-
14
+ export { default as max } from "https://cdn.skypack.dev/lodash@4.17.21/max?dts" ;
15
+ export { default as sum } from "https://cdn.skypack.dev/lodash@4.17.21/sum?dts" ;
15
16
export { default as intersection } from "https://cdn.skypack.dev/lodash@4.17.21/intersection?dts" ;
16
17
import { BinaryHeap } from "https://deno.land/std@0.171.0/collections/binary_heap.ts" ;
17
18
export { BinaryHeap } ;
@@ -26,7 +27,7 @@ export {
26
27
BinarySearchTreeNode ,
27
28
} from "npm:@datastructures-js/binary-search-tree@5.2.0" ;
28
29
export { default as groupBy } from "https://cdn.skypack.dev/lodash@4.17.21/groupBy?dts" ;
29
- export { default as sum } from "https://cdn.skypack.dev/lodash@4.17.21/sum?dts" ;
30
+
30
31
export {
31
32
runScript ,
32
33
TreeNode ,
Original file line number Diff line number Diff line change
1
+ import { max } from "../deps.ts" ;
2
+
3
+ function countDifferentSubsequenceGCDs ( nums : number [ ] ) : number {
4
+ const maxVal = max ( nums ) as number ;
5
+ const occured = new Array < boolean > ( maxVal + 1 ) . fill ( false ) ;
6
+ for ( const num of nums ) {
7
+ occured [ num ] = true ;
8
+ }
9
+ let ans = 0 ;
10
+ for ( let i = 1 ; i <= maxVal ; i ++ ) {
11
+ let subGcd = 0 ;
12
+ for ( let j = i ; j <= maxVal ; j += i ) {
13
+ if ( occured [ j ] ) {
14
+ if ( subGcd === 0 ) {
15
+ subGcd = j ;
16
+ } else {
17
+ subGcd = gcd ( subGcd , j ) ;
18
+ }
19
+ if ( subGcd === i ) {
20
+ ans ++ ;
21
+ break ;
22
+ }
23
+ }
24
+ }
25
+ }
26
+ return ans ;
27
+ }
28
+ export function gcd ( num1 : number , num2 : number ) {
29
+ while ( num2 !== 0 ) {
30
+ const temp = num1 ;
31
+ num1 = num2 ;
32
+ num2 = temp % num2 ;
33
+ }
34
+ return num1 ;
35
+ }
36
+ export default countDifferentSubsequenceGCDs ;
You can’t perform that action at this time.
0 commit comments