Posts

Walking Robot Simulation

Image
Latest problem from Leetcode is this one: https://leetcode.com/problems/walking-robot-simulation/description/ Now the description of the problem doesn’t seem super accurate since in reality what the author is asking is: as the robot walks, for each step there will be an Euclidean distance from that position to the origin. Return the maximum Euclidean distance to the origin as the robot walks the course . Also, it wasn’t clear that one could have duplicated obstacle. I want to believe that many Leetcoders must have been confused and that might be the reason that there are more thumb downs than ups. I failed submission few times because of these misunderstandings: The problem is a simple traversal (some attention to details required) with usage of a hash table for a quick lookup into the obstacles. To quickly access the hash table, use the following trick to build the key: suppose that you want to build a key for index (a,b) with the following constraints: 0<=a<=N ...

Reordered Power of 2 - Medium Difficulty

Image
Hello dear friends, problem is this one:  https://leetcode.com/problems/reordered-power-of-2/description/ Starting with a positive integer  N , we reorder the digits in any order (including the original order) such that the leading digit is not zero. Return  true  if and only if we can do this in a way such that the resulting number is a power of 2. Example 1: Input: 1 Output: true Example 2: Input: 10 Output: false Example 3: Input: 16 Output: true Example 4: Input: 24 Output: false Example 5: Input: 46 Output: true This is a medium-difficulty problem, but if you pay close attention to the constraints you can solve it relatively easy. First, the upper limit is really small: 10^9. The approach that can be taken is to use a static hashtable (static so that you don't have to recompute it all the time) holding all the powers of two between 0 and 10^9. There won't be many since they grow exponentially fast. But you don't wa...

IBM Ponder This July 2018: Obscure Triplet

Image
The IBM Ponder This are fun coding/math/logic challenges that are sponsored by IBM Haifa on a monthly basis, and that's been going on for several years now. Some challenges are really obscure and complex, others can be solved with the help of some programming language. I've managed to solve three in the past - the prize is just bragging rights with your name posted on their challenge pages: http://www.research.ibm.com/haifa/ponderthis/challenges/October2016.html http://www.research.ibm.com/haifa/ponderthis/challenges/September2016.html https://www.research.ibm.com/haifa/ponderthis/challenges/October2014.html The latest one was posted on July 2018, and this is it:  http://www.research.ibm.com/haifa/ponderthis/challenges/July2018.html : " This month's challenge is based on a riddle I heard from Odelia Moshe Ostrovsky (thanks!). Let's call a triplet of natural numbers "obscure" if one cannot uniquely deduce them from their sum and product....

Peak and Pride

Image
Problem is here: https://leetcode.com/problems/peak-index-in-a-mountain-array/description/ Let's call an array  A  a  mountain  if the following properties hold: A.length >= 3 There exists some  0 < i < A.length - 1  such that  A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1] Given an array that is definitely a mountain, return any  i  such that  A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1] . Example 1: Input: [0,1,0] Output: 1 Example 2: Input: [0,2,1,0] Output: 1 If you play close attention to the problem, it actually ends up being much simpler than the whole problem statement suggests. Since the input is confirmed to be a mountain (hence you don’t need to check whether it is a mountain or not), then all you really need is to find the max element in the array! Now even if the question was to deter...

Backspace String Compare, by LeetCode

Image
Problem is labeled as easy by LeetCode, here it is:  https://leetcode.com/problems/backspace-string-compare/description/ : Given two strings  S  and  T , return if they are equal when both are typed into empty text editors.  #  means a backspace character. Example 1: Input: S = "ab#c" , T = "ad#c" Output: true Explanation : Both S and T become "ac". Example 2: Input: S = "ab##" , T = "c#d#" Output: true Explanation : Both S and T become "". Example 3: Input: S = "a##c" , T = "#a#c" Output: true Explanation : Both S and T become "c". Example 4: Input: S = "a#c" , T = "b" Output: false Explanation : S becomes "c" while T becomes "b". My solution won't be the most efficient - it will run in O(n)-time and O(n)-space. There is a way to do in O(1)-space but this is a straightforward problem to use a stack that I decided to use i...

Split Array into Fibonacci Sequence

Image
Memorial Day Problem:  https://leetcode.com/problems/split-array-into-fibonacci-sequence/description/ Given a string  S  of digits, such as  S = "123456579" , we can split it into a  Fibonacci-like sequence   [123, 456, 579]. Formally, a Fibonacci-like sequence is a list  F  of non-negative integers such that: 0 <= F[i] <= 2^31 - 1 , (that is, each integer fits a 32-bit signed integer type); F.length >= 3 ; and  F[i] + F[i+1] = F[i+2]  for all  0 <= i < F.length - 2 . Also, note that when splitting the string into pieces, each piece must not have extra leading zeroes, except if the piece is the number 0 itself. Return any Fibonacci-like sequence split from  S , or return  []  if it cannot be done. One strategy to solve the problem is to do an N^2 nested-loop thru the array (given that the array length is 200, this would cost you 40,000, which is reasonable) where the outer-loop corre...

Most Common Word, by LeetCode

Problem is this one https://leetcode.com/problems/most-common-word/description/ . String and Counting problems are (for the most part) solvable by using the string operators within your programming language (avoid reinventing the wheel, there is no need to do a lot of the parsing by hand anymore. If your programming language doesn’t support the basic string operations, time to switch languages) and hash-tables to quickly access specific substrings. This is exactly the approach that will be taken here. First a quick prep where we add the banned words to a hash-table and we also split all the words in the paragraph. Then it is just a matter of going thru the words in the paragraph, make sure they are not in the banned list, and keep track of the most common used so far. Code is below, Marcelo.                public class Solution              ...