Created
October 26, 2021 11:47
-
-
Save keif/71b0203bfaa06b558a5af1793ca02992 to your computer and use it in GitHub Desktop.
/* The stocks of a company are being surveyed to analyze the net profit of the company over a period of several months. For an analysis parameter k, a group of k consecutive months is said to be highly profitable if the values of the stock prices are strictly increasing for those months. Given the stock prices of the company for n months and the…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function countHighlyProfitableMonths(stockPrices, k) { | |
// write your code here | |
let numOfProfitableMonths = 0 | |
let stockPriceLen = stockPrices.length | |
let profitableMonthsArr = [stockPrices[0]] | |
for (let i = 1; i < stockPriceLen; i += 1) { | |
const currStockPrice = stockPrices[i] | |
// checking current stock price against the profitable array | |
if (currStockPrice > profitableMonthsArr[profitableMonthsArr.length - 1]) { | |
if (profitableMonthsArr.length === k) { | |
// drop the first element | |
profitableMonthsArr.shift() | |
} | |
profitableMonthsArr.push(currStockPrice) | |
} else { | |
// if we need to start the array over | |
profitableMonthsArr = [currStockPrice] | |
} | |
if (profitableMonthsArr.length === k) { | |
numOfProfitableMonths += 1 | |
} | |
} | |
return numOfProfitableMonths | |
} | |
let stockPrices = [5, 3, 5, 7, 8] | |
let k = 3 | |
// let res = countHighlyProfitableMonths(stockPrices, k) | |
console.log(countHighlyProfitableMonths(stockPrices, k) === 2) | |
console.log(countHighlyProfitableMonths([1,2,3,4,5,6,7,8,9], 3) === 7) | |
console.log(countHighlyProfitableMonths([1,2,3,4,5,6,7,8,9], 6) === 4) | |
console.log(countHighlyProfitableMonths([1,2,8,45,6,37,8,9], 6) === 0) | |
console.log(countHighlyProfitableMonths([1,2,8,45,6,37,8,9], 3) === 2) | |
// console.log(res) | |
//countHighlyProfitableMonths([1,2,3,4,5,6,7,8,9], 3) //7 | |
//countHighlyProfitableMonths([1,2,3,4,5,6,7,8,9], 6) //4 | |
//countHighlyProfitableMonths([1,2,8,45,6,37,8,9], 6) //0 | |
//countHighlyProfitableMonths([1,2,8,45,6,37,8,9], 3) //2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment