# Archive for August 2017

Problem statement Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. Solution Obviously the naive approach to this problem would be to subtract the divisor from the dividend until the dividend becomes less than the divisor, while keeping track of how many times . . . Read more

Problem statement Given a list, nums, of distinct numbers, return all possible unique permutations. Sample input [1,2,1] Sample output [[1, 2, 1], [2, 1, 1], [1, 1, 2]] Solution In another post we showed how this problem can be solved iteratively. We can use the same technique but add a . . . Read more

Problem statement Given a list, nums, of distinct numbers, return all possible permutations. Sample input [1,2,3] Sample output [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] Solution In another post we showed how this problem can be solved using a recursive technique by swapping. We stumbled upon a nice iterative . . . Read more

Problem statement Given a list, nums, of distinct numbers, return all possible permutations. Sample input [1,2,3] Sample output [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] Solution We are using a technique called permutations by swapping. Full code class Solution(object): def swap(self, nums, i, j): tmp = nums[i] nums[i] = . . . Read more

Problem statement You are given an 2D matrix / array representing an image. Rotate matrix by 90 degrees (clockwise) in-place. Solution We will work through the matrix layers. The layers of a matrix are illustrated below in different colours: There are a total of layers. We then iterate of the . . . Read more

Problem statement A time series is a series of data points indexed in time order. They are commonly used in the financial world, especially in stock markets. In this challenge you are working with a time series of stock prices. You are given historical records where the stock at time . . . Read more

Problem statement Given a string containing just the characters ( and ), find the length of the longest valid (well-formed) parentheses substring. Sample input (() Sample output 2 Sample input 1 )()()) Sample output 1 4 Solution We will solve this problem using a Stack. We are going to iterate . . . Read more

Problem statement Given an array of integers sorted in ascending order, find the starting and ending position of a given target value. If the target is not found in the array, return . Sample input [5, 7, 7, 8, 8, 10] 8 Sample output [3, 4] Solution class Solution(object): def . . . Read more

By now you may know that PHP 7 has been available for quite some time now coming with a number of improvements over version 5. It may be wise to upgrade to PHP7 when running for instance a WordPress using Amazon AWS where you are responsible for any upgrades. Also . . . Read more

Problem statement Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. Sample input [6,8,5,0,0,6,5] Sample output 13 Solution We can solve this problem in time by iterating over the array twice. Once . . . Read more