File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @param {number } k
4+ * @return {number }
5+ */
6+ var largestInteger = function ( nums , k ) {
7+ if ( nums . length < k ) return - 1 ;
8+ if ( nums . length === k ) return Math . max ( ...nums ) ;
9+
10+ const group = { } ;
11+ let max = - 1 ;
12+ // k가 1이면 중간에 겹치는거 없으면 됨
13+ // 양쪽 끝은 중간에 숫자가 없으면 안겹침
14+
15+ for ( let i = 0 ; i < nums . length ; i ++ ) {
16+ if ( k !== 1 && ( i == 0 || i == nums . length - 1 ) ) continue ;
17+ group [ nums [ i ] ] = ( group [ nums [ i ] ] || 0 ) + 1 ;
18+ }
19+
20+ if ( k !== 1 ) {
21+ const left = nums [ 0 ] ;
22+ const right = nums [ nums . length - 1 ] ;
23+ if ( left === right ) return - 1 ;
24+ if ( group [ left ] === undefined ) max = Math . max ( max , left ) ;
25+ if ( group [ right ] === undefined ) max = Math . max ( max , right ) ;
26+ } else {
27+ for ( const [ key , value ] of Object . entries ( group ) ) {
28+ if ( value === 1 ) max = Math . max ( key ) ;
29+ }
30+ }
31+ return max ;
32+ } ;
You can’t perform that action at this time.
0 commit comments