2-SUM algorithm

21 篇文章 0 订阅
9 篇文章 0 订阅
The goal of this problem is to implement a variant of the 2-SUM algorithm (covered in the Week 6 lecture on hash table applications).

The file contains 1 million integers, both positive and negative (there might be some repetitions!).This is your array of integers, with the  ith  row of the file specifying the  ith  entry of the array.

Your task is to compute the number of target values  t  in the interval [-10000,10000] (inclusive) such that there are distinct numbers  x,y  in the input file that satisfy  x+y=t . (NOTE: ensuring distinctness requires a one-line addition to the algorithm from lecture.)

Write your numeric answer (an integer between 0 and 20001) in the space provided.

OPTIONAL CHALLENGE: If this problem is too easy for you, try implementing your own hash table for it. For example, you could compare performance under the chaining and open addressing approaches to resolving collisions.

这个不知道怎么用Hash实现,只采用了一种简单的实现:

// Reference: http://tech-wonderland.net/blog/summary-of-ksum-problems.html

#include <iostream>
#include <fstream>
#include <math.h>
#include <vector>
#include <sstream>
#include <set>

using namespace std;


bool Sum2(set<__int64> &inputs, int target)
{
	set<__int64>::iterator itBeg;
	set<__int64>::iterator itEnd;

	itBeg = inputs.begin();
	itEnd = inputs.end();
	itEnd--;

	__int64 sum;
	bool result = false;
	while(itBeg != itEnd) 
	{
		sum = *itBeg + *itEnd;
		if (sum == target)
		{
			result = true;
			break;
		}
		else if (sum < target)
		{
			itBeg++;
		}else{
			itEnd--;
		}
	}


	return result;
}


int main()
{
	ifstream infile;
	infile.open("algo1-programming_prob-2sum.txt");

	set<__int64> inputSet;
	__int64 input;
	while(!infile.eof())
	{
		infile >> input;
		inputSet.insert(input);
	}

	int sum =0;
	for (int i=-10000; i<10001; i++)
	{
		if (Sum2(inputSet, i))
		{
			sum++;
		}
	}
	
	cout << "The number is " << sum << endl;
	infile.close();
	return 0;
}



参考:

[1] Summary for leetcode 2Sum, 3Sum, 4Sum, K Sum http://tech-wonderland.net/blog/summary-of-ksum-problems.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用R语言优化并更改以下代码的变量名称set.seed(123) n <- 1000 mu1 <- c(0,4) mu2 <- c(-2,0) Sigma1 <- matrix(c(3,0,0,0.5),nr=2,nc=2) Sigma2 <- matrix(c(1,0,0,2),nr=2,nc=2) phi <- c(0.6,0.4) X <- matrix(0,nr=2,nc=n) for (i in 1:n) { if (runif(1)<=phi[1]) { X[,i] <- mvrnorm(1,mu=mu1,Sigma=Sigma1) }else{ X[,i] <- mvrnorm(1,mu=mu2,Sigma=Sigma2) } } ##initial guess for parameters mu10 <- runif(2) mu20 <- runif(2) Sigma10 <- diag(2) Sigma20 <- diag(2) phi0 <- runif(2) phi0 <- phi0/sum(phi0) ##EM algorithm k=2 prob <- matrix(rep(0,k*n),ncol = 2) weight <- matrix(rep(0,k*n),ncol = 2) phi <- phi0 mu <- matrix(c(mu10,mu20),nr=2) Sigma <- matrix(c(Sigma10,Sigma20),nr=2) #for loop,set up max iterations (200) for (step in 1:200) { for (j in 1:k) { for (i in 1:1000) { prob[i,j] <- dmvnorm(X[,i], mu[,j], Sigma[,(2*j-1):(2*j)]) weight[i,j] <- phi[j] * prob[i,j] } } row_sum <- rowSums(weight) prob <- weight/row_sum #prob denotes "wij" hear # note the parameters of the last iteration oldphi <- phi oldmu <- mu oldSigma <- Sigma # M-step:calculate the next theta by maximizing g(theta) for (j in 1:k) { sum1 <- sum(prob[, j]) sum2 <- X%*%prob[, j] phi[j] <- sum1/n mu[,j] <- sum2/sum1 sum3 <- matrix(c(0,0,0,0),nr=2) for (m in 1:n) { sum30 <- ((X[,m]-mu[,j])%*%t(X[,m]-mu[,j]))*prob[m,j] sum3 <- sum3+sum30 } Sigma[,(2*j-1):(2*j)] <- sum3/sum1 } # Set threshold: convergence is considered when the parameter obtained from the previous iteration has little change from the parameter obtained from the next iteration threshold <- 1e-5 if (sum(abs(phi - oldphi)) < threshold & sum(abs(mu - oldmu)) < threshold & sum(abs(Sigma - oldSigma)) < threshold) break #print the parameters in every iteration cat('step', step, 'phi', phi, 'mu', mu, 'Sigma', Sigma, '\n') }
最新发布
04-20

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值