Lowest Common Ancestor in a Binary Tree in C++

Problem Statement Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Definition of LCA (according to Wikipedia) The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants. For the given Binary tree, if info[key1] = 15 and […]

Best Time to Buy and Sell Stock [Interview Question]

Problem Statement [LeetCode]You are given an array prices where prices[i] is the price of a given stock on the ith day. You need 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 […]

Edit Distance – LeetCode Solution [Hard]

Problem statement: Given two strings word1 and word2 (in lowercase alphabets), return the minimum number of operations required to convert word1 to word2. You could only perform three following operations on a word: Insert a character Delete a character Replace a character Example 1: Input: word1 = “horse”, word2 = “ros” Output: 3 Explanation: horse -> rorse (replace […]

Given a sorted Alien dictionary, find order of characters

In an alien language, surprisingly, they also use English lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters. Given a sequence of words written in the alien language, find the order of the alphabet. Problem statement: GeeksforGeeks Problem Link Example 1: Input: words = [“hello”,”leetcode”], order = “hlabcdefgijkmnopqrstuvwxyz”Explanation: […]