8000 update 26 oct · khemssharma/leetcode@a7bea59 · GitHub
[go: up one dir, main page]

Skip to content

Commit a7bea59

Browse files
committed
update 26 oct
1 parent a715604 commit a7bea59

File tree

1 file changed

+85
-92
lines changed

1 file changed

+85
-92
lines changed

array.js

Lines changed: 85 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
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
37
// Simply check if the next number is greater than the current
48
// If the array is sorted in cyclic order then the next is nums[(i+1)%nums.length]
59
// 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) {
1519
return true;
1620
};
1721

18-
// array increment by one
19-
var plusOne = function (digits) {
22+
var plusOne = function (digits) { // array increment by one
2023
for (let i = digits.length - 1; i >= 0; i--) {
2124
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
2426
return digits;
2527
}
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
2829
}
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
3131
return digits;
3232
};
3333

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
3646
let k = 0;
3747
for (let i in nums) {
38-
if (nums[i] !== nums[k]) {
48+
if (nums[i] !== nums[k]) { // we found second/third... distinct number k
3949
k++;
40-
nums[k] = nums[i]
50+
nums[k] = nums[i] // overrides itself or previous duplicate
4151
}
4252
}
4353
return k + 1;
4454
};
4555

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
5561
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--;
5678
}
5779
}
58-
return k;
80+
return maxWater;
5981
};
6082

61-
// search insert position
62-
var searchInsert = function (nums, target) {
83+
var searchInsert = function (nums, target) { // search insert position
6384
let left = 0,
6485
right = nums.length - 1;
6586
while (left <= right) {
@@ -75,61 +96,25 @@ var searchInsert = function (nums, target) {
7596
return left;
7697
};
7798

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]]
98105
}
99-
return index;
100106
}
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;
123110
}
124111
}
125-
return maxWater;
112+
return n + 1; // If all numbers are at their correct position return i+1
126113
};
127114

128-
// trapping rain water
129-
var trap = function (height) {
115+
var trap = function (height) { // trapping rain water
130116
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
133118
leftMax = 0;
134119
let right = height.length - 1,
135120
rightMax = 0;
@@ -154,24 +139,32 @@ var trap = function (height) {
154139
return waterTrapped;
155140
};
156141

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+
}
167161
}
162+
return index;
168163
}
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];
174167
}
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

Comments
 (0)
0