Skip to main content

Best Time to Buy and Sell Stock

You are given an array prices where prices[i] is the price of a given stock on the ith day.

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

 

Example 1:

Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.

Example 2:

Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.

 

Constraints:

  • 1 <= prices.length <= 105
  • 0 <= prices[i] <= 104
Solution: 

class Solution {
public int maxProfit(int[] prices) {
if(prices.length < 2){
return 0;
}
int profit = 0;
int buyPrice = prices[0];

for(int sellPrice : prices ){
if(sellPrice > buyPrice){
profit = Math.max(profit, sellPrice - buyPrice);
} else {
buyPrice = sellPrice;
}
}

return profit;
}
}

Comments

Popular posts from this blog

How to Setup Virtual Environment in Python with venv

A virtual environment is the most used tool by the developers to isolate the dependencies for different projects. Suppose you have two projects say porj1 and proj2 . proj1 needs the Django dependency with version 3.2 but your proj2 needs the Django dependency with version 2.2. In this situation you need a virtual environment to keep the both version on your system separately.  How to create virtual environment in python:  Decide a directory where you want to create the virtual environment. You can use your project directory or any other directory as per your wish.  Run the below command. Here` awesome_proj_env ` is the folder where virtual environment will be created. if the folder does not exists then it will be created automatically. python3 -m venv awesome_proj_env    Activate the virtual environment: On Linux/Mac OSX: source awesome_proj_env/bin/activate  On Windows: awesome_proj_env \Scripts\activate.bat Deactivate the virtual environment in Pyth...

Cloud Computing - Virtualization

Virtualization: Hosting all of a physical machine's hardware and software components independently on a single or shared hardware resource. In virtualization, we can run multiple operating systems on a single machine at the same time. In the case of dual-boot, we can run an OS at a time. Virtual Machine: VM is the set of virtual hardware devices, virtual RAM and virtual CPU that run like a traditional operating system. Virtual Server: It is also a virtual machine running as a server. It can run one or possibly more server-based application. This server may be a database or messaging or anything else. VMM (Virtual Machine Monitor): VMM consists of multiple virtual machines top of the hypervisor to handle all the VMs. Hypervisor: Hypervisor consists of multiple VMs and communicates with the real hardware to provide the resources to the available virtual machines. It controls all the physical resources on the computer. two types of the hypervisor exist as shown in the below diagram....

Run a Local LLM with Ollama and Use Apple Shortcut Automations to Auto-Reply to Messages

Apple’s Shortcut Automations allow your iPhone or Mac to react to events like receiving a message . By combining this with a locally hosted LLM (via Ollama) , you can build a private AI auto-reply system that runs entirely on your local network. In this guide, we’ll configure: A local LLM using Ollama A message-triggered Shortcut Automation Sender-based filtering Automatic AI-generated replies (within Apple’s security limits) 1. Why Use Shortcut Automations Instead of Manual Shortcuts? Automations let Shortcuts run automatically when an event occurs. Examples: When a message is received When a specific person messages you When you arrive at a location At a specific time For AI auto-replies, message-based automations are ideal. 2. Install and Run a Local LLM with Ollama Install Ollama: brew install ollama Start the server (default port): ollama serve Verify installation: ollama list 3. Pull a Lightweight Model For message replies, small models work best: ollama pull tinyllama Or for b...