File tree Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ * @return {number }
4
+ */
5
+ const findShortestSubArray = function ( nums ) {
6
+ const left = { } ;
7
+ const right = { } ;
8
+ const count = { } ;
9
+
10
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
11
+ if ( ! left . hasOwnProperty ( nums [ i ] ) ) {
12
+ left [ nums [ i ] ] = i ;
13
+ }
14
+ right [ nums [ i ] ] = i ;
15
+ count [ nums [ i ] ] = count [ nums [ i ] ] ? count [ nums [ i ] ] + 1 : 1 ;
16
+ }
17
+ const degree = Math . max ( ...Object . keys ( count ) . map ( el => count [ el ] ) ) ;
18
+ let res = nums . length ;
19
+ for ( let el in count ) {
20
+ if ( count . hasOwnProperty ( el ) && count [ el ] === degree ) {
21
+ res = Math . min ( res , right [ el ] - left [ el ] + 1 ) ;
22
+ }
23
+ }
24
+
25
+ return res ;
26
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * function ListNode(val) {
4
+ * this.val = val;
5
+ * this.next = null;
6
+ * }
7
+ */
8
+ /**
9
+ * @param {ListNode } root
10
+ * @param {number } k
11
+ * @return {ListNode[] }
12
+ */
13
+ const splitListToParts = function ( root , k ) {
14
+ let cur = root ;
15
+ let N = 0 ;
16
+ while ( cur != null ) {
17
+ cur = cur . next ;
18
+ N ++ ;
19
+ }
20
+ let width = Math . floor ( N / k ) ,
21
+ rem = N % k ;
22
+ let ans = [ ] ;
23
+ cur = root ;
24
+ for ( let i = 0 ; i < k ; ++ i ) {
25
+ let head = cur ;
26
+ for ( let j = 0 ; j < width + ( i < rem ? 1 : 0 ) - 1 ; ++ j ) {
27
+ if ( cur != null ) cur = cur . next ;
28
+ }
29
+ if ( cur != null ) {
30
+ let prev = cur ;
31
+ cur = cur . next ;
32
+ prev . next = null ;
33
+ }
34
+ ans [ i ] = head ;
35
+ }
36
+ return ans ;
37
+ } ;
You can’t perform that action at this time.
0 commit comments