zoukankan      html  css  js  c++  java
  • TwoSum / Three Sum

    Let's begin with a naive method.


    We first need to sort the array A[n]. And we want to solve the problem by iterating through A from beginning and ending. Then, if the sum is less than the target, we move the leading pointer to next right. When the sum is larger than target, we move the ending pointer to next left. The workflow of finding a, b such that $$a + b = target$$ as flows:

    vector<vector<int> > res;
    
    //we access the array from start point and end point
    int* begin = A;
    int* end = A + n - 1;
    
    while(begin < end){
        if(begin + *end < target)//it means we need to increase the sum
            begin += begin; 
        
        if(begin + *end > target)//it means we need to decrease the sum
            end -= end;
            
        if(begin + *end == target){
            begin += begin;
            end -= end;
            res.push_back({*begin, *end});
            
            // there may be some other combinations
            ++begin;
            --end;
        }
    }
    

    Running Time:

    • $O(n*log{n})$ for sorting.
    • $O(n)$ for accessing through the array

     

     In fact, there're some directly optimizations. When we move the pointer begin and end, it will stay the same status if $$ *(new begin) == *begin $$, or $$ *(new end) == *end$$. Thus, we can move the pointers until it reaches the first different value.

    ++begin;
    while(begin < length && num[begin] == num[begin-1])
        ++begin;
    

     and

    --end;
    while(end > 0 && num[end+1] == num[end])
        --end;
    

    Assume we have m same *begin, n same *end, we will reduce the running time of iterating moving points from $O(m*n)$ to $O(m+n)$.

     

    Pay attention the above analysis and optimization are only useful when we find valid combination.

    • When $*begin + *end == target$. In this case, we need to move both begin and end. Thus we reduce running time from $O(m*n)$ to $O(m+n)$.
    • When $*begin + *end < target$, we only do m times ++begin. And when we get different begin, we stop. Without the optimization, the loop process is the same. So in this case, we only move the begin. The running time is always $O(m)$.
    • When $*begin + *end > target$, we have the same deduction. In this case, we only move end. The running time is always $O(n)$.

    The Three Sum problem is based on the Two Sum problem above. In the Three Sum prolem, the direct optimization talked above is very important.

    If we don't need to implement the three sum problem, we can use the hash table to get $O(n)$ running time.

  • 相关阅读:
    MoSQL
    Open Search Server 1.4 Beta3 发布
    NxWidgets 1.5 发布,NuttX的GUI开发包
    segatex 7.900 发布,SELinux 策略编辑器
    MySQL Connector/ODBC 5.2.4 发布
    Phing 2.5 发布,PHP 项目构建工具
    SwingX 1.6.5 发布,GUI 工具包
    XWiki 4.4.1 发布,Java 的 Wiki 引擎
    流言终结者——C语言内存管理
    Hudson 3.0 正式版发布,持续集成引擎
  • 原文地址:https://www.cnblogs.com/kid551/p/4113610.html
Copyright © 2011-2022 走看看