Skip to content

Commit cba3bf9

Browse files
committed
more qiestion practice
1 parent 42d744e commit cba3bf9

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
*
3+
* @param {[]} prices
4+
* @returns Number
5+
* - Initialize `minPrice` to a very large value (or prices[i]) and `maxProfit` to 0.
6+
- Iterate through the prices:
7+
- Update `minPrice` to the smaller of `minPrice` or the current price.
8+
- Compute the profit for the current price: `currentProfit = currentPrice - minPrice`.
9+
- Update `maxProfit` if `currentProfit` is greater than `maxProfit`.
10+
- Return `maxProfit`.
11+
12+
Complexity
13+
TC O(n)
14+
with O(1) space, as it only uses two variables.
15+
*
16+
*/
17+
18+
function maxProfit(prices) {
19+
let len = prices.length;
20+
let maxProfit = 0;
21+
let minPrice = prices[0];
22+
23+
for (let i = 0; i < len; i++) {
24+
if (prices[i] < minPrice) {
25+
minPrice = prices[i];
26+
}
27+
28+
let currentProfit = prices[i] - minPrice;
29+
if (currentProfit > maxProfit) {
30+
maxProfit = currentProfit;
31+
}
32+
}
33+
return maxProfit;
34+
}
35+
36+
/** Explaination
37+
*
38+
* Day 0: First price is 7, so we set minPrice to 7. No profit can be made yet.
39+
Day 1: Price drops to 1, which is the lowest so far. Update minPrice to 1.
40+
Day 2: Price increases to 5. The profit from buying at 1 and selling at 5 is 4, so update maxProfit to 4.
41+
Day 3: Price drops to 3. Profit from buying at 1 and selling at 3 is 2. maxProfit remains 4 as this profit is smaller.
42+
Day 4: Price increases to 6. The profit from buying at 1 and selling at 6 is 5, so update maxProfit to 5.
43+
Day 5: Price decreases to 4. Profit from buying at 1 and selling at 4 is 3. maxProfit remains 5.
44+
*/
45+
46+
/**
47+
* Day prices[i] minPrice profit (prices[i] - minPrice) maxProfit
48+
49+
0 7 7 0 0
50+
1 1 1 0 0
51+
2 5 1 4 4
52+
3 3 1 2 4
53+
4 6 1 5 5
54+
5 4 1 3 5
55+
*/

Easy-Questions/two_sneaky_numbers.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
*
3+
* @param {[]} nums
4+
* Complexity - TC - O(n), SC - O(n)
5+
*/
6+
7+
function getSneakyNumbers(nums) {
8+
let hashMap = {};
9+
let ans = [];
10+
11+
for(let key of nums) {
12+
hashMap[key] = (hashMap[key] || 0) + 1;
13+
}
14+
for(let i in hashMap) {
15+
if(hashMap[i] >= 2 ){
16+
ans.push(parseInt(i));
17+
}
18+
}
19+
return ans;
20+
}
21+
22+
let nums = [7,1,5,4,3,4,6,0,9,5,8,2];
23+
const result = getSneakyNumbers(nums);
24+
console.log(result);
25+

Medium-Questions/rotate_array.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
function rotateArray(nums) {
2+
if (nums.length < 1 && k === 0) return nums;
3+
4+
let i = 0;
5+
let j = nums.length - 1;
6+
k = k % nums.length;
7+
8+
function reverseArray(i, j) {
9+
while (i < j) {
10+
[nums[i], nums[j]] = [nums[j], nums[i]];
11+
i++;
12+
j--;
13+
}
14+
}
15+
16+
reverseArray(0, j);
17+
reverseArray(0, k - 1); // reverse elements from 0 value to k value
18+
reverseArray(k, j);
19+
}
20+
21+
/**
22+
* Short Notes for Key Concepts:
23+
-Reverse Approach:
24+
-
25+
-Rotate the array by reversing parts of it in three steps:
26+
-Reverse the entire array.
27+
-Reverse the first k elements.
28+
-Reverse the remaining elements.
29+
-Key Observations:
30+
-
31+
-Reversing the entire array moves the k largest elements to the front.
32+
-Reversing the first k elements reorders them correctly.
33+
-Reversing the rest restores their original order.
34+
-Time Complexity:
35+
-
36+
-Each reversal operation takes O(n) time.
37+
-Total complexity: O(n).
38+
-Space Complexity:
39+
-
40+
-No additional space is used for another array.
41+
-Space complexity: O(1).
42+
-Edge Case Handling:
43+
-
44+
-Normalizing k (k = k % nums.length) ensures we avoid redundant rotations (e.g., k > nums. -length).
45+
*
46+
* Time Complexity:
47+
48+
Each reversal operation takes O(n) time.
49+
Total complexity: O(n).
50+
Space Complexity:
51+
52+
No additional space is used for another array.
53+
Space complexity: O(1).
54+
*
55+
*/

0 commit comments

Comments
 (0)