1+ // subarrays
2+ var searchMatrix = function ( matrix , target ) {
3+ if ( ! matrix . length || ! matrix [ 0 ] . length ) return false ;
4+ let row = 0 ;
5+ let col = matrix [ 0 ] . length - 1 ;
6+ while ( row < matrix . length && col >= 0 ) {
7+ if ( matrix [ row ] [ col ] === target ) {
8+ return true ;
9+ } else if ( matrix [ row ] [ col ] > target ) {
10+ col --
11+ } else row ++
12+ }
13+ return false
14+ } ;
15+
16+ var maxArea = function ( height ) { // container with most water
17+ let left = 0 ; // two pointers
18+ let right = height . length - 1 ;
19+ let maxWater = 0 ;
20+ while ( left < right ) {
21+ const area = Math . min ( height [ left ] , height [ right ] ) * ( right - left ) // calculate area enclosed by left and right pillar
22+ maxWater = Math . max ( maxWater , area ) // if current area is maximum, update it
23+ if ( height [ left ] < height [ right ] ) { // continue with next larger pillar whether it is left or right
24+ left ++ ;
25+ } else {
26+ right -- ;
27+ }
28+ }
29+ return maxWater ;
30+ } ;
31+
32+ var searchInsert = function ( nums , target ) { // search insert position
33+ let left = 0 ,
34+ right = nums . length - 1 ;
35+ while ( left <= right ) {
36+ let mid = Math . floor ( ( left + right ) / 2 ) ;
37+ if ( nums [ mid ] === target ) {
38+ return mid ;
39+ } else if ( nums [ mid ] < target ) {
40+ left = mid + 1 ;
41+ } else {
42+ right = mid - 1 ;
43+ }
44+ }
45+ return left ;
46+ } ;
47+
48+ var spiralOrder = function ( matrix ) {
49+ const result = [ ] ;
50+ if ( matrix . length == 0 ) return result ;
51+ let top = 0 ,
52+ bottom = matrix . length - 1 ;
53+ let left = 0 ,
54+ right = matrix [ 0 ] . length - 1 ;
55+ while ( top <= bottom && left <= right ) {
56+ for ( let i = left ; i <= right ; i ++ ) {
57+ result . push ( matrix [ top ] [ i ] ) ;
58+ }
59+ top ++ ;
60+ for ( let i = top ; i <= bottom ; i ++ ) {
61+ result . push ( matrix [ i ] [ right ] ) ;
62+ }
63+ right -- ;
64+ if ( top <= bottom ) {
65+ for ( let i = right ; i >= left ; i -- ) {
66+ result . push ( matrix [ bottom ] [ i ] ) ;
67+ }
68+ }
69+ bottom -- ;
70+ if ( left <= right ) {
71+ for ( let i = bottom ; i >= top ; i -- ) {
72+ result . push ( matrix [ i ] [ left ] ) ;
73+ }
74+ }
75+ left ++ ;
76+ }
77+ return result ;
78+ } ;
79+
80+ var trap = function ( height ) { // trapping rain water
81+ if ( height . length === 0 ) return 0 ;
82+ let left = 0 , // two pointer and correspoding variables
83+ leftMax = 0 ;
84+ let right = height . length - 1 ,
85+ rightMax = 0 ;
86+ let waterTrapped = 0 ;
87+ while ( left < right ) {
88+ if ( height [ left ] < height [ right ] ) {
89+ if ( height [ left ] >= leftMax ) {
90+ leftMax = height [ left ] ;
91+ } else {
92+ waterTrapped += leftMax - height [ left ]
93+ }
94+ left ++ ;
95+ } else {
96+ if ( height [ right ] >= rightMax ) {
97+ rightMax = height [ right ] ;
98+ } else {
99+ waterTrapped += rightMax - height [ right ]
100+ }
101+ right -- ;
102+ }
103+ }
104+ return waterTrapped ;
105+ } ;
106+
107+
108+ var searchRange = function ( nums , target ) { // Find first and last position of an elemnet in a sorted array
109+ const binarySearch = ( isleft ) => {
110+ let left = 0 ,
111+ right = nums . length - 1 ;
112+ let index = - 1 ;
113+ while ( left <= right ) {
114+ let mid = Math . floor ( ( left + right ) / 2 ) ;
115+ if ( nums [ mid ] === target ) {
116+ index = mid ;
117+ if ( isleft ) {
118+ right = mid - 1 ;
119+ } else {
120+ left = mid + 1 ;
121+ }
122+ } else if ( nums [ mid ] < target ) {
123+ left = mid + 1 ;
124+ } else {
125+ right = mid - 1 ;
126+ }
127+ }
128+ return index ;
129+ }
130+ const start = binarySearch ( true ) ;
131+ if ( start === - 1 ) {
132+ return [ - 1 , - 1 ] ;
133+ }
134+ const end = binarySearch ( false ) ;
135+ return [ start , end ] ;
136+ } ;
0 commit comments