Algorithms—279.Perfect Squares

本文探讨了使用动态规划解决特定问题的过程,并通过优化算法提高了效率。重点在于识别并改进了原有方法中存在的瓶颈,通过引入特定策略减少了计算时间,最终实现了311毫秒的性能提升。此外,文章还提出了在最小值处理中利用前次测试结果的策略,尽管尝试将其融入动态规划框架,但考虑到可能的复杂性并未实施。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:动态规划,然后发现超时,思考了下原因,估计是每次尝试的时候都要尝试1进去,然后1显示在大树的时候不是最快的,于是换了一个测试方法,顺带优化了下算法,311ms

又想了下,关于最小值处理的时候应该带入上一次测试的平方数,下一次处理的平方数不能大于上一次处理的平方数,不过这样不利于冬天规划,所以还是算了。

public class Solution {
	Map<Integer, Integer> map=new HashMap<Integer, Integer>();
    public int numSquares(int n) {
    	if (n<4) {
			return n;
		}
        if (map.get(n)==null) {
			int s=(int) Math.sqrt(n);
			if (s*s==n) {
				return 1;
			}
			int ans=numSquares(n-s*s);
			//凭感觉处理了一下测试的最小值
			for (int i = s-1; i >=Math.max(s/2, 1); i--) {
				int t=numSquares(n-i*i);
				if (t<ans) {
					ans=t;
				}
			}
			map.put(n, 1+ans);
		}
        return map.get(n);
    }
}




function [P,Uinit,output] = cp_als(X,R,varargin) %CP_ALS Compute a CP decomposition of any type of tensor. % % P = CP_ALS(X,R) computes an estimate of the best rank-R % CP model of a tensor X using an alternating least-squares % algorithm. The input X can be a tensor, sptensor, ktensor, or % ttensor. The result P is a ktensor. % % P = CP_ALS(X,R,'param',value,...) specifies optional parameters and % values. Valid parameters and their default values are: % 'tol' - Tolerance on difference in fit {1.0e-4} % 'maxiters' - Maximum number of iterations {50} % 'dimorder' - Order to loop through dimensions {1:ndims(A)} % 'init' - Initial guess [{'random'}|'nvecs'|cell array] % 'printitn' - Print fit every n iterations; 0 for no printing {1} % % [P,U0] = CP_ALS(...) also returns the initial guess. % % [P,U0,out] = CP_ALS(...) also returns additional output that contains % the input parameters. % % Note: The "fit" is defined as 1 - norm(X-full(P))/norm(X) and is % loosely the proportion of the data described by the CP model, i.e., a % fit of 1 is perfect. % % NOTE: Updated in various minor ways per work of Phan Anh Huy. See Anh % Huy Phan, Petr Tichavsk�, Andrzej Cichocki, On Fast Computation of % Gradients for CANDECOMP/PARAFAC Algorithms, arXiv:1204.1586, 2012. % % Examples: % X = sptenrand([5 4 3], 10); % P = cp_als(X,2); % P = cp_als(X,2,'dimorder',[3 2 1]); % P = cp_als(X,2,'dimorder',[3 2 1],'init','nvecs'); % U0 = {rand(5,2),rand(4,2),[]}; %<-- Initial guess for factors of P % [P,U0,out] = cp_als(X,2,'dimorder',[3 2 1],'init',U0); % P = cp_als(X,2,out.params); %<-- Same params as previous run % % See also KTENSOR, TENSOR, SPTENSOR, TTENSOR. % %MATLAB Tensor Toolbox. %Copyright 2015, Sandia Corporation. % This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others. % http://www.sandia.gov/~tgkolda/TensorToolbox. % Copyright (2015) Sandia Corporation. Under the terms of Contract % DE-AC04-9
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值