## Before and After Puzzle

Given a list of phrases, generate a list of Before and After puzzles. A phrase is a string that consists of lowercase English letters and spaces only. No space appears in the start or the end of a phrase. There are no consecutive spaces in a phrase. Before and After puzzles are phrases that are formed by merging two phrases where the last word of the first phrase is the same as the first word of the second phrase.

by lek tin in "algorithm" access_time 2-min read

## Maximum Product of Three Numbers

Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4] Output: 24 Note: The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000]. Multiplication of any three numbers in the input won’t exceed the range of 32-bit signed integer. Solution class Solution(object): def maximumProduct(self, nums): """ :type nums: List[int] :rtype: int """ min_1, min_2 = float("inf"), float("inf") max_1, max_2, max_3 = float("-inf"), float("-inf"), float("-inf") for num in nums: if num < min_1: min_2 = min_1 min_1 = num elif num < min_2: min_2 = num if num > max_1: max_3 = max_2 max_2 = max_1 max_1 = num elif num > max_2: max_3 = max_2 max_2 = num elif num > max_3: max_3 = num return max(min_1 * min_2 * max_1, max_1 * max_2 * max_3)

by lek tin in "algorithm" access_time 1-min read

## Prefix Postfix Conversion

Prefix : An expression is called the prefix expression if the operator appears in the expression before the operands. Simply of the form (operator operand1 operand2). Example : *+AB-CD (Infix : (A+B) * (C-D) ) Postfix: An expression is called the postfix expression if the operator appears in the expression after the operands. Simply of the form (operand1 operand2 operator). Example : AB+CD-* (Infix : (A+B * (C-D) )

by lek tin in "algorithm" access_time 2-min read

## Maximum Value Array M Range Increment Operations

Consider an array of size n with all initial values as 0, we need to perform following m range increment operations. increment(a, b, k) : Increment values from a to b by k. After m operations, we need to calculate the maximum of the values in the array. Example 1: Input : n = 5 m = 3 a = 0, b = 1, k = 100 a = 1, b = 4, k = 100 a = 2, b = 3, k = 100 Output : 200 Explanation: Initially array = {0, 0, 0, 0, 0} After first operation: array = {100, 100, 0, 0, 0} After second operation: array = {100, 200, 100, 100, 100} After third operation: array = {100, 200, 200, 200, 100} Maximum element after m operations is 200.

by lek tin in "algorithm" access_time 2-min read

## Consecutive Numbers Sum

Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? Example 1: Input: 5 Output: 2 Explanation: 5 = 5 = 2 + 3 Example 2: Input: 9 Output: 3 Explanation: 9 = 9 = 4 + 5 = 2 + 3 + 4 Example 3: Input: 15 Output: 4 Explanation: 15 = 15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5 Note 1 <= N <= 10 ^ 9.

by lek tin in "algorithm" access_time 1-min read

## Beautiful Subarrays

Find the distinct subarrays with m odd numbers Example 1: Input : arr = {2, 5, 6, 9}, m = 2 Output : 2 Explanation: subarrays are [2, 5, 6, 9] and [5, 6, 9] Example 2: Input : arr = {2, 2, 5, 6, 9, 2, 11}, m = 2 Output : 8 Explanation: subarrays are [2, 2, 5, 6, 9], [2, 5, 6, 9], [5, 6, 9], [2, 2, 5, 6, 9, 2], [2, 5, 6, 9, 2], [5, 6, 9, 2], [6, 9, 2, 11] and [9, 2, 11] Solution python

by lek tin in "algorithm" access_time 1-min read

## Intersection of Three Sorted Arrays

Given three integer arrays arr1, arr2 and arr3 sorted in strictly increasing order, return a sorted array of only the integers that appeared in all three arrays. Example 1: Input: arr1 = [1,2,3,4,5], arr2 = [1,2,5,7,9], arr3 = [1,3,4,5,8] Output: [1,5] Explanation: Only 1 and 5 appeared in the three arrays. Constraints: 1 <= arr1.length, arr2.length, arr3.length <= 1000 1 <= arr1[i], arr2[i], arr3[i] <= 2000 Solution Python

by lek tin in "algorithm" access_time 1-min read

Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero. You must not use any built-in BigInteger library or convert the inputs to integer directly. Solution class Solution: def addStrings(self, num1: str, num2: str) -> str: if len(num1) < len(num2): return self.

by lek tin in "algorithm" access_time 1-min read

## Reverse Words in a String

Given an input string, reverse the string word by word. Example 1: Input: "the sky is blue" Output: "blue is sky the" Example 2: Input: " hello world! " Output: "world! hello" Explanation: Your reversed string should not contain leading or trailing spaces. Example 3: Input: "a good example" Output: "example good a" Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.

by lek tin in "algorithm" access_time 1-min read

## Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h. Example: Input: 1 / \ 2 3 / \ / 4 5 6 Output: 6 Solution # Definition for a binary tree node.

by lek tin in "algorithm" access_time 1-min read