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.

  • 相关阅读:
    电子商务测试点总结
    pc 端 测试点总结
    web测试点总结
    Monkey脚本API
    Git 命令
    配置samba的流程
    scrapy使用指南
    链接
    顺序表总结
    python数据类型——字符串类型
  • 原文地址:https://www.cnblogs.com/kid551/p/4113610.html
Copyright © 2011-2022 走看看