To make sure your calendar, event reminders, and other features are always
correct, please tell us your time zone (and other details) using the
drop-down menus below:
Set Date/Time format:
In 12 Hour format the hours will be displayed as 1 through 12 with “a.m.” and “p.m.”
displayed after the time (ex. 1:00p.m.). In 24 hour format the hours will be displayed as 00 through 23 (ex. 13:00).
You can always change your time zone by going to your Account Settings.
Use the dropdown menu to view the events in another time zone. The primary time zone will be displayed in parentheses.
Use the dropdown menu to view the events in another time zone. The primary time zone will be displayed in parentheses.
Visiting Rany Milton(username: itsrandy)
Tag
Please wait...
Select a Color
Manage Applications
Check the items that you want displayed. Uncheck all to hide the section.
Calendars
Files
Addresses
To Dos
Discussions
Photos
Bookmarks
The “Switch Navigator” button will no longer be available after February 14, 2017.
Please learn more about how to use the new Navigator by clicking this link.
01.DS.02.07. Stock Buy Sell to Maximize Profit.html -- 1.6 meg
Stock Buy Sell to Maximize Profit - Coderust: Hacking the Coding Interview
We’re given an array of daily stock prices (integers for simplicity). Return the buying and selling prices for making the maximum profit.
The values in the array represent the cost of stock each day. As we can buy and sell the stock only once, we need to find the best buy and sell prices that maximize profit (or minimized loss) over a given span of time.
We need to maximize the profit from a single buy and sell. If we can’t make any profit, we’ll try to minimize the loss.
With runtime complexity of O(n2)O(n^2)O(n2), a naive solution is to find the maximum gain between each element and its succeeding elements.
There is a tricky linear solution to this problem that requires that we maintaincurrent_buy_price (which is the smallest number seen so far), current_profit, and global_profit as we iterate through the entire array of stock prices. At each iteration, we compare the current_profit with the global_profit, and update the global_profit accordingly.
The basic algorithm is as follows:
current profit = INT_MIN current buy = stock_prices[0] global sell = stock_prices[1] global profit = global sell - current buy for i = 1 to stock_prices.length: current profit = stock_prices[i] - current buy if current profit is greater than global profit then update global profit to current profit and update global sell to stock_prices[i] if stock_prices[i] is less than current buy then update current buy to stock_prices[i] return global profit and global sell
Let’s run through a visual representation of the pseudocode:
Created with Fabric.js 3.6.6125919Initial state'current_buy' is set to first element of the array.
1 of 4
Created with Fabric.js 3.6.6125919current_buy is updated to 5 as it is smaller than 12 and current_profit is updated to -7 as it is greater than INT_MIN
1 of 4
Created with Fabric.js 3.6.6125919current_profit becomes 4 and global_profit is updated as well as global_sell. Note that current_buy remains the same.
1 of 4
Created with Fabric.js 3.6.6125919current_profit becomes 14 and global_profit is updated as well as global_sell. Note that current_buy remains the same. We return back 19 as sell price and 5 ( 19 - 14 ) as buy price.
// Return NULL when stock list has a size less than 2
if(stockNums.size()<2){
tuple<int,int> t(std::make_pair(NULL, NULL));
return t;
}
// Initializations
int currentBuy = stockNums[0];
int globalSell = stockNums[1];
// Calculating the global profit
int globalProfit = globalSell - currentBuy;
// Initializing currentProfit with minimum value
int currentProfit = INT_MIN;
// Looping over stocks to find best buy and selling price
for(int i =1; i < stockNums.size(); i++){
// Calculating the current profit
currentProfit = stockNums[i]- currentBuy;
// Current profit is greater than the global profit
#
#include <iostream>
#include <vector>
#include <tuple>
#include <climits>
#include <string>
using namespace std;
tuple<int, int> FindBuySellStockPrices(vector<int>& stockNums) {
// Return NULL when stock list has a size less than 2
Attach this document to an event, task, or address
You can attach a link to this document to an event in your Calendar, a task in your To Do list or an Address. Check the boxes below for the data you want to
bring into the event’s or task’s description, and then click “Select text to copy” to have the next event or task you create or edit have the document text and link.