Skip to content

Commit ef3359f

Browse files
committedMar 8, 2019
update
1 parent 15e67d2 commit ef3359f

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed
 

‎get-max-profit.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Writing programming interview questions hasn't made me rich yet ... so I might give up and start trading Apple stocks all day instead.
3+
*
4+
* First, I wanna know how much money I could have made yesterday if I'd been trading Apple stocks all day.
5+
*
6+
* So I grabbed Apple's stock prices from yesterday and put them in an array called stockPrices, where:
7+
*
8+
* The indices are the time (in minutes) past trade opening time, which was 9:30am local time.
9+
* The values are the price (in US dollars) of one share of Apple stock at that time.
10+
*
11+
* So if the stock cost $500 at 10:30am, that means stockPrices[60] = 500.
12+
*
13+
* Write an efficient function that takes stockPrices and returns the best profit I could have made from one purchase and one sale of one share of Apple stock yesterday.
14+
*/
15+
16+
function getMaxProfit(prices) {
17+
if (prices.length < 2) {
18+
throw new Error("Require at least two prices");
19+
}
20+
21+
let minBuying = prices[0];
22+
let maxProfit = -Infinity;
23+
24+
for (let i = 1, l = prices.length; i < l; i++) {
25+
minBuying = Math.min(minBuying, prices[i]);
26+
maxProfit = Math.max(maxProfit, prices[i] - minBuying);
27+
}
28+
29+
return maxProfit;
30+
}
31+
32+
const stockPrices = [10, 7, 5, 8, 11, 9];
33+
34+
console.log(getMaxProfit(stockPrices));
35+
// Returns 6 (buying for $5 and selling for $11)

‎largest-range.js

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
const assert = require('assert');
2+
3+
/**
4+
* Largest Range
5+
* Write a function that takes in an array of integers and returns an array of length 2 representing the largest range of numbers contained in that array. The first number in the output array should be the first number in the range while the second number should be the last number in the range. A range of numbers is defined as a set of numbers that come right after each other in the set of real integers. For instance, the output array [2, 6] represents the range {2, 3, 4, 5, 6}, which is a range of length 5. Note that numbers do not need to be ordered or adjacent in the array in order to form a range. Assume that there will only be one largest range.
6+
*/
7+
8+
function largestRange(array) {
9+
const nums = {};
10+
let ans = [];
11+
let largest = 0;
12+
13+
for (let n of array) {
14+
nums[n] = true;
15+
}
16+
17+
for (let n of array) {
18+
let len = 1;
19+
let min = n - 1;
20+
let max = n + 1;
21+
22+
nums[n] = false;
23+
24+
while (nums[min] === true) {
25+
nums[min] = false;
26+
len++;
27+
min--;
28+
}
29+
30+
while (nums[max] === true) {
31+
nums[max] = false;
32+
len++;
33+
max++;
34+
}
35+
36+
if (len > largest) {
37+
largest = len;
38+
ans = [min + 1, max - 1];
39+
}
40+
}
41+
42+
return ans;
43+
}
44+
45+
46+
function largestRange2(array) {
47+
const nums = {};
48+
let ans = [];
49+
let largest = 0;
50+
51+
for (let n of array) {
52+
nums[n] = true;
53+
}
54+
55+
for (let n of array) {
56+
if (nums[n] === false) {
57+
continue;
58+
}
59+
60+
nums[n] = false;
61+
62+
let currLen = 1;
63+
let left = n - 1;
64+
let right = n + 1;
65+
66+
while (nums[left] === true) {
67+
nums[left] = false;
68+
currLen++;
69+
left--;
70+
}
71+
72+
while (nums[right] === true) {
73+
nums[right] = false;
74+
currLen++;
75+
right++;
76+
}
77+
78+
if (currLen > largest) {
79+
largest = currLen;
80+
ans = [left + 1, right - 1];
81+
}
82+
}
83+
84+
return ans;
85+
}
86+
87+
function largestRangeBasic(array) {
88+
if (array.length === 1) {
89+
return [array[0], array[0]];
90+
}
91+
92+
array.sort((a, b) => a - b);
93+
94+
let largest = 0;
95+
let ans = [];
96+
let i = 0;
97+
let j = 0;
98+
99+
for (let l = array.length; j < l; j++) {
100+
if (j === l - 1 || array[j] !== array[j + 1] - 1) {
101+
let curr = j - i + 1;
102+
103+
if (curr > largest) {
104+
largest = curr;
105+
ans = [array[i], array[j]];
106+
i = j + 1;
107+
}
108+
}
109+
}
110+
111+
112+
113+
return ans;
114+
}
115+
116+
117+
console.time('Run Time');
118+
119+
assert.deepStrictEqual(largestRange([1]), [1, 1]);
120+
assert.deepStrictEqual(largestRange([1, 2]), [1, 2]);
121+
assert.deepStrictEqual(largestRange([4, 2, 1, 3]), [1, 4]);
122+
assert.deepStrictEqual(largestRange([4, 2, 1, 3, 6]), [1, 4]);
123+
assert.deepStrictEqual(largestRange([8, 4, 2, 10, 3, 6, 7, 9, 1]), [6, 10]);
124+
125+
console.timeEnd('Run Time');

0 commit comments

Comments
 (0)
Please sign in to comment.