1
- // check if array is sorted (increasing) and rotated
2
- var check = function ( nums ) {
1
+ /** JSDoc comment like this is used for compile time type validation
2
+ * @param {number[] } prices
3
+ * @return {number }
4
+ */
5
+
6
+ var check = function ( nums ) { // check if array is sorted (increasing) and rotated
3
7
// Simply check if the next number is greater than the current
4
8
// If the array is sorted in cyclic order then the next is nums[(i+1)%nums.length]
5
9
// in cyclic sorted array only 1 time the next number can not be greater/lesser than the current
@@ -15,51 +19,68 @@ var check = function (nums) {
15
19
return true ;
16
20
} ;
17
21
18
- // array increment by one
19
- var plusOne = function ( digits ) {
22
+ var plusOne = function ( digits ) { // array increment by one
20
23
for ( let i = digits . length - 1 ; i >= 0 ; i -- ) {
21
24
if ( digits [ i ] < 9 ) {
22
- // simply add one and return digits
23
- digits [ i ] += 1 ;
25
+ digits [ i ] += 1 ; // simply add one and return digits
24
26
return digits ;
25
27
}
26
- // countinuosly mark digit zero if it is 9
27
- digits [ i ] = 0 ;
28
+ digits [ i ] = 0 ; // countinuosly mark digit zero if it is 9
28
29
}
29
- // if all digits are marked zero then add a one in the beginning and return digits
30
- digits . unshift ( 1 ) ;
30
+ digits . unshift ( 1 ) ; // if all digits are marked zero then add a one in the beginning and return digits
31
31
return digits ;
32
32
} ;
33
33
34
- // remove duplicates in nums
35
- var removeDuplicates = function ( nums ) {
34
+ var maxProfit = function ( prices ) { // Best time to buy and sell stocks
35
+ let maxProfit = 0 ; // no profit
36
+ let minPrice = Infinity ; // highest price
37
+ for ( let price of prices ) { // look for every price
38
+ minPrice = Math . min ( minPrice , price ) ; // compare cuurent price with minimum price
39
+ const profit = price - minPrice ; // find current profit
40
+ maxProfit = Math . max ( maxProfit , profit ) ; // compare current profit with maximum profit
41
+ }
42
+ return maxProfit ;
43
+ } ;
44
+
45
+ var removeDuplicates = function ( nums ) { // remove duplicates in nums
36
46
let k = 0 ;
37
47
for ( let i in nums ) {
38
- if ( nums [ i ] !== nums [ k ] ) {
48
+ if ( nums [ i ] !== nums [ k ] ) { // we found second/third... distinct number k
39
49
k ++ ;
40
- nums [ k ] = nums [ i ]
50
+ nums [ k ] = nums [ i ] // overrides itself or previous duplicate
41
51
}
42
52
}
43
53
return k + 1 ;
44
54
} ;
45
55
46
- // remove element by value
47
- var removeElement = function ( nums , val ) {
48
- // pointer k
49
- let k = 0 ;
50
- // iterate over nums
51
- for ( let i = 0 ; i < nums . length ; i ++ ) {
52
- // for unique number
53
- if ( nums [ i ] !== val ) {
54
- nums [ k ] = nums [ i ] ;
56
+ var removeElement = function ( nums , val ) { // remove element by value
57
+ let k = 0 ; // pointer k
58
+ for ( let i = 0 ; i < nums . length ; i ++ ) { // iterate over nums
59
+ if ( nums [ i ] !== val ) {
60
+ nums [ k ] = nums [ i ] ; // for number not equal to given, value override itself
55
61
k ++ ;
62
+ } // for number to remove, number is overriden by next
63
+ } // at the end of loop there would be one duplicate number in the array
64
+ return k ; // but only the count of distinct number will be returned excluding duplicate
65
+ } ;
66
+
67
+ var maxArea = function ( height ) { // container with most water
68
+ let left = 0 ; // two pointers
69
+ let right = height . length - 1 ;
70
+ let maxWater = 0 ;
71
+ while ( left < right ) {
72
+ const area = Math . min ( height [ left ] , height [ right ] ) * ( right - left )
73
+ maxWater = Math . max ( maxWater, area ) // update maxWater only when necessary
74
+ if ( height [ left ] < height [ right ] ) {
75
+ left ++ ;
76
+ } else {
77
+ right -- ;
56
78
}
57
79
}
58
- return k ;
80
+ return maxWater ;
59
81
} ;
60
82
61
- // search insert position
62
- var searchInsert = function ( nums , target ) {
83
+ var searchInsert = function ( nums , target ) { // search insert position
63
84
let left = 0 ,
64
85
right = nums . length - 1 ;
65
86
while ( left <= right ) {
@@ -75,61 +96,25 @@ var searchInsert = function (nums, target) {
75
96
return left ;
76
97
} ;
77
98
78
- // Find first and last position of an elemnet ina sorted array
79
- var searchRange = function ( nums , target ) {
80
- const binarySearch = ( isleft ) => {
81
- let left = 0 ,
82
- right = nums . length - 1 ;
83
- let index = - 1 ;
84
- while ( left <= right ) {
85
- let mid = Math . floor ( ( left + right ) / 2 ) ;
86
- if ( nums [ mid ] === target ) {
87
- index = mid ;
88
- if ( isleft ) {
89
- right = mid - 1 ;
90
- } else {
91
- left = mid + 1 ;
92
- }
93
- } else if ( nums [ mid ] < target ) {
94
- left = mid + 1 ;
95
- } else {
96
- right = mid - 1 ;
97
- }
99
+ var firstMissingPositive = function ( nums ) { // first missing positive
100
+ let n = nums . length ;
101
+ for ( let i in nums ) { // Place the numbers in thier correct position
102
+ while ( nums [ i ] > 0 && nums [ i ] <= n && nums [ nums [ i ] - 1 ] !== nums [ i ] ) { // while the number is in range but not in correct position
103
+ let correctIndex = nums [ i ] - 1 ; // swap number at i with its correct position
104
+ [ nums [ i ] , nums [ correctIndex ] ] = [ nums [ correctIndex ] , nums [ i ] ]
98
105
}
99
- return index ;
100
106
}
101
- const start = binarySearch ( true ) ;
102
- if ( start === - 1 ) {
103
- return [ - 1 , - 1 ] ;
104
- }
105
- const end = binarySearch ( false ) ;
106
- return [ start , end ] ;
107
- } ;
108
-
109
- // container with most water
110
- var maxArea = function ( height ) {
111
- // two pointers
112
- let left = 0 ;
113
- let right = height . length - 1 ;
114
- let maxWater = 0 ;
115
- while ( left < right ) {
116
- const area = Math . min ( height [ left ] , height [ right ] ) * ( right - left )
117
- // update maxWater only when necessary
118
- maxWater = Math . max ( maxWater , area )
119
- if ( height [ left ] < height [ right ] ) {
120
- left ++ ;
121
- } else {
122
- right -- ;
107
+ for ( let i = 0 ; i < n ; i ++ ) { // Identify the first missing positive integer
108
+ if ( nums [ i ] !== i + 1 ) {
109
+ return i + 1 ;
123
110
}
124
111
}
125
- return maxWater ;
112
+ return n + 1 ; // If all numbers are at their correct position return i+1
126
113
} ;
127
114
128
- // trapping rain water
129
- var trap = function ( height ) {
115
+ var trap = function ( height ) { // trapping rain water
130
116
if ( height . length === 0 ) return 0 ;
131
- // two pointer and correspoding variables
132
- let left = 0 ,
117
+ let left = 0 , // two pointer and correspoding variables
133
118
leftMax = 0 ;
134
119
let right = height . length - 1 ,
135
120
rightMax = 0 ;
@@ -154,24 +139,32 @@ var trap = function (height) {
154
139
return waterTrapped ;
155
140
} ;
156
141
157
- // first missing positive
158
- var firstMissingPositive = function ( nums ) {
159
- let n = nums . length ;
160
- // Place the numbers in thier correct position
161
- for ( let i in nums ) {
162
- // while the number is in range but not in correct position
163
- while ( nums [ i ] > 0 && nums [ i ] <= n && nums [ nums [ i ] - 1 ] !== nums [ i ] ) {
164
- // swap number at i with its correct position
165
- let correctIndex = nums [ i ] - 1 ;
166
- [ nums [ i ] , nums [ correctIndex ] ] = [ nums [ correctIndex ] , nums [ i ] ]
142
+ var searchRange = function ( nums , target ) { // Find first and last position of an elemnet in a sorted array
143
+ const binarySearch = ( isleft ) => {
144
+ let left = 0 ,
145
+ right = nums . length - 1 ;
146
+ let index = - 1 ;
147
+ while ( left <= right ) {
148
+ let mid = Math . floor ( ( left + right ) / 2 ) ;
149
+ if ( nums [ mid ] === target ) {
150
+ index = mid ;
151
+ if ( isleft ) {
152
+ right = mid - 1 ;
153
+ } else {
154
+ left = mid + 1 ;
155
+ }
156
+ } else if ( nums [ mid ] < target ) {
157
+ left = mid + 1 ;
158
+ } else {
159
+ right = mid - 1 ;
160
+ }
167
161
}
162
+ return index ;
168
163
}
169
- // Identify the first missing positive integer
170
- for ( let i = 0 ; i < n ; i ++ ) {
171
- if ( nums [ i ] !== i + 1 ) {
172
- return i + 1 ;
173
- }
164
+ const start = binarySearch ( true ) ;
165
+ if ( start === - 1 ) {
166
+ return [ - 1 , - 1 ] ;
174
167
}
175
- // If all numbers are at their correct position return i+1
176
- return n + 1 ;
177
- } ;
168
+ const end = binarySearch ( false ) ;
169
+ return [ start , end ] ;
170
+ } ;
0 commit comments