Skip to content

Commit 79e5435

Browse files
committed
update
1 parent 2dab7f0 commit 79e5435

File tree

3 files changed

+209
-0
lines changed

3 files changed

+209
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* Best Time to Buy and Sell Stock II
3+
* Say you have an array for which the ith element is the price of a given stock on day i.
4+
*
5+
* Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
6+
*
7+
* Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
8+
*
9+
* Example 1:
10+
*
11+
* Input: [7,1,5,3,6,4]
12+
* Output: 7
13+
* Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
14+
* Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
15+
*
16+
* Example 2:
17+
*
18+
* Input: [1,2,3,4,5]
19+
* Output: 4
20+
* Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
21+
* Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
22+
* engaging multiple transactions at the same time. You must sell before buying again.
23+
*
24+
* Example 3:
25+
*
26+
* Input: [7,6,4,3,1]
27+
* Output: 0
28+
* Explanation: In this case, no transaction is done, i.e. max profit = 0.
29+
*/
30+
31+
const assert = require('assert');
32+
33+
/**
34+
* @param {number[]} prices
35+
* @return {number}
36+
* @Complexity Analysis
37+
* Time: O(n)
38+
* Space: O(1)
39+
*/
40+
function maxProfit(prices) {
41+
let profit = 0;
42+
43+
for (let i = 1, l = prices.length; i < l; i++) {
44+
if (prices[i] > prices[i - 1]) {
45+
profit += prices[i] - prices[i - 1];
46+
}
47+
}
48+
49+
return profit;
50+
}
51+
52+
/**
53+
* @param {number[]} prices
54+
* @return {number}
55+
* @Complexity Analysis
56+
* Time: O(n)
57+
* Space: O(1)
58+
*/
59+
function maxProfitPeakValley(prices) {
60+
let len = prices.length;
61+
let profit = 0;
62+
let i = 0;
63+
let valley;
64+
65+
while (i < len - 1) {
66+
67+
while (i < len - 1 && prices[i] >= prices[i + 1]) {
68+
i++;
69+
}
70+
71+
valley = prices[i];
72+
73+
while (i < len - 1 && prices[i] <= prices[i + 1]) {
74+
i++;
75+
}
76+
77+
profit += prices[i] - valley;
78+
}
79+
80+
81+
return profit;
82+
}
83+
84+
85+
assert.deepStrictEqual(maxProfit([7,1,5,3,6,4]), 7);
86+
assert.deepStrictEqual(maxProfit([1,2,3,4,5]), 4);
87+
assert.deepStrictEqual(maxProfit([7,6,4,3,1]), 0);
88+
89+
assert.deepStrictEqual(maxProfitPeakValley([7,1,5,3,6,4]), 7);
90+
assert.deepStrictEqual(maxProfitPeakValley([1,2,3,4,5]), 4);
91+
assert.deepStrictEqual(maxProfitPeakValley([7,6,4,3,1]), 0);
92+

merge-sort.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Algorithms: Implement Merge Sort
3+
*
4+
* Another intermediate sorting algorithm that is very common is merge sort. Like quick sort, merge sort also uses a divide-and-conquer, recursive methodology to sort an array. It takes advantage of the fact that it is relatively easy to sort two arrays as long as each is sorted in the first place. But we'll start with only one array as input, so how do we get to two sorted arrays from that? Well, we can recursively divide the original input in two until we reach the base case of an array with one item. A single-item array is naturally sorted, so then we can start combining. This combination will unwind the recursive calls that split the original array, eventually producing a final sorted array of all the elements. The steps of merge sort, then, are:
5+
*
6+
* 1) Recursively split the input array in half until a sub-array with only one element is produced.
7+
*
8+
* 2) Merge each sorted sub-array together to produce the final sorted array.
9+
*
10+
* Merge sort is an efficient sorting method, with time complexity of O(nlog(n)). This algorithm is popular because it is performant and relatively easy to implement.
11+
*
12+
* As an aside, this will be the last sorting algorithm we cover here. However, later in the section on tree data structures we will describe heap sort, another efficient sorting method that requires a binary heap in its implementation.
13+
*
14+
* Instructions: Write a function mergeSort which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest. A good way to implement this is to write one function, for instance merge, which is responsible for merging two sorted arrays, and another function, for instance mergeSort, which is responsible for the recursion that produces single-item arrays to feed into merge. Good luck!
15+
*/
16+
17+
function mergeSort(arr) {
18+
if (arr.length <= 1) {
19+
return arr;
20+
}
21+
22+
const mid = Math.floor(arr.length / 2);
23+
const left = mergeSort(arr.slice(0, mid));
24+
const right = mergeSort(arr.slice(mid));
25+
26+
let i = 0;
27+
let j = 0;
28+
let sorted = [];
29+
30+
while (i < left.length && j < right.length) {
31+
if (left[i] < right[j]) {
32+
sorted.push(left[i++]);
33+
} else {
34+
sorted.push(right[j++]);
35+
}
36+
}
37+
38+
while (i < left.length) {
39+
sorted.push(left[i++]);
40+
}
41+
42+
while (j < right.length) {
43+
sorted.push(right[j++]);
44+
}
45+
46+
return sorted;
47+
}
48+
49+
console.log(mergeSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]));
50+
console.log(mergeSort([1, 4, 2, 8, 345, 3]));
51+
console.log(mergeSort([1, 3, 2, 4, 5]));

quick-sort.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Algorithms: Implement Quick Sort
3+
*
4+
* Here we will move on to an intermediate sorting algorithm: quick sort. Quick sort is an efficient, recursive divide-and-conquer approach to sorting an array. In this method, a pivot value is chosen in the original array. The array is then partitioned into two subarrays of values less than and greater than the pivot value. We then combine the result of recursively calling the quick sort algorithm on both sub-arrays. This continues until the base case of an empty or single-item array is reached, which we return. The unwinding of the recursive calls return us the sorted array.
5+
*
6+
* Quick sort is a very efficient sorting method, providing O(nlog(n)) performance on average. It is also relatively easy to implement. These attributes make it a popular and useful sorting method.
7+
*
8+
* Instructions: Write a function quickSort which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest. While the choice of the pivot value is important, any pivot will do for our purposes here. For simplicity, the first or last element could be used.
9+
*/
10+
11+
function quickSort(array, left = 0, right = array.length - 1) {
12+
13+
if (left < right) {
14+
let pivotIndex = pivot(array, left, right);
15+
16+
quickSort(array, left, pivotIndex - 1);
17+
quickSort(array, pivotIndex + 1, right);
18+
}
19+
20+
return array;
21+
}
22+
23+
function pivot(array, left = 0, right = array.length - 1) {
24+
let shift = left;
25+
26+
for (let i = left + 1; i <= right; i++) {
27+
if (array[i] < array[left]) {
28+
swap(array, i, ++shift);
29+
}
30+
}
31+
32+
swap(array, left, shift);
33+
34+
return shift;
35+
}
36+
37+
// function partition(array, left, right) {
38+
// let pivot = array[Math.floor((left + right) / 2)];
39+
40+
// while (left <= right) {
41+
42+
// while (array[left] < pivot) {
43+
// left++;
44+
// }
45+
46+
// while (array[right] > pivot) {
47+
// right--;
48+
// }
49+
50+
// if (left <= right) {
51+
// swap(array, left, right);
52+
// left++;
53+
// right--;
54+
// }
55+
56+
// }
57+
58+
// return left;
59+
// }
60+
61+
function swap(arr, i, j) {
62+
[arr[i], arr[j]] = [arr[j], arr[i]];
63+
}
64+
65+
console.log(quickSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]));
66+

0 commit comments

Comments
 (0)