由买买提看人间百态

topics

全部话题 - 话题: nsum
(共0页)
w****x
发帖数: 2483
1
来自主题: JobHunting版 - 今天做了挺难的一道题, 分享一下
//Print All different Combinations of a Number as a Sum of Candidate Numbers
//Before start coding, several things to consider
//1. How to deal with duplicated numbers (sort them)
//2. What exactly is the successful ending condition
//3. What exactly is the failure ending condition
//4. For the container, pass by reference or by value
void _inner_print(int a[], int n, int nSum, vector& vecRes)
{
//the success ending condition is nSum == 0 and at the
//same time n == 0!! not nSum == ... 阅读全帖
w****x
发帖数: 2483
2
partition解法, 一点都没觉得简单:
int _inner_min_set(int a[], int n, int k)
{
if (n <= 0) return 0;
if (n == 1) return a[0] >= k ? 1 : 0;
swap(a[0], a[n/2]);
int i = 1;
int j = n-1;
while (i <= j)
{
if (a[i] < a[0])
i++;
else if (a[j] >= a[0])
j--;
else swap(a[i], a[j]);
}
swap(a[0], a[j]);
int nSum = 0;
int nLft = 0;
if (j == n-1)
{
nSum = a[j];
nLft = n-1;
}
else
{
for (... 阅读全帖
r**h
发帖数: 1288
3
来自主题: JobHunting版 - 火帖里边的一道M的题Subarray sum
题目不要求subarray是连续的吧
DFS,就是根据每个节点(取或者不取)两种情况向后搜索并记录当前的解。由于题目
里面提到数组中所有元素都是正数,因此如果当前的和已经大于sum那么就可以直接剪枝
void findSubSetSum(int *pArr, int *pSub, int nSize, int nIndex, int curSum,
int nSum){
//剪枝
if(curSum+pArr[nIndex] > nSum)
return;
//找到解
if(curSum+pArr[nIndex] == nSum){
pSub[nIndex] = 1;
printResult(pArr, pSub, nIndex);
pSub[nIndex] = 0;
return;
}
//继续向后搜索,分两种情况
if(nIndex < nSize-1){
pSub[nIndex] = 1;
findSubSetSum(pArr, pSub, nSize, nIndex+1, curSum+pArr[nIndex... 阅读全帖
w**********9
发帖数: 105
4
我这个解法也》30行,
int candy(vector &ratings) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
int sum = ratings.size();
int r=0, l=0, nsum=0;
for (int i=1; i if (ratings[i]>=ratings[i-1]) {
if (l<0) {
sum += -1*(nsum-l);
if (r+l<0) sum += -1*(r+l);
l=0;
nsum = 0;
r= (rating... 阅读全帖
w****x
发帖数: 2483
5
来自主题: JobHunting版 - [案例]常见题一定要零失误拿下
int BiggestSubArry(int a[], int n)
{
assert(a && n > 0);
int nSum = INT_MIN;
int nTmp = INT_MIN;
for (int i = 0; i < n; i++)
{
if (nTmp < 0)
nTmp = max(nTmp, a[i]);
else
nTmp = nTmp + a[i] <= 0 ? 0 : nTmp + a[i];
nSum = max(nTmp, nSum);
}
return nSum;
}
说实话, 真够绕的, 别看就这几行
g******h
发帖数: 266
6
来自主题: Statistics版 - 如何用SAS Macro来计算这个公式?
想用SAS计算一个likelihood公式,是人工选Box-Cox最佳lambda用的(intentionally
不用transreg procedure)。lambda应改是个循环变量。
Likelihood(lambda)=-n/2[1/nSum(X^lambda-mean(X^lambda)^2)]+(lambda-1)sum(lnX)
我不太知道怎么样把计算出来的多个变量统计数值放到macro variable中,然后循环调
用。对macro也不是很熟。 我的数据不是单变量。有X1,X2两个变量。要分别对这两个
变量找最佳lambda。我想从-5to5 with step 0.05 for trying out the best lambda.
哪位高手对macro熟的给个指导。非常感谢。
部分数据如下:
Obs X1 X2
1 47.4 2.05
2 35.8 1.02
3 32.9 2.53
4 1508.5 1.23
5 1217.4
(共0页)