Best Time to Buy and Sell Stock

How to get maximum profit by buying and selling a stock when at most one transaction is allowed?

In this problem, we have given an array of stock prices, the ith element represents the stock price on ith day.

If we are allowed to complete at most one transaction (i.e.,buy one and sell one share of the stock), design an algorithm to find the maximum profit.

Note that you cannot sell a stock before you buy one. If it’s not possible to achieve any profit return 0.

For example –

Example 1-

Input – { 9, 1, 6, 3, 7, 4}

Output – 6 (Buy on day 2 (1) and sell on day 5 (7))

Example 2-

Input – {5, 4, 3, 2, 1}

Output – 0 (It is not possible to do any transaction)

Rotting Oranges

In this tutorial, i am going to discuss a very interesting problem rotting oranges (minimum time required to rot all fresh oranges).

Given an m*n grid where each cell in the matrix can have values 0, 1 or 2.

0 represents an empty cell
1 represents cells have fresh oranges
2 represents cells have rotten oranges

Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten.

Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1.

Valid Palindrome II | Delete at Most One Character to Make it a Palindrome

In this tutorial, I am going to discuss a variation of palindrome questions valid palindrome II. In this problem, we can remove a character from a string to make it a palindrome.

Given a non-empty string, We may delete at most one character from a string. We have to write a code which checks whether we can make this string palindrome or not.

For example –

Example 1:

Input : “aba”
Output: true

This string is already palindrome. So we return true.

Example 2:

Input : “abca”
Output: true

This string is not a palindrome. But, if we delete either a character b or c from this string then the new string is a palindrome.