File tree 1 file changed +47
-0
lines changed
1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Time O(n) | Space O(n)
3
+ * HashSet | Math
4
+ * https://leetcode.com/problems/minimum-index-of-a-valid-split
5
+ * @param {number[] } nums
6
+ * @return {number }
7
+ */
8
+ var minimumIndex = function ( nums ) {
9
+
10
+ const freq = { } ;
11
+
12
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
13
+
14
+ const num = nums [ i ] ;
15
+ freq [ num ] = ( freq [ num ] && freq [ num ] + 1 ) || 1 ;
16
+ }
17
+
18
+ let target = 0 ;
19
+ let maxOccurance = 0 ;
20
+
21
+ for ( const key in freq ) {
22
+
23
+ if ( freq [ key ] > maxOccurance ) {
24
+ maxOccurance = freq [ key ] ;
25
+ target = + key ;
26
+ }
27
+ }
28
+
29
+ let leftSideOccurance = 0 ;
30
+ let rightSideOccurance = maxOccurance ;
31
+
32
+ for ( let i = 0 ; i < nums . length - 1 ; i ++ ) {
33
+
34
+ const num = nums [ i ] ;
35
+
36
+ if ( num === target ) {
37
+
38
+ leftSideOccurance += 1 ;
39
+ rightSideOccurance -= 1 ;
40
+ if ( leftSideOccurance > ( i + 1 ) / 2 &&
41
+ rightSideOccurance > ( nums . length - i - 1 ) / 2
42
+ ) return i ;
43
+ }
44
+ }
45
+
46
+ return - 1 ;
47
+ } ;
You can’t perform that action at this time.
0 commit comments