algorithm:

Best Time to Buy and Sell Stock III

Say you have an array for which the ithelement is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete at most two transactions. NOTE: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again). Example 1 Input: [3,3,5,0,0,3,1,4] Output: 6 Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.

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

Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. *### Example 1 Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. *### Example 2 Input: "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. *### Example 3 Input: "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

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

Min Cost Climbing Stairs

On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0, or the step with index 1. Example 1 Input: cost = [10, 15, 20] Output: 15 Explanation: Cheapest is start on cost[1], pay that cost and go to the top.

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

Reorder Data in Log Files

You have an array of logs. Each log is a space delimited string of words. For each log, the first word in each log is an alphanumeric identifier. Then, either: Each word after the identifier will consist only of lowercase letters, or; Each word after the identifier will consist only of digits. We will call these two varieties of logs letter-logs and digit-logs. It is guaranteed that each log has at least one word after its identifier.

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

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) ) Given a Prefix expression, convert it into a Postfix expression.

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