Posts

Reverse Words in a String

Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". Clarification: What constitutes a word?  A sequence of non-space characters constitutes a word.  Could the input string contain leading or trailing spaces?  Yes. However, your reversed string should not contain leading or trailing spaces.  How about multiple spaces between two words?  Reduce them to a single space in the reversed string. Python version is super easy... Following version works, however, iterator library is not included in leetcode... Or, we can use any methods in Simplify Path to split the string. Following is an example.

[ITint5] Excel Number

http://www.itint5.com/oj/#23 The row and column indices in Excel are denoted by: A, B, C, ..., Z, AA, AB, ... AZ, BA, ... The corresponding decimal numbers are: 1, 2, 3, ..., 26, 27, 28, ... Implement two functions to transform decimal numbers to Excel numbers and vice versa. Excel number is a numeral system based 26.

Wildcard Matching

Implement wildcard pattern matching with support for '?' and '*'.  '?' Matches any single character.  '*' Matches any sequence of characters (including the empty sequence).  The matching should cover the entire input string (not partial).  The function prototype should be: bool isMatch(const char *s, const char *p)  Some examples:  isMatch("aa","a") → false  isMatch("aa","aa") → true  isMatch("aaa","aa") → false  isMatch("aa", "*") → true  isMatch("aa", "a*") → true  isMatch("ab", "?*") → true  isMatch("aab", "c*a*b") → false Use two pointers to save the position of last '*' and the position of corresponding char in s. First time when we meet '*', we assume it is an empty string. Once we do not find the match, backtrack to the last '*', and assume it matches a string of length ...

Regular Expression Matching

Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch("aa","a") → false isMatch("aa","aa") → true isMatch("aaa","aa") → false isMatch("aa", "a*") → true isMatch("aa", ".*") → true isMatch("ab", ".*") → true isMatch("aab", "c*a*b") → true If the next character of p is not *: check current *s and *p, and recursive search. If the next character of p is *: search all possibilities.

Stock Maximize

https://www.hackerrank.com/challenges/stockmax Related problems:  Best Time to Buy and Sell Stock I ,  Best Time to Buy and Sell Stock II ,  Best Time to Buy and Sell Stock III . In previous problems in Leetcode, we can only hold up to one stock. Now, we can hold multiple stocks and try to achieve the maximum profit. The idea is to scan the array from back to front. Try to find out a monotonic increasing sequence; For example, if the stock prices are [1, 7, 2, 4, 5, 9, 1, 2, 6] We start from 6, and store the increasing sequence as [9, 6]. Note that we actually store the index instead of the value. This sequence represents the points we want to sell the stocks. In other words, the optimal strategy is to continuously buy the stock if it is possible to gain profit. Once we achieve a highest point, sell all of the previous stocks. In this example, we should buy 1, 7, 2, 4, 5. Then sell all these five stocks at the point 9, get the profit of 8 + 2 + 7...

Median

https://www.hackerrank.com/challenges/median Test cases:  https://s3.amazonaws.com/hr-testcases/104/input01.txt (input00.txt to input09.txt) This is a classic problem: finding the moving median for an integer stream. The key idea is to use two ordered structures (one for the smaller half and another for the larger half). If we can make sure that these two half are size-balanced, we can easily obtain the median. Remarks: 1. If the delete operation is not required, priority_queue could be used. Here I use multiset to implement add/remove/search in O(logn). Find median is O(1). Note that instead of using set, multiset should be used for handling duplicated values. 2.  multiset<int, greater<int> > defines a multiset in descending order. In this case, the begin() iterator points to the largest number. On the other hand, multiset<int, less<int> >, which is by default, defines a multiset in ascending order. 3. I spend a lot of the times on the out...

Coin on the Table

https://www.hackerrank.com/challenges/coin-on-the-table Solution: I can only figure out the DFS solution. No idea how to do the DP.