LintCode "Subarray Sum II"
Sliding window doesn't work. So it is a typical partial_sum base solution. As below. However if you use a balanced binary search tree, you can get O(nlgn) - like std::set<int>
class Solution {
public:
/**
* @param A an integer array
* @param start an integer
* @param end an integer
* @return the number of possible answer
*/
int subarraySumII(vector<int>& A, int start, int end) {
size_t len = A.size(); vector<int> presum(len + );
std::partial_sum(A.begin(), A.end(), presum.begin() + ); int ret = ;
for(int i = ; i <= len; i ++)
{
for(int j = ; j < i; j++)
{
int diff = presum[i] - presum[j];
if(diff<=end && diff>=start)
ret ++;
}
}
return ret;
}
};
LintCode "Subarray Sum II"的更多相关文章
- [LintCode] Subarray Sum & Subarray Sum II
Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...
- Continuous Subarray Sum II(LintCode)
Continuous Subarray Sum II Given an circular integer array (the next element of the last element i ...
- [LintCode] Continuous Subarray Sum II
Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...
- Lintcode: Subarray Sum 解题报告
Subarray Sum 原题链接:http://lintcode.com/zh-cn/problem/subarray-sum/# Given an integer array, find a su ...
- Lintcode: k Sum II
Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where the ...
- Lintcode: Subarray Sum Closest
Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first nu ...
- Subarray Sum II
Description Given an positive integer array A and an interval. Return the number of subarrays whose ...
- Continuous Subarray Sum II
Description Given an circular integer array (the next element of the last element is the first eleme ...
- LintCode Subarray Sum
For this problem we need to learn a new trick that if your start sum up all elements in an array. Wh ...
随机推荐
- 【题解】【矩阵】【DP】【Leetcode】Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- Word Ladder II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- 一步一步使用sklearn
http://kukuruku.co/hub/python/introduction-to-machine-learning-with-python-andscikit-learn Hello, %u ...
- OVS ARP Responder – Theory and Practice
Prefix In the GRE tunnels post I’ve explained how overlay networks are used for connectivity and ten ...
- pcr free library 介绍
一句话:illumina的建库方法,建库时间段,质量还好... the adapters are different in the PCR-free kit compared to the stand ...
- 在Linux上使用web2py_uwsgi_nginx搭建web服务器
本文介绍在Linux使用Python+Nginx+web2py+uWSGI搭建一个web服务器的过程. Python 2.7.11 解压安装包 tar -zxvf Python-2.7.11.tgz ...
- jq中如何阻止元素的默认行为?
阻止网页元素的默认行为: event.preventDefault(); 或者:return false;
- codeforces 192b
link: http://codeforces.com/contest/330/problem/B I think the problem is hard at first. However, whe ...
- HDU 2095 find your present (2)
HDU 2095 find your present (2) 解法一:使用set 利用set,由于只有一个为奇数次,对一个数进行查询,不在集合中则插入,在集合中则删除,最后剩下的就是结果 /* HDU ...
- POJ-1741 Tree (树上点分治)
题目大意:一棵带边权无根树,边权代表距离,求距离小于等于k的点对儿数. 题目分析:这两个点之间的路径只有两种可能,要么经过根节点,要么在一棵子树内.定义depth(i)表示点 i 到根节点的距离,be ...