File tree Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ leetcode 测试
10
10
11
11
##### 包含的内容如下
12
12
13
+ https://leetcode.cn/problems/three-equal-parts/
14
+
13
15
https://leetcode.cn/problems/number-of-pairs-satisfying-inequality/
14
16
15
17
https://leetcode.cn/problems/count-of-smaller-numbers-after-self/
Original file line number Diff line number Diff line change
1
+ import sum from "https://esm.sh/[email protected] /sum?dts" ;
2
+
3
+ export default function threeEqualParts ( arr : number [ ] ) : number [ ] {
4
+ const summ = sum ( arr ) ;
5
+ if ( summ % 3 !== 0 ) {
6
+ return [ - 1 , - 1 ] ;
7
+ }
8
+ if ( summ === 0 ) {
9
+ return [ 0 , 2 ] ;
10
+ }
11
+
12
+ const partial = Math . floor ( summ / 3 ) ;
13
+ let first = 0 ,
14
+ second = 0 ,
15
+ third = 0 ,
16
+ cur = 0 ;
17
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
18
+ if ( arr [ i ] === 1 ) {
19
+ if ( cur === 0 ) {
20
+ first = i ;
21
+ } else if ( cur === partial ) {
22
+ second = i ;
23
+ } else if ( cur === 2 * partial ) {
24
+ third = i ;
25
+ }
26
+ cur ++ ;
27
+ }
28
+ }
29
+
30
+ const len = arr . length - third ;
31
+ if ( first + len <= second && second + len <= third ) {
32
+ let i = 0 ;
33
+ while ( third + i < arr . length ) {
34
+ if (
35
+ arr [ first + i ] !== arr [ second + i ] ||
36
+ arr [ first + i ] !== arr [ third + i ]
37
+ ) {
38
+ return [ - 1 , - 1 ] ;
39
+ }
40
+ i ++ ;
41
+ }
42
+ return [ first + len - 1 , second + len ] ;
43
+ }
44
+ return [ - 1 , - 1 ] ;
45
+ }
Original file line number Diff line number Diff line change
1
+ import { assertEquals } from "https://deno.land/[email protected] /testing/asserts.ts" ;
2
+ import threeEqualParts from "./index.ts" ;
3
+ Deno . test ( "three-equal-parts" , ( ) => {
4
+ assertEquals (
5
+ [
6
+ [ 1 , 0 , 1 , 0 , 1 ] ,
7
+ [ 1 , 1 , 0 , 1 , 1 ] ,
8
+ [ 1 , 1 , 0 , 0 , 1 ] ,
9
+ ] . map ( threeEqualParts ) ,
10
+ [
11
+ [ 0 , 3 ] ,
12
+ [ - 1 , - 1 ] ,
13
+ [ 0 , 2 ] ,
14
+ ] ,
15
+ ) ;
16
+ } ) ;
You can’t perform that action at this time.
0 commit comments