<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4847489804169610923</id><updated>2024-09-09T14:14:56.629-07:00</updated><category term="math"/><category term="DP"/><category term="Linked List"/><category term="Tree"/><category term="DFS"/><category term="combination"/><category term="BT"/><category term="search"/><category term="Two pointers"/><category term="Sort"/><category term="string"/><category term="overflow"/><category term="BFS"/><category term="stack"/><category term="Two rounds"/><category term="in place"/><category term="bit"/><category term="graph"/><category term="heap"/><category term="c++"/><category term="deque"/><category term="divide and conquer"/><category term="matrix"/><category term="multimap"/><category term="topological sort"/><title type='text'>Techinpad</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://techinpad.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>169</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-2927973984234281044</id><published>2015-12-23T01:53:00.001-08:00</published><updated>2015-12-23T01:53:46.871-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="string"/><title type='text'>[LintCode] Add and Search Word</title><content type='html'>&lt;div class=&quot;panel&quot; style=&quot;background-color: white; border-radius: 2px; border: 1px solid rgb(235, 239, 246); box-shadow: rgba(0, 0, 0, 0.0470588) 0px 1px 1px; box-sizing: border-box; color: #717171; font-family: &#39;Open Sans&#39;, &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; margin-bottom: 20px;&quot;&gt;
&lt;div class=&quot;panel-body&quot; style=&quot;box-sizing: border-box; padding: 15px;&quot;&gt;
&lt;div style=&quot;box-sizing: border-box; margin-bottom: 10px;&quot;&gt;
Design a data structure that supports the following two operations:&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px; white-space: nowrap;&quot;&gt;addWord(word)&lt;/code&gt;&amp;nbsp;and&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px; white-space: nowrap;&quot;&gt;search(word)&lt;/code&gt;&lt;/div&gt;
&lt;div style=&quot;box-sizing: border-box; margin-bottom: 10px;&quot;&gt;
&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px; white-space: nowrap;&quot;&gt;search(word)&lt;/code&gt;&amp;nbsp;can search a literal word or a regular expression string containing only letters&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px; white-space: nowrap;&quot;&gt;a-z&lt;/code&gt;&amp;nbsp;or&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px; white-space: nowrap;&quot;&gt;.&lt;/code&gt;.&lt;/div&gt;
&lt;div style=&quot;box-sizing: border-box; margin-bottom: 10px;&quot;&gt;
A&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px; white-space: nowrap;&quot;&gt;.&lt;/code&gt;&amp;nbsp;means it can represent any one letter.&lt;/div&gt;
&lt;div style=&quot;box-sizing: border-box; margin-bottom: 10px;&quot;&gt;
&lt;b style=&quot;box-sizing: border-box;&quot;&gt;Example&lt;/b&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;m-t-lg m-b-lg&quot; style=&quot;background-color: white; box-sizing: border-box; color: #717171; font-family: &#39;Open Sans&#39;, &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; margin-bottom: 20px; margin-top: 20px;&quot;&gt;
&lt;div class=&quot;m-t-sm&quot; style=&quot;box-sizing: border-box; margin-top: 10px;&quot;&gt;
&lt;pre style=&quot;background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;&quot;&gt;&lt;code style=&quot;background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;&quot;&gt;addWord(&quot;bad&quot;)
addWord(&quot;dad&quot;)
addWord(&quot;mad&quot;)
search(&quot;pad&quot;)  // return false
search(&quot;bad&quot;)  // return true
search(&quot;.ad&quot;)  // return true
search(&quot;b..&quot;)  // return true
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;m-t-lg m-b-lg&quot; style=&quot;background-color: white; box-sizing: border-box; color: #717171; font-family: &#39;Open Sans&#39;, &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; margin-bottom: 20px; margin-top: 20px;&quot;&gt;
&lt;b style=&quot;box-sizing: border-box;&quot;&gt;Note&lt;/b&gt;&lt;div class=&quot;m-t-sm&quot; style=&quot;box-sizing: border-box; margin-top: 10px;&quot;&gt;
&lt;div style=&quot;box-sizing: border-box; margin-bottom: 10px;&quot;&gt;
You may assume that all words are consist of lowercase letters a-z.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;
class Trie {
    unordered_map&amp;lt;char,Trie*&amp;gt; _map;
    bool _end;
public:
    Trie() {
        _end = false;
    }
    void add(string &amp;amp;s,int n) {
        if(!_map.count(s[n])) {
            _map[s[n]] = new Trie();
        }
        if(n == s.length() - 1) {
            _map[s[n]]-&amp;gt;_end = true;
        } else {
            _map[s[n]]-&amp;gt;add(s, n+1);
        }
    }
    
    bool search(string &amp;amp;s, int n){
        if(s.length() -1 == n) {
            if(s[n] == &#39;.&#39;) {
                for(auto e : _map) {
                    if(e.second-&amp;gt;_end) return true;
                }
                return false;
            }
            return _map.count(s[n]) ? _map[s[n]]-&amp;gt;_end : false;
        }
        if(s[n] == &#39;.&#39;) {
            for(auto e : _map) {
                if(e.second-&amp;gt;search(s, n+1)) {
                    return true;
                }
            }
        } else {
            if(!_map.count(s[n])) return false;
            return _map[s[n]]-&amp;gt;search(s, n+1);
        }
        return false;
    }
};

class WordDictionary {
    Trie _trie;
public:
    void addWord(string word) {
        _trie.add(word, 0);
    }
    bool search(string word) {
        return _trie.search(word, 0);
    }
};

&lt;/pre&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/2927973984234281044'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/2927973984234281044'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/12/lintcode-add-and-search-word.html' title='[LintCode] Add and Search Word'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-2593687462927866532</id><published>2015-12-22T03:11:00.000-08:00</published><updated>2015-12-22T03:11:43.709-08:00</updated><title type='text'>[LintCode] Restore IP Addresses Show result </title><content type='html'>&lt;div class=&quot;panel&quot; style=&quot;background-color: white; border-radius: 2px; border: 1px solid rgb(235, 239, 246); box-shadow: rgba(0, 0, 0, 0.0470588) 0px 1px 1px; box-sizing: border-box; color: #717171; font-family: &#39;Open Sans&#39;, &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; margin-bottom: 20px;&quot;&gt;
&lt;div class=&quot;panel-body&quot; style=&quot;box-sizing: border-box; padding: 15px;&quot;&gt;
&lt;div style=&quot;box-sizing: border-box; margin-bottom: 10px;&quot;&gt;
Given a string containing only digits, restore it by returning all possible valid IP address combinations.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;m-t-lg m-b-lg&quot; style=&quot;-webkit-text-stroke-width: 0px; background-color: white; box-sizing: border-box; color: #717171; font-family: &#39;Open Sans&#39;, &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 20px; margin-bottom: 20px; margin-top: 20px; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px;&quot;&gt;
&lt;b style=&quot;box-sizing: border-box; font-weight: bold;&quot;&gt;Example&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;m-t-sm&quot; style=&quot;box-sizing: border-box; margin-top: 10px;&quot;&gt;
&lt;div style=&quot;box-sizing: border-box; margin: 0px 0px 10px;&quot;&gt;
Given&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px; white-space: nowrap;&quot;&gt;&quot;25525511135&quot;&lt;/code&gt;, return&lt;/div&gt;
&lt;pre style=&quot;background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; display: block; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 13px; line-height: 1.42857; margin: 0px 0px 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;&quot;&gt;&lt;code style=&quot;background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: inherit; padding: 0px; white-space: pre-wrap;&quot;&gt;[
  &quot;255.255.11.135&quot;,
  &quot;255.255.111.35&quot;
]
&lt;/code&gt;&lt;/pre&gt;
&lt;div style=&quot;box-sizing: border-box; margin: 0px 0px 10px;&quot;&gt;
Order does not matter.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;class Solution {
public:
    vector&amp;lt;string&amp;gt; helper(string s, int n) {
        int l = s.length();
        vector&amp;lt;string&amp;gt; res;
        
        if(3*n &amp;lt; l || n&amp;gt;l) {
            return res;
        }
        
        if(n == 1){
            if(l&amp;gt;1 &amp;amp;&amp;amp; s[0] == &#39;0&#39;) return res;
            if(stoi(s)&amp;lt;=255) {
                res.push_back(s);
            }
            return res;
        }
        
        for(int i=1;i&amp;lt;=3 &amp;amp;&amp;amp; i&amp;lt;l;i++){
            string t = s.substr(0,i);
            if((i&amp;gt;1 &amp;amp;&amp;amp; s[0] == &#39;0&#39;) || 
                (i == 3 &amp;amp;&amp;amp; stoi(t)&amp;gt;255)) break;
            vector&amp;lt;string&amp;gt; pp = helper(s.substr(i), n-1);
            for(auto e:pp) {
                res.push_back(t+&quot;.&quot;+e);
            }
        }
        return res;
    }
    
    vector&amp;lt;string&amp;gt; restoreIpAddresses(string&amp;amp; s) {
        // Write your code here
        return helper(s, 4);
    }
};
&lt;/pre&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/2593687462927866532'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/2593687462927866532'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/12/lintcode-restore-ip-addresses-show.html' title='[LintCode] Restore IP Addresses Show result '/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-2233945934867219569</id><published>2015-10-30T00:46:00.003-07:00</published><updated>2015-10-30T00:47:10.491-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="multimap"/><title type='text'>LFU Cache</title><content type='html'>Similar to LRU&amp;nbsp;http://techinpad.blogspot.com/2015/06/leetcode-lru-cache.html, implement a LFU, which is the least frequently used cache.&lt;br /&gt;
&lt;br /&gt;
Analysis: multimap should be the best data structure to use.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;

&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;
class LFUCache{
    unordered_map&amp;lt;int, multimap&amp;lt;int, pair&amp;lt;int,int&amp;gt;&amp;gt;::iterator&amp;gt; mMap;
    multimap&amp;lt;int, pair&amp;lt;int,int&amp;gt;&amp;gt; mList;
    int mCap;
public:
    LFUCache(int capacity) {
        mCap = capacity;
    }
    
    void moveToHead(int key) {
        multimap&amp;lt;int, pair&amp;lt;int,int&amp;gt;&amp;gt;::iterator it = mMap[key];
        pair&amp;lt;int, pair&amp;lt;int,int&amp;gt;&amp;gt; n(it-&amp;gt;first+1, pair&amp;lt;int,int&amp;gt;(it-&amp;gt;second.first, it-&amp;gt;second.second));
        mList.erase(it);
        mMap[key] = mList.insert(n);
    }
    
    int get(int key) {
        if(!mMap.count(key)) return -1;
        moveToHead(key);
        return mMap[key]-&amp;gt;second.second;
    }
    
    void set(int key, int value) {
        
        if(mMap.count(key)) {
            mMap[key]-&amp;gt;second.second = value;
            moveToHead(key);
            return;
        } 
        
        if(mList.size() == mCap) {
            auto it = mList.begin();
            mMap.erase(it-&amp;gt;second.first);
            mList.erase(it);
        }
        pair&amp;lt;int,pair&amp;lt;int,int&amp;gt;&amp;gt; n(1, pair&amp;lt;int,int&amp;gt;(key, value));
        mMap[key] = mList.insert(n);
        
    }
};

&lt;/pre&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/2233945934867219569'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/2233945934867219569'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/10/similar-to-lru-implement-lfu-which-is.html' title='LFU Cache'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-2696643996131558561</id><published>2015-10-27T16:44:00.003-07:00</published><updated>2015-10-27T16:44:46.356-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="BFS"/><title type='text'>[LeetCode] Serialize and Deserialize Binary Tree</title><content type='html'>Question can be found&lt;br /&gt;
https://leetcode.com/problems/serialize-and-deserialize-binary-tree/&lt;br /&gt;
&lt;br /&gt;

&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Codec {
public:

    // Encodes a tree to a single string.
    string serialize(TreeNode* root) {
        if(root == NULL) return &amp;quot;&amp;quot;;
        vector&amp;lt;string&amp;gt; v;
        queue&amp;lt;TreeNode*&amp;gt; q;
        q.push(root);
        while(!q.empty()){
            TreeNode *t = q.front();
            q.pop();
            if(t == NULL) {
                v.push_back(&amp;quot;null&amp;quot;);
                continue;
            }
            
            v.push_back(to_string(t-&amp;gt;val));
            
            q.push(t-&amp;gt;left);
            q.push(t-&amp;gt;right);
        }
        
        int l = v.size() - 1;
        while(v[l] == &amp;quot;null&amp;quot;) l--;
        
        string res = v[0];
        for(int i=1;i&amp;lt;=l;i++){
            res += &amp;quot;,&amp;quot; + v[i];
        }
        return res;
    }

    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
        if(0 == data.length()) return NULL;
        vector&amp;lt;string&amp;gt; v;
        int start = 0;
        for(int i=1;i&amp;lt;data.length();i++) {
            if(data[i] != &#39;,&#39;) continue;
            v.push_back(data.substr(start, i-start));
            start = i+1;
        }
        v.push_back(data.substr(start));
        int index = 0;
        
        TreeNode *node = new TreeNode(stoi(v[index]));
        
        queue&amp;lt;TreeNode*&amp;gt; nodesOnLevel;
        int count = v.size();
        nodesOnLevel.push(node);
        
        while(!nodesOnLevel.empty()) {
            int size = nodesOnLevel.size();
            for(int i=0;i&amp;lt;size;i++){
                
                int left = index + 1;
                if(left &amp;gt;= count) return node;
                if(v[left] != &amp;quot;null&amp;quot;) {
                    TreeNode *cur = new TreeNode(stoi(v[left]));
                    nodesOnLevel.front()-&amp;gt;left = cur;
                    nodesOnLevel.push(cur);
                }
                
                int right = index + 2;
                if(right &amp;gt;= count) return node;
                if(v[right] != &amp;quot;null&amp;quot;) {
                    TreeNode *cur = new TreeNode(stoi(v[right]));
                    nodesOnLevel.front()-&amp;gt;right = cur;
                    nodesOnLevel.push(cur);
                }
                nodesOnLevel.pop();
                index += 2;
            }
        }
        
        return node;
    }
};

// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));
&lt;/pre&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/2696643996131558561'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/2696643996131558561'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/10/leetcode-serialize-and-deserialize.html' title='[LeetCode] Serialize and Deserialize Binary Tree'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-191580945870407477</id><published>2015-10-20T23:29:00.001-07:00</published><updated>2015-10-20T23:29:49.279-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="heap"/><title type='text'>[LeetCode] Find Median from Data Stream</title><content type='html'>Question can be found&amp;nbsp;https://leetcode.com/problems/find-median-from-data-stream/&lt;br /&gt;
&lt;br /&gt;

&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;
class MedianFinder {
    priority_queue&amp;lt;int&amp;gt; up;
    priority_queue&amp;lt;int, vector&amp;lt;int&amp;gt;, greater&amp;lt;int&amp;gt; &amp;gt; down;
public:
    // Adds a number into the data structure.
    void addNum(int num) {
        if(up.empty() || num &amp;lt;= up.top()) {
            up.push(num);
        }
        else if(down.empty() || num &amp;gt;= down.top()) {
            down.push(num);
        }
        else {
            up.push(num);
        }

        if(up.size() &amp;gt;= down.size() + 2) {
            int t = up.top();
            up.pop();
            down.push(t);
        }
        else if(down.size() &amp;gt;= up.size() + 2){
            int t = down.top();
            down.pop();
            up.push(t);
        }
    }
    // Returns the median of current data stream
    double findMedian() {
        if(up.size() &amp;gt; down.size()) return up.top();
        else if(down.size() &amp;gt; up.size()) return down.top();
        return (up.top() + down.top())/2.0;
    }
};

&lt;/pre&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/191580945870407477'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/191580945870407477'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/10/leetcode-find-median-from-data-stream.html' title='[LeetCode] Find Median from Data Stream'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-7175418398835747283</id><published>2015-09-19T19:55:00.003-07:00</published><updated>2015-09-19T19:55:57.774-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="in place"/><title type='text'>[LeetCode] Move Zeroes</title><content type='html'>&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Given an array&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px;&quot;&gt;nums&lt;/code&gt;, write a function to move all&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px;&quot;&gt;0&lt;/code&gt;&#39;s to the end of it while maintaining the relative order of the non-zero elements.&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
For example, given&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px;&quot;&gt;nums = [0, 1, 0, 3, 12]&lt;/code&gt;, after calling your function,&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px;&quot;&gt;nums&lt;/code&gt;&amp;nbsp;should be&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px;&quot;&gt;[1, 3, 12, 0, 0]&lt;/code&gt;.&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
&lt;span style=&quot;box-sizing: border-box; font-weight: 700;&quot;&gt;Note&lt;/span&gt;:&lt;/div&gt;
&lt;ol style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px; margin-top: 0px;&quot;&gt;
&lt;li style=&quot;box-sizing: border-box;&quot;&gt;You must do this&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: 700;&quot;&gt;in-place&lt;/span&gt;&amp;nbsp;without making a copy of the array.&lt;/li&gt;
&lt;li style=&quot;box-sizing: border-box;&quot;&gt;Minimize the total number of operations.&lt;/li&gt;
&lt;/ol&gt;

&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;
class Solution {
public:
    void moveZeroes(vector&amp;lt;int&amp;gt;&amp;amp; nums) {
        int size = nums.size();
        if(size&amp;lt;=1) return;
        int s = 0, e = 0;
        
        while(s&amp;lt;size) {
            while( s &amp;lt; size &amp;amp;&amp;amp; nums[s] == 0) {
                s++;
            }
            if(s == size) break;
            
            swap(nums[s], nums[e]);
            e++;
            s++;
        }
        return;
    }
};
&lt;/pre&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/7175418398835747283'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/7175418398835747283'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/09/leetcode-move-zeroes.html' title='[LeetCode] Move Zeroes'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-2236751416498255171</id><published>2015-09-16T20:21:00.001-07:00</published><updated>2015-09-16T20:21:10.685-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="BT"/><title type='text'>[LeetCode] Expression Add Operators</title><content type='html'>&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Given a string that contains only digits&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px;&quot;&gt;0-9&lt;/code&gt;&amp;nbsp;and a target value, return all possibilities to add operators&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px;&quot;&gt;+&lt;/code&gt;,&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px;&quot;&gt;-&lt;/code&gt;, or&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px;&quot;&gt;*&lt;/code&gt;&amp;nbsp;between the digits so they evaluate to the target value.&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Examples:&amp;nbsp;&lt;/div&gt;
&lt;pre style=&quot;background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;&quot;&gt;&quot;123&quot;, 6 -&amp;gt; [&quot;1+2+3&quot;, &quot;1*2*3&quot;] 
&quot;232&quot;, 8 -&amp;gt; [&quot;2*3+2&quot;, &quot;2+3*2&quot;]
&quot;00&quot;, 0 -&amp;gt; [&quot;0+0&quot;, &quot;0-0&quot;, &quot;0*0&quot;]
&quot;3456237490&quot;, 9191 -&amp;gt; []
&lt;/pre&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;

&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;
class Solution {
void helper(vector&amp;lt;string&amp;gt;&amp;amp; res, string &amp;amp;nums, 
            string expr, long prev, 
            long curVal, int target) {
                
    if (nums.length() == 0 
        &amp;amp;&amp;amp; curVal == target) {
        res.push_back(expr);
        return;
    }

    for (int i = 1; i&amp;lt;=nums.length(); i++) {
        
        string firstNum = nums.substr(0, i);
        
        if(i &amp;gt; 1 &amp;amp;&amp;amp; firstNum[0] == &#39;0&#39;) {
            return;
        }
        
        long first = stol(firstNum);
        string secondNum = nums.substr(i);

        if (expr.length() == 0) {
            helper(res, secondNum, firstNum, first, first, target);
            continue;
        }
        helper(res, secondNum, expr+&amp;quot;+&amp;quot;+firstNum, 
                first, curVal + first, target);
        helper(res, secondNum, expr+&amp;quot;-&amp;quot;+firstNum,
                -first, curVal - first, target);
        helper(res, secondNum, expr+&amp;quot;*&amp;quot;+firstNum, 
                prev*first, curVal-prev+prev*first, target);
    }
}

public:
    vector&amp;lt;string&amp;gt; addOperators(string num, int target) {
        vector&amp;lt;string&amp;gt; res;
        helper(res, num, &amp;quot;&amp;quot;, 0, 0, target);
        return res;
    }
};
&lt;/pre&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/2236751416498255171'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/2236751416498255171'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/09/leetcode-expression-add-operators.html' title='[LeetCode] Expression Add Operators'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-2088904598454509716</id><published>2015-09-15T23:23:00.002-07:00</published><updated>2015-09-15T23:23:47.800-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="string"/><title type='text'>[LeetCode] Longest Common Prefix</title><content type='html'>&lt;span style=&quot;background-color: white; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;&quot;&gt;Write a function to find the longest common prefix string amongst an array of strings.&lt;/span&gt;

&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;
class Solution {
    static bool comp(string &amp;amp;s1, string &amp;amp;s2) {
        return s1.length() &amp;lt; s2.length();
    }
public:
    string longestCommonPrefix(vector&amp;lt;string&amp;gt;&amp;amp; strs) {
        if(strs.size() == 0) return &amp;quot;&amp;quot;;
        sort(strs.begin(), strs.end(), comp);
        string s = strs[0];
        int len = s.length();
        for(int i=1;i&amp;lt;strs.size();i++) {

            while(strs[i].find(s.substr(0, len)) != 0) len --;
        }
        return s.substr(0, len);
    }
};
&lt;/pre&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/2088904598454509716'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/2088904598454509716'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/09/leetcode-longest-common-prefix.html' title='[LeetCode] Longest Common Prefix'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-2332384717107059496</id><published>2015-09-15T17:15:00.002-07:00</published><updated>2015-09-15T17:15:52.729-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="divide and conquer"/><title type='text'>[LeetCode] Median of Two Sorted Arrays</title><content type='html'>&lt;span style=&quot;background-color: white; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;&quot;&gt;There are two sorted arrays&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; font-weight: 700; line-height: 30px;&quot;&gt;nums1&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;&quot;&gt;&amp;nbsp;and&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; font-weight: 700; line-height: 30px;&quot;&gt;nums2&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;&quot;&gt;&amp;nbsp;of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;

&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;
class Solution {
public:
    double findK(vector&amp;lt;int&amp;gt;&amp;amp; nums1, int s1, 
                vector&amp;lt;int&amp;gt;&amp;amp; nums2, int s2, int k)
    {
        if(s1&amp;gt;=nums1.size()) return nums2[s2+k-1];
        if(s2&amp;gt;=nums2.size()) return nums1[s1+k-1];
        
        if(k == 1) return min(nums1[s1], nums2[s2]);
        
        int n1 = s1+k/2-1 &amp;lt; nums1.size() ? nums1[s1+k/2-1] : INT_MAX;
        int n2 = s2+k/2-1 &amp;lt; nums2.size() ? nums2[s2+k/2-1] : INT_MAX;
        
        if(n1 &amp;gt; n2)
        {
            return findK(nums1, s1, nums2, s2+k/2, k- k/2);
        }
        else
        {
            return findK(nums1, s1+k/2, nums2, s2, k- k/2);
        }
    }   
    
    double findMedianSortedArrays(vector&amp;lt;int&amp;gt;&amp;amp; nums1, 
                            vector&amp;lt;int&amp;gt;&amp;amp; nums2) {
        int l1 = nums1.size(), l2 = nums2.size();
        if((l1 + l2) % 2 == 0)
        {
            return (findK(nums1, 0, nums2, 0, (l1+l2)/2) + findK(nums1, 0, nums2, 0, (l1+l2)/2+1))/2;
        }
        
        return findK(nums1, 0, nums2, 0, (l1+l2)/2+1);
    }
};
&lt;/pre&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/2332384717107059496'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/2332384717107059496'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/09/leetcode-median-of-two-sorted-arrays.html' title='[LeetCode] Median of Two Sorted Arrays'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-7257834131571823203</id><published>2015-09-13T22:59:00.001-07:00</published><updated>2015-09-13T22:59:10.742-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="DP"/><title type='text'>[LeetCode] Perfect Squares</title><content type='html'>&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Given a positive integer&amp;nbsp;&lt;i style=&quot;box-sizing: border-box;&quot;&gt;n&lt;/i&gt;, find the least number of perfect square numbers (for example,&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px;&quot;&gt;1, 4, 9, 16, ...&lt;/code&gt;) which sum to&amp;nbsp;&lt;i style=&quot;box-sizing: border-box;&quot;&gt;n&lt;/i&gt;.&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
For example, given&amp;nbsp;&lt;i style=&quot;box-sizing: border-box;&quot;&gt;n&lt;/i&gt;&amp;nbsp;=&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px;&quot;&gt;12&lt;/code&gt;, return&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px;&quot;&gt;3&lt;/code&gt;&amp;nbsp;because&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px;&quot;&gt;12 = 4 + 4 + 4&lt;/code&gt;; given&amp;nbsp;&lt;i style=&quot;box-sizing: border-box;&quot;&gt;n&lt;/i&gt;&amp;nbsp;=&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px;&quot;&gt;13&lt;/code&gt;, return&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px;&quot;&gt;2&lt;/code&gt;&amp;nbsp;because&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6px; padding: 2px 4px;&quot;&gt;13 = 4 + 9&lt;/code&gt;.&lt;/div&gt;

&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;
class Solution {
public:
    int numSquares(int n) {
        vector&amp;lt;int&amp;gt; DP(n+1,INT_MAX);
        DP[0] = 0;
        for(int i =1;i&amp;lt;=n;i++)
        {
            for(int j=1;j*j&amp;lt;=i;j++)
            {
                DP[i] = min(DP[i], DP[i-j*j]+1);
            }
        }
        return DP[n];
    }
};
&lt;/pre&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/7257834131571823203'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/7257834131571823203'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/09/leetcode-perfect-squares.html' title='[LeetCode] Perfect Squares'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-7612826187496550135</id><published>2015-09-08T11:55:00.002-07:00</published><updated>2015-09-08T11:56:15.303-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="overflow"/><category scheme="http://www.blogger.com/atom/ns#" term="search"/><title type='text'>[LeetCode] First Bad Version</title><content type='html'>The question can be found https://leetcode.com/problems/first-bad-version/&lt;br /&gt;
&lt;br /&gt;
Note: if we use m = (e+s)/2, it might cause overflow.

&lt;br /&gt;

&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;// Forward declaration of isBadVersion API.
bool isBadVersion(int version);

class Solution {
    
public:
    int firstBadVersion(int n) {
        int s = 1;
        while(s &amp;lt; n)
        {
            int m = s + (n-s)/2;
            if(isBadVersion(m)) n = m;
            else s = m+1;
        }
        return s;
    }
};
&lt;/pre&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/7612826187496550135'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/7612826187496550135'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/09/leetcode-first-bad-version.html' title='[LeetCode] First Bad Version'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-3616641597832430887</id><published>2015-09-03T23:46:00.001-07:00</published><updated>2015-09-03T23:46:39.773-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="math"/><title type='text'>[LeetCode] H-Index</title><content type='html'>This question can be found&amp;nbsp;https://leetcode.com/problems/h-index/&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;

&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;
class Solution {
public:
    int hIndex(vector&lt;int&gt;&amp; citations) {
        sort(citations.begin(), citations.end());
        int j = 0;
        for(int i=citations.size()-1;i&gt;=0;i--)
        {
            j++;
            if(citations[i] == j) return j;
            if(citations[i] &lt; j) return j-1;
        }
        return j;
    }
};
&lt;/pre&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/3616641597832430887'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/3616641597832430887'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/09/leetcode-h-index.html' title='[LeetCode] H-Index'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-8094570639466920254</id><published>2015-08-27T00:17:00.002-07:00</published><updated>2015-08-27T00:26:02.013-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="DFS"/><category scheme="http://www.blogger.com/atom/ns#" term="graph"/><category scheme="http://www.blogger.com/atom/ns#" term="topological sort"/><title type='text'>[LeetCode] Course Schedule II </title><content type='html'>&lt;div style=&quot;background-color: white; box-sizing: border-box; margin-bottom: 10px;&quot;&gt;
&lt;div style=&quot;color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;&quot;&gt;
The question can be found here:&amp;nbsp;https://leetcode.com/problems/course-schedule-ii/&lt;/div&gt;
&lt;div style=&quot;color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;&quot;&gt;
Analysis:&lt;br /&gt;
1) find the sink node which doesn&#39;t have any directed neighbors&lt;br /&gt;
2) put it at the head of the list and remove it&lt;br /&gt;
3) repeat 1) and 2)&lt;br /&gt;
See more at :https://www.cs.usfca.edu/~galles/visualization/TopoSortDFS.html&lt;/div&gt;
&lt;/div&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;class Solution {
    bool isSinkNode(unordered_map&amp;lt;int, unordered_set&amp;lt;int&amp;gt;&amp;gt;&amp;amp; map, 
                    vector&amp;lt;int&amp;gt; &amp;amp;visited, int cur, 
                    vector&amp;lt;int&amp;gt; &amp;amp; res, bool &amp;amp;cycle){
                        
        if(visited[cur] == 1) 
        {
            cycle = true;
            return false;
        }
        // 1 means its being visited in a path
        visited[cur] = 1;
        int nonSinkNeighbors = map[cur].size();
        for(auto e: map[cur])
        {
            if(visited[e] == 2|| 
                isSinkNode(map, visited, e,
                res, cycle)) nonSinkNeighbors--;
            else if(cycle) return false;
        }
        // if all the neighbors are sink node
        if(0 == nonSinkNeighbors) 
        {
            res.push_back(cur);
            //2 means this node is a sink node
            visited[cur] = 2;
        }
        // 3 means its visited in a path but not a sink node
        // we need to set it a value other than 1 to 
        else visited[cur] = 3; 
        return visited[cur] == 2;
    }
public:
    vector&amp;lt;int&amp;gt; findOrder(int numCourses, vector&amp;lt;pair&amp;lt;int, int&amp;gt;&amp;gt;&amp;amp; prerequisites) {
        vector&amp;lt;int&amp;gt; res;
        unordered_map&amp;lt;int, unordered_set&amp;lt;int&amp;gt;&amp;gt; map;
        for(auto p: prerequisites) map[p.first].insert(p.second);
        bool cycle = false;
        
        vector&amp;lt;int&amp;gt; visited(numCourses, 0);
        for(int i=0;i&amp;lt;numCourses;i++)
        {
            //0 means its not ever visited.
            if(visited[i] == 0) isSinkNode(map, visited, i, res, cycle);
        }
        if(cycle) return vector&amp;lt;int&amp;gt;();
        return res;
    }
};
&lt;/pre&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/8094570639466920254'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/8094570639466920254'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/08/leetcode-course-schedule-ii.html' title='[LeetCode] Course Schedule II '/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-897251070299834810</id><published>2015-08-24T17:01:00.001-07:00</published><updated>2015-08-24T17:01:10.540-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="math"/><title type='text'>[LeetCode] Missing Number</title><content type='html'>&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Given an array containing&amp;nbsp;&lt;i style=&quot;box-sizing: border-box;&quot;&gt;n&lt;/i&gt;&amp;nbsp;distinct numbers taken from&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;0, 1, 2, ..., n&lt;/code&gt;, find the one that is missing from the array.&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
For example,&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;Given&amp;nbsp;&lt;i style=&quot;box-sizing: border-box;&quot;&gt;nums&lt;/i&gt;&amp;nbsp;=&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;[0, 1, 3]&lt;/code&gt;&amp;nbsp;return&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;2&lt;/code&gt;.&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
&lt;span style=&quot;box-sizing: border-box; font-weight: 700;&quot;&gt;Note&lt;/span&gt;:&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;

&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;
class Solution {
public:
    int missingNumber(vector&amp;lt;int&amp;gt;&amp;amp; nums) {
        int size = nums.size();
        int total = 0;
        for(int i=0; i&amp;lt;size; i++)
        {
            total += nums[i];
        }
        return size*(size+1)/2 - total;
    }
};
&lt;/pre&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/897251070299834810'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/897251070299834810'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/08/leetcode-missing-number.html' title='[LeetCode] Missing Number'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-8836591907938636530</id><published>2015-08-16T19:27:00.001-07:00</published><updated>2015-08-16T19:27:39.037-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="string"/><title type='text'>[LeetCode] String to Integer (atoi) </title><content type='html'>&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Implement&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-family: monospace;&quot;&gt;atoi&lt;/span&gt;&amp;nbsp;to convert a string to an integer.&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
&lt;span style=&quot;box-sizing: border-box; font-weight: 700;&quot;&gt;Hint:&lt;/span&gt;&amp;nbsp;Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
&lt;span style=&quot;box-sizing: border-box; font-weight: 700;&quot;&gt;Notes:&lt;/span&gt;&amp;nbsp;It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.&lt;/div&gt;

&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;
class Solution {
public:
    int myAtoi(string str) {
        if(str.empty()) return 0;
        int i = 0;
        bool hasPlus = false;
        while(i&amp;lt;str.length() &amp;amp;&amp;amp; str[i] == &#39; &#39;) i++;
        
        if(i&amp;lt;str.length() &amp;amp;&amp;amp; str[i] == &#39;+&#39;) {
            i++;
            hasPlus = true;
        }
        int flag = 1;
        if(!hasPlus &amp;amp;&amp;amp; i &amp;lt; str.length() &amp;amp;&amp;amp; str[i] == &#39;-&#39;) 
        {
            flag = -1;
            i++;
        }

        long res = 0;
        
        while(i&amp;lt;str.length())
        {
            if(!isdigit(str[i]) ) return res*flag;
            res = res*10 + str[i] - &#39;0&#39;;
            if(res &amp;gt; INT_MAX)
            {
                if(flag == 1) return INT_MAX;
                if(res != (long)-1*INT_MIN ) return INT_MIN;
            }
            i++;
        }
        
        return res * flag;
    }
};
&lt;/pre&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/8836591907938636530'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/8836591907938636530'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/08/string-to-integer-atoi.html' title='[LeetCode] String to Integer (atoi) '/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-2410913396083455996</id><published>2015-08-16T13:39:00.002-07:00</published><updated>2015-08-16T13:40:31.065-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="DFS"/><category scheme="http://www.blogger.com/atom/ns#" term="Tree"/><title type='text'>[LeetCode] Sum Root to Leaf Numbers</title><content type='html'>&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Given a binary tree containing digits from&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;0-9&lt;/code&gt;&amp;nbsp;only, each root-to-leaf path could represent a number.&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
An example is the root-to-leaf path&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;1-&amp;gt;2-&amp;gt;3&lt;/code&gt;&amp;nbsp;which represents the number&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;123&lt;/code&gt;.&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Find the total sum of all root-to-leaf numbers.&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
For example,&lt;/div&gt;
&lt;pre style=&quot;background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 13px; line-height: 1.42857143; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;&quot;&gt;    1
   / \
  2   3
&lt;/pre&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
The root-to-leaf path&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;1-&amp;gt;2&lt;/code&gt;&amp;nbsp;represents the number&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;12&lt;/code&gt;.&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;The root-to-leaf path&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;1-&amp;gt;3&lt;/code&gt;&amp;nbsp;represents the number&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;13&lt;/code&gt;.&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Return the sum = 12 + 13 =&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;25&lt;/code&gt;.&lt;/div&gt;

&lt;b&gt;Analysis:&lt;/b&gt;
It&#39;s the same question as get &lt;a href=&quot;http://techinpad.blogspot.com/2015/08/binary-tree-paths.html&quot;&gt;Binary Tree Paths&lt;/a&gt;

&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    vector&amp;lt;string&amp;gt; getAllPaths(TreeNode * node)
    {
        vector&amp;lt;string&amp;gt; res;
        if(!node) return res;
        string cur = to_string(node-&amp;gt;val);
        if(!node-&amp;gt;left &amp;amp;&amp;amp; !node-&amp;gt;right) res.push_back(cur);
        
        if(node-&amp;gt;left)
        {
            vector&amp;lt;string&amp;gt; left = getAllPaths(node-&amp;gt;left);
            for(auto e :left) res.push_back(cur+e);
            
        }
        if(node-&amp;gt;right)
        {
            vector&amp;lt;string&amp;gt; right = getAllPaths(node-&amp;gt;right);
            for(auto e :right) res.push_back(cur+e);
        }    
        return res;
    }
public:
    int sumNumbers(TreeNode* root) {
        vector&amp;lt;string&amp;gt; paths = getAllPaths(root);
        int res = 0;
        for(auto e:paths) res += stoi(e);
        return res;
    }
};
&lt;/pre&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/2410913396083455996'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/2410913396083455996'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/08/leetcode-sum-root-to-leaf-numbers.html' title='[LeetCode] Sum Root to Leaf Numbers'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-904667271513483673</id><published>2015-08-16T13:01:00.000-07:00</published><updated>2015-08-16T13:01:09.858-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="BT"/><title type='text'>[LeetCode] Combination Sum III </title><content type='html'>&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; white-space: normal;&quot;&gt;
&lt;div style=&quot;box-sizing: border-box; margin-bottom: 10px;&quot;&gt;
Find all possible combinations of&amp;nbsp;&lt;i style=&quot;box-sizing: border-box;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: 700;&quot;&gt;k&lt;/span&gt;&lt;/i&gt;&amp;nbsp;numbers that add up to a number&amp;nbsp;&lt;i style=&quot;box-sizing: border-box;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: 700;&quot;&gt;n&lt;/span&gt;&lt;/i&gt;, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.&lt;/div&gt;
&lt;div style=&quot;box-sizing: border-box; margin-bottom: 10px;&quot;&gt;
Ensure that numbers within the set are sorted in ascending order.&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; white-space: normal;&quot;&gt;
&lt;div style=&quot;box-sizing: border-box; margin-bottom: 10px;&quot;&gt;
&lt;i style=&quot;box-sizing: border-box;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: 700;&quot;&gt;Example 1:&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;box-sizing: border-box; margin-bottom: 10px;&quot;&gt;
Input:&amp;nbsp;&lt;i style=&quot;box-sizing: border-box;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: 700;&quot;&gt;k&lt;/span&gt;&lt;/i&gt;&amp;nbsp;= 3,&amp;nbsp;&lt;i style=&quot;box-sizing: border-box;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: 700;&quot;&gt;n&lt;/span&gt;&lt;/i&gt;&amp;nbsp;= 7&lt;/div&gt;
&lt;div style=&quot;box-sizing: border-box; margin-bottom: 10px;&quot;&gt;
Output:&lt;/div&gt;
&lt;div style=&quot;box-sizing: border-box; margin-bottom: 10px;&quot;&gt;
&lt;/div&gt;
&lt;pre style=&quot;background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 13px; line-height: 1.42857143; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;&quot;&gt;[[1,2,4]]
&lt;/pre&gt;
&lt;div style=&quot;box-sizing: border-box; margin-bottom: 10px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;box-sizing: border-box; margin-bottom: 10px;&quot;&gt;
&lt;i style=&quot;box-sizing: border-box;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: 700;&quot;&gt;Example 2:&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;
&lt;div style=&quot;box-sizing: border-box; margin-bottom: 10px;&quot;&gt;
Input:&amp;nbsp;&lt;i style=&quot;box-sizing: border-box;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: 700;&quot;&gt;k&lt;/span&gt;&lt;/i&gt;&amp;nbsp;= 3,&amp;nbsp;&lt;i style=&quot;box-sizing: border-box;&quot;&gt;&lt;span style=&quot;box-sizing: border-box; font-weight: 700;&quot;&gt;n&lt;/span&gt;&lt;/i&gt;&amp;nbsp;= 9&lt;/div&gt;
&lt;div style=&quot;box-sizing: border-box; margin-bottom: 10px;&quot;&gt;
Output:&lt;/div&gt;
&lt;div style=&quot;box-sizing: border-box; margin-bottom: 10px;&quot;&gt;
&lt;/div&gt;
&lt;pre style=&quot;background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 13px; line-height: 1.42857143; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;&quot;&gt;[[1,2,6], [1,3,5], [2,3,4]]&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;
class Solution {
    void solve(vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt; &amp;amp;res, vector&amp;lt;int&amp;gt; &amp;amp;one, int k, int s, int e)
    {
        if(e == 0 &amp;amp;&amp;amp; k == 0)
        {   
            res.push_back(one);
            return;
        }

        for(int i=s;i&amp;lt;=e &amp;amp;&amp;amp; i&amp;lt;=9;i++)
        {
            one.push_back(i);
            solve(res, one, k-1,i+1, e-i);
            one.pop_back();
        }
    }
public:

    vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt; combinationSum3(int k, int n) {
        vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt; res;
        vector&amp;lt;int&amp;gt; one;
        solve(res, one, k, 1, n);
        return res;
    }
};
&lt;/pre&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/904667271513483673'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/904667271513483673'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/08/leetcode-combination-sum-iii.html' title='[LeetCode] Combination Sum III '/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-6118960836128217506</id><published>2015-08-16T12:41:00.002-07:00</published><updated>2015-08-16T12:43:06.930-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="math"/><title type='text'>[LeetCode] Add Digits</title><content type='html'>&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Given a non-negative integer&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;num&lt;/code&gt;, repeatedly add all its digits until the result has only one digit.&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
For example:&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Given&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;num = 38&lt;/code&gt;, the process is like:&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;3 + 8 = 11&lt;/code&gt;,&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;1 + 1 = 2&lt;/code&gt;. Since&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;2&lt;/code&gt;&amp;nbsp;has only one digit, return it.&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: white; color: #252525; font-family: sans-serif; font-size: 14px; line-height: 22.3999996185303px; margin-bottom: 0.5em; margin-top: 0.5em;&quot;&gt;
Please refer to this formula:&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #252525; font-family: sans-serif; font-size: 14px; line-height: 22.3999996185303px; margin-bottom: 0.5em; margin-top: 0.5em;&quot;&gt;
The formula is:&lt;/div&gt;
&lt;dl style=&quot;background-color: white; color: #252525; font-family: sans-serif; font-size: 14px; line-height: 22.3999996185303px; margin-bottom: 0.5em; margin-top: 0.2em;&quot;&gt;&lt;dd style=&quot;margin-bottom: 0.1em; margin-left: 1.6em; margin-right: 0px;&quot;&gt;&lt;img alt=&quot; \operatorname{dr}(n) = \begin{cases}0 &amp;amp; \mbox{if}\ n = 0, \\ 9 &amp;amp; \mbox{if}\ n \neq 0,\ n\ \equiv 0\pmod{9},\\ n\ {\rm mod}\ 9 &amp;amp; \mbox{if}\ n \not\equiv 0\pmod{9}.\end{cases}&quot; class=&quot;mwe-math-fallback-image-inline tex&quot; src=&quot;https://upload.wikimedia.org/math/3/6/1/361cd5046da55beef218b6d2d03a1a0a.png&quot; style=&quot;border: none; display: inline-block; vertical-align: middle;&quot; /&gt;&lt;/dd&gt;&lt;/dl&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;class Solution {
public:
    int addDigits(int num) {
        if(0 == num) return 0;
        if(num % 9 == 0) return 9;
        return num % 9;
    }
};
&lt;/pre&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/6118960836128217506'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/6118960836128217506'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/08/leetcode-add-digits.html' title='[LeetCode] Add Digits'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-1122277075873445938</id><published>2015-08-15T20:28:00.000-07:00</published><updated>2015-08-16T12:41:05.450-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="BT"/><title type='text'>[LeetCode] Binary Tree Paths</title><content type='html'>&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Given a binary tree, return all root-to-leaf paths.&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
For example, given the following binary tree:&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
&lt;/div&gt;
&lt;pre style=&quot;background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 13px; line-height: 1.42857143; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;&quot;&gt;   1
 /   \
2     3
 \
  5
&lt;/pre&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
All root-to-leaf paths are:&lt;/div&gt;
&lt;pre style=&quot;background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 13px; line-height: 1.42857143; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;&quot;&gt;[&quot;1-&amp;gt;2-&amp;gt;5&quot;, &quot;1-&amp;gt;3&quot;]&lt;/pre&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector&amp;lt;string&amp;gt; binaryTreePaths(TreeNode* root) {
        vector&amp;lt;string&amp;gt; res;
        if(!root) return res;
        string cur = to_string(root-&amp;gt;val);
        if(root-&amp;gt;left) 
        {
            vector&amp;lt;string&amp;gt; left = binaryTreePaths(root-&amp;gt;left);
            for(auto e: left)
            {
                res.push_back(cur + &quot;-&amp;gt;&quot; + e);
            }
        }
        if (root-&amp;gt;right)
        {
            vector&amp;lt;string&amp;gt; right = binaryTreePaths(root-&amp;gt;right);
            for(auto e: right)
            {
                res.push_back(cur + &quot;-&amp;gt;&quot; + e);
            }            
        }
        
        if(!root-&amp;gt;right &amp;amp;&amp;amp; !root-&amp;gt;left)
        {
            res.push_back(to_string(root-&amp;gt;val));
        }
        return res;
    }
};
&lt;/pre&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/1122277075873445938'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/1122277075873445938'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/08/binary-tree-paths.html' title='[LeetCode] Binary Tree Paths'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-4219910436874865941</id><published>2015-08-15T12:10:00.001-07:00</published><updated>2015-08-15T12:10:34.549-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="math"/><category scheme="http://www.blogger.com/atom/ns#" term="stack"/><title type='text'>[LeetCode] Basic Calculator II</title><content type='html'>&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Implement a basic calculator to evaluate a simple expression string.&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
The expression string contains only&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: 700;&quot;&gt;non-negative&lt;/span&gt;&amp;nbsp;integers,&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;+&lt;/code&gt;,&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;-&lt;/code&gt;,&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;*&lt;/code&gt;,&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;/&lt;/code&gt;&amp;nbsp;operators and empty spaces&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;&lt;/code&gt;. The integer division should truncate toward zero.&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
You may assume that the given expression is always valid.&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Some examples:&lt;/div&gt;
&lt;pre style=&quot;background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 13px; line-height: 1.42857143; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;&quot;&gt;&quot;3+2*2&quot; = 7
&quot; 3/2 &quot; = 1
&quot; 3+5 / 2 &quot; = 5
&lt;/pre&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
&lt;span style=&quot;box-sizing: border-box; font-weight: 700;&quot;&gt;Note:&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: 700;&quot;&gt;Do not&lt;/span&gt;&amp;nbsp;use the&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;eval&lt;/code&gt;&amp;nbsp;built-in library function.&lt;/div&gt;

Note: What can be done through stl stack, can also be done through stl vector.

&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;
class Solution {

public:
    int calculate(string s) {
        vector&amp;lt;int&amp;gt; numbers;
        vector&amp;lt;char&amp;gt; operators;
        int i = 0;
        while(i&amp;lt;s.length())
        {
            char e = s[i++];
            if(e == &#39; &#39;) continue;
            
            if(isdigit(e))
            {
                int j = i;
                while(j &amp;lt; s.length() &amp;amp;&amp;amp; isdigit(s[j]) ) j++;
                int num = stoi(s.substr(i-1, j-i+1));
                i = j;
                if(!operators.empty() &amp;amp;&amp;amp; 
                    (operators.back() == &#39;*&#39; 
                    || operators.back() == &#39;/&#39;)
                  )
                {
                    char op = operators.back();
                    operators.pop_back();
                    if(op == &#39;*&#39;) numbers.back() *= num;
                    else numbers.back() /= num;
                }
                else numbers.push_back(num);
                
            }
            else operators.push_back(e);
        }
        
        if(numbers.empty()) return 0;
        int a = numbers[0];
        int opIndex = 0;
        for(int i = 1;i&amp;lt;numbers.size();i++)
        {
            char op = operators[opIndex++];
            if(op == &#39;+&#39;) a += numbers[i];
            else a -= numbers[i];
        }
        return a;
    }
};
&lt;/pre&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/4219910436874865941'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/4219910436874865941'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/08/leetcode-basic-calculator-ii.html' title='[LeetCode] Basic Calculator II'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-5396647689451637588</id><published>2015-08-13T23:56:00.003-07:00</published><updated>2015-08-13T23:56:36.990-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Tree"/><title type='text'>[LeetCode] Kth Smallest Element in a BST</title><content type='html'>&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Given a binary search tree, write a function&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;kthSmallest&lt;/code&gt;&amp;nbsp;to find the&amp;nbsp;&lt;span style=&quot;box-sizing: border-box; font-weight: 700;&quot;&gt;k&lt;/span&gt;th smallest element in it.&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
&lt;span style=&quot;box-sizing: border-box; font-weight: 700;&quot;&gt;Note:&amp;nbsp;&lt;/span&gt;&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;You may assume k is always valid, 1 ≤ k ≤ BST&#39;s total elements&lt;/div&gt;

Analysis:

Note, in order traverse a BST will get a sorted array from smallest to biggest. 

&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;
    int visit(TreeNode* root, int k, int &amp;amp;i){
        if(!root)   return 0;
        int res = visit(root-&amp;gt;left,k,i);
        if(i == k)  return res;
        if(++i == k)    return root-&amp;gt;val;
        return visit(root-&amp;gt;right,k,i);
    }
    
    int kthSmallest(TreeNode* root, int k) {
        int i=0;
        return visit(root,k,i);
    }
&lt;/pre&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/5396647689451637588'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/5396647689451637588'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/08/leetcode-kth-smallest-element-in-bst.html' title='[LeetCode] Kth Smallest Element in a BST'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-4122616647806833568</id><published>2015-08-11T17:28:00.001-07:00</published><updated>2015-08-13T23:56:53.360-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Linked List"/><title type='text'>[LeetCode] Palindrome Linked List</title><content type='html'>&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Given a singly linked list, determine if it is a palindrome.&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
&lt;span style=&quot;box-sizing: border-box; font-weight: 700;&quot;&gt;Follow up:&lt;/span&gt;&lt;br style=&quot;box-sizing: border-box;&quot; /&gt;Could you do it in O(n) time and O(1) space?&lt;/div&gt;

&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */


class Solution {
    ListNode *reverse(ListNode *node)
    {
        ListNode *res = NULL;
        while(node)
        {
            ListNode *t = node-&amp;gt;next;
            node-&amp;gt;next = res;
            res = node;
            node = t;
        }
        return res;
    }
public:
    bool isPalindrome(ListNode* head) {
        if(!head) return true;
        
        ListNode *slow = head, *fast = head;
        while(fast &amp;amp;&amp;amp; fast-&amp;gt;next)
        {
            slow = slow-&amp;gt;next;
            fast = fast-&amp;gt;next-&amp;gt;next;
        }

        if(fast) slow = slow-&amp;gt;next;

        slow = reverse(slow);
        
        while(slow &amp;amp;&amp;amp; head)
        {
            if(slow-&amp;gt;val != head-&amp;gt;val) return false;
            slow = slow-&amp;gt;next;
            head = head-&amp;gt;next;
        }
        return true;
    }
};
&lt;/pre&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/4122616647806833568'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/4122616647806833568'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/08/leetcode-palindrome-linked-list.html' title='[LeetCode] Palindrome Linked List'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-1195619899934485336</id><published>2015-08-10T22:03:00.000-07:00</published><updated>2015-08-13T23:57:24.009-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="combination"/><title type='text'>[LeetCode] Different Ways to Add Parentheses</title><content type='html'>&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;+&lt;/code&gt;,&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;-&lt;/code&gt;&amp;nbsp;and&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;*&lt;/code&gt;.&lt;/div&gt;
&lt;br style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;&quot; /&gt;
&lt;span style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; font-weight: 700; line-height: 30px;&quot;&gt;Example 1&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Input:&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;&quot;2-1-1&quot;&lt;/code&gt;.&lt;/div&gt;
&lt;pre style=&quot;background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 13px; line-height: 1.42857143; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;&quot;&gt;((2-1)-1) = 0
(2-(1-1)) = 2&lt;/pre&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Output:&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;[0, 2]&lt;/code&gt;&lt;/div&gt;
&lt;br style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;&quot; /&gt;
&lt;span style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; font-weight: 700; line-height: 30px;&quot;&gt;Example 2&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Input:&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;&quot;2*3-4*5&quot;&lt;/code&gt;&lt;/div&gt;
&lt;pre style=&quot;background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 13px; line-height: 1.42857143; margin-bottom: 10px; overflow: auto; padding: 9.5px; word-break: break-all; word-wrap: break-word;&quot;&gt;(2*(3-(4*5))) = -34
((2*3)-(4*5)) = -14
((2*(3-4))*5) = -10
(2*((3-4)*5)) = -10
(((2*3)-4)*5) = 10&lt;/pre&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Output:&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;[-34, -14, -10, -10, 10]&lt;/code&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
&lt;b style=&quot;background-color: transparent;&quot;&gt;Analysis&lt;/b&gt;&lt;span style=&quot;background-color: transparent;&quot;&gt;:&lt;/span&gt;&lt;/div&gt;
Divide and conquer 

&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;class Solution {
    void calculate(vector&amp;lt;int&amp;gt;&amp;amp;res, vector&amp;lt;int&amp;gt;&amp;amp;l,
                   vector&amp;lt;int&amp;gt;&amp;amp;r, char op)
    {
        for(auto e:l)
        {
            for(auto m:r)
            {
                if(op == &#39;+&#39;) res.push_back(e+m);
                else if(op == &#39;-&#39;) res.push_back(e-m);
                else if(op == &#39;*&#39;) res.push_back(e*m);
                else if(op == &#39;/&#39;) {
                    if(m!=0)res.push_back(e/m);
                    else res.push_back(e&amp;gt;0?INT_MAX:INT_MIN);
                }
            }
        }
    }
    void solve(vector&amp;lt;int&amp;gt; &amp;amp;res,  const string &amp;amp; input)
    {
        bool found = false;
        for(int i=1;i&amp;lt;input.length();i++)
        {
            if(input[i] == &#39;+&#39; || input[i] == &#39;-&#39;  
                || input[i] == &#39;*&#39; || input[i] == &#39;/&#39;)
            {
                vector&amp;lt;int&amp;gt; l, r;
                solve(l, input.substr(0,i));
                solve(r, input.substr(i+1));
                if(!found) found = true;
                calculate(res, l, r, input[i]);
            }
        }
        
        if(!found) res.push_back(stoi(input));
    }
    
public:
    vector&amp;lt;int&amp;gt; diffWaysToCompute(string input) {
        vector&amp;lt;int&amp;gt; res;
        solve(res, input);
        return res;
    }
};
&lt;/pre&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/1195619899934485336'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/1195619899934485336'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/08/different-ways-to-add-parentheses.html' title='[LeetCode] Different Ways to Add Parentheses'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-3491848449590548834</id><published>2015-06-25T13:21:00.002-07:00</published><updated>2015-06-25T13:22:28.319-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="overflow"/><title type='text'>[LeetCode] Summary Ranges</title><content type='html'>&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Given a sorted integer array without duplicates, return the summary of its ranges.&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
For example, given&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;[0,1,2,4,5,7]&lt;/code&gt;, return&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;[&quot;0-&amp;gt;2&quot;,&quot;4-&amp;gt;5&quot;,&quot;7&quot;].&lt;/code&gt;&lt;/div&gt;

&lt;b&gt;Note: Overflow.
&lt;/b&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;class Solution {
    string getRange(int start, int end)
    {
        if(start == end) return to_string(start);
        return to_string(start) + &quot;-&amp;gt;&quot; + to_string(end);
    }
public:
    vector&amp;lt;string&amp;gt; summaryRanges(vector&amp;lt;int&amp;gt;&amp;amp; nums) {
        vector&amp;lt;string&amp;gt; res;
        if(nums.empty()) return res;
        
        long start = nums[0];
        long end = start;
        
        for(int i=1;i&amp;lt;nums.size();i++)
        {
            if(nums[i] - end &amp;gt;1)
            {
                res.push_back(getRange(start, end));
                start = nums[i];
                end = start;
            }
            else
            {
                end = nums[i];
            }
        }
        res.push_back(getRange(start, end));
        return res;
    }
};
&lt;/pre&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/3491848449590548834'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/3491848449590548834'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/06/leetcode-summary-ranges.html' title='[LeetCode] Summary Ranges'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-4847489804169610923.post-431032906206665060</id><published>2015-06-07T18:09:00.001-07:00</published><updated>2015-06-07T18:14:57.673-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="search"/><title type='text'>[LeetCode] LRU Cache</title><content type='html'>&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;get&lt;/code&gt;&amp;nbsp;and&amp;nbsp;&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;set&lt;/code&gt;.&lt;/div&gt;
&lt;div style=&quot;background-color: white; box-sizing: border-box; color: #333333; font-family: &#39;Helvetica Neue&#39;, Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px; margin-bottom: 10px;&quot;&gt;
&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;get(key)&lt;/code&gt;&amp;nbsp;- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.&lt;br /&gt;
&lt;code style=&quot;background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: Menlo, Monaco, Consolas, &#39;Courier New&#39;, monospace; font-size: 12.6000003814697px; padding: 2px 4px;&quot;&gt;set(key, value)&lt;/code&gt;&amp;nbsp;- Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.&lt;/div&gt;
&lt;b&gt;NOTE&lt;/b&gt;:&lt;br /&gt;
In the list, we should be able to track the key in map, so that when it&#39;s reached the cap, we can delete the entry in map.

&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;class LRUCache{
    struct LRUEntry
    {
        int val,key;
        LRUEntry(int k, int v) :val(v), key(k){}
    };
    //save value
    list&amp;lt;LRUEntry&amp;gt; cache;
    
    //save key
    unordered_map&amp;lt;int, list&amp;lt;LRUEntry&amp;gt;::iterator&amp;gt; map;
    int cap;
public:
    LRUCache(int capacity) {
        cap = capacity;
    }
    void moveToHead(int key)
    {
        auto it =  map[key];
        cache.push_front(LRUEntry(key, it-&amp;gt;val));
        cache.erase(it);
        map[key] = cache.begin();
    }
    int get(int key) {
        if(map.find(key) == map.end()) return -1;
        moveToHead(key);
        return map[key]-&amp;gt;val;
    }
    void set(int key, int value) 
    {
        if(map.find(key) != map.end())
        {
            *map[key] =  LRUEntry(key, value);
            moveToHead(key);
            return;
        }
        if(cache.size() == cap)
        {
            map.erase(cache.rbegin()-&amp;gt;key);
            cache.pop_back();
        }
        cache.push_front(LRUEntry(key,value));
        map[key] = cache.begin();
    }
};
&lt;/pre&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/431032906206665060'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4847489804169610923/posts/default/431032906206665060'/><link rel='alternate' type='text/html' href='http://techinpad.blogspot.com/2015/06/leetcode-lru-cache.html' title='[LeetCode] LRU Cache'/><author><name>TechAuthor</name><uri>http://www.blogger.com/profile/16150805318776344903</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry></feed>