InterviewStreet的保龄球计数算法(Bowling Counting algorithm from InterviewStreet)
我不知道这是不是正确的部分...但是这里有:
上周的面试比赛(Code Sprint 3)有一个叫做保龄球的问题。 (10针保龄球,N帧)。 重点是计算通过播放N帧来获得M分的方式的数量。
问题陈述在这里: http : //pastebin.com/cyeLML8U
我很确定我已经使用2维DP解决了这个问题。 但是,我得到第3个样本数据错误(1帧,25分)。 样本答案是1,但我得到6。
这是他们对样本答案的解释:
For the third case, there is only 1 way. Score a strike in the first frame, score another strike with the first extra ball, and an additional 5 with the second extra ball.
但是,你不能在第一帧(也是唯一一帧)中获得一次击球,然后在随后的额外帧中得分以下任何一项?
10 5 9 6 8 7 7 8 6 9 5 10
我无法理解为什么“1”是正确的答案....我也看过维基百科的规则。
他们的回答可能是正确的,我可能忽略了一些非常明显的事情。 谁能告诉我答案有什么问题?
I don't know if this is the right section... but here goes:
Last weeks contest on interviewstreet (Code Sprint 3) had a problem called bowling. (10 pin bowling, N frames). The point is to count the number of ways to score M points by playing N frames.
Problem Statement is here: http://pastebin.com/cyeLML8U
I'm pretty sure I've solved the problem using 2 dimensional DP. However, I get the 3rd sample data wrong (1 Frame, 25 points). The sample answer is 1, however I get 6.
This is their explanation of the sample answer:
For the third case, there is only 1 way. Score a strike in the first frame, score another strike with the first extra ball, and an additional 5 with the second extra ball.
However, can't you score a strike in the first (and only) frame, then score any of the following in the subsequent extra frames?
10 5 9 6 8 7 7 8 6 9 5 10
I can't wrap my head around why "1" is the right answer.... I've looked on wikipedia for the rules too.
Their answer is probably right, and I'm probably overlooking something REALLY obvious. Can anyone tell me what's wrong with my answer?
原文:https://stackoverflow.com/questions/13147654
最满意答案
这没有任何影响 - 实际上,它对性能有害,因为你首先要复制整个表格。
优化器不维护表值参数或表变量的统计信息。 这很容易导致错误的查询计划与基数不匹配; 解决方案通常是一个中间临时表。 在任何情况下,参数嗅探都不是问题 - 表内容从不用于优化查询计划。
顺便提一下,虽然您可以将参数分配给局部变量来规避嗅探,但更灵活的选择是在特别受影响的查询中使用
OPTIMIZE FOR
或RECOMPILE
提示(或者在整个存储过程中WITH RECOMPILE
,但这样做多一点激烈)。 这可以防止程序与所有内容的副本混乱。This has no effect whatsoever -- in fact, it's detrimental to performance because you're copying the whole table first.
The optimizer maintains no statistics for either table-valued parameters or table variables. This can easily lead to bad query plans with cardinality mismatches; the solution for that is usually an intermediate temp table. In any case, parameter sniffing won't be an issue -- the table contents are never used to optimize the query plan.
Incidentally, while you can assign the parameter to a local variable to circumvent sniffing, a more flexible option is to use the
OPTIMIZE FOR
orRECOMPILE
hints in queries that are particularly affected (orWITH RECOMPILE
on the whole stored procedure, but that's a little more drastic). This prevents cluttering the procedure with copies of everything.
相关问答
更多-
现在(n Dapper 1.26及更高版本)直接支持烘焙到dapper中的表值参数。 在存储过程的情况下,由于数据类型内置到sproc API中,所以您只需要提供一个DataTable : var data = connection.Query
(..., new { id=123, name="abc", values = someTable }, ...); 对于直接命令文本,您还有两个选项: 使用帮助方法来告诉它自定义数据类型: var data = connection. ... -
这里你唯一的参数是a.tpt_file_id = @tpt_file_id ,如果这是参数嗅探,那么案例必须是这样的,对于某些tpt_file_id,有数千(或更多)记录,并且肯定有很少(或没有)。 生产计划与测试环境不同的另一个原因是机器不同。 您通常在生产环境中拥有更多内存和更多CPU /核心,导致优化器选择不同的计划,当然如果表中的行数不相同,它当然会导致完全不同的计划。 您可以使用option (recompile)来检查这一点,以查看计划是否更改或查看计划缓存,即用于创建计划的参数值是多少。 可以 ...
-
你不能使用临时表 - 你必须使用表变量 : declare @t [MyNameSpace].[MyTypeTable] insert into @t (/*columns*/) values (/* first row */), (/* second row */) EXECUTE MyNameSpace.MyStoredProc @MyTypeTableVar = @t; (如上所示,您可以使用INSERT ... VALUES填充它,或者如果您有一个包含您关心的数据的现有表,则可以使用INSERT ...
-
添加这个: cmd.CommandType = CommandType.StoredProcedure; Add this: cmd.CommandType = CommandType.StoredProcedure;
-
这没有任何影响 - 实际上,它对性能有害,因为你首先要复制整个表格。 优化器不维护表值参数或表变量的统计信息。 这很容易导致错误的查询计划与基数不匹配; 解决方案通常是一个中间临时表。 在任何情况下,参数嗅探都不是问题 - 表内容从不用于优化查询计划。 顺便提一下,虽然您可以将参数分配给局部变量来规避嗅探,但更灵活的选择是在特别受影响的查询中使用OPTIMIZE FOR或RECOMPILE提示(或者在整个存储过程中WITH RECOMPILE ,但这样做多一点激烈)。 这可以防止程序与所有内容的副本混乱。 ...
-
在Mono 2.10中使用表值参数(SqlDbType.Structured)(Using Table-Valued Parameters (SqlDbType.Structured) in Mono 2.10)[2023-04-01]
我正在尝试做同样的事情。 在我们的例子中,不需要设置TypeName就可以使查询在MS .Net上工作,这样摆脱了Mono错误。 现在我得到以下错误: System.ArgumentOutOfRangeException: No mapping exists from SqlDbType Structured to a known DbType 我从Mono 3.0库中反编译System.Data.dll,它创建的映射不包含SqlDbType.Structured的映射。 Mono库中的SqlDbType的 ... -
看来你的主要问题是给你的存储函数一个表值参数,这在PostgreSQL中确实不受支持。 但是你可以使用一个记录数组,这基本上与更多的样板相同(可能还有一些性能影响): CREATE TYPE my_rec AS ( c1 BIGINT, c2 BIGINT ); 接着: CREATE FUNCTION my_func(almost_a_table my_rec[]) RETURNS TABLE (c1 BIGINT, c2 BIGINT) AS $$ BEGIN RETURN QUERY ...
-
一个简单的解决方法是不使用参数。 代替: SELECT * FROM YourTable WHERE UserName = @myUserName; 通过: SELECT * FROM YourTable WHERE UserName = 'PFranchise' 如果SQL Server不知道参数,它就无法嗅探它们! SQL Server将为每个查询重新编译查询计划。 关于这种方法的两点说明: 注意SQL注入 在SQL Server的更高版本中, 服务器选项“强制参数化”甚至可以嗅探没有参数的查询。 ...
-
如果没有看到完整的代码示例,很难说出问题可能是什么。 考虑一下,执行完全正常: 首先,TVP定义 CREATE TYPE [dbo].[TVPSTRING] AS TABLE( [VALUE] NVARCHAR(MAX) NULL ) 然后示例Dapper代码: using System; using System.Data; using System.Data.SqlClient; using System.Linq; using Dapper; namespace CPTVP { c ...
-
我不知道你的意思是plan is non-determinstically cached的执行计划可以兑现或不兑现。 服务器使用参数嗅探来根据参数值找出最有效的执行计划,该参数值在某些条件下使用。 粗略地说,使用参数sniffling sql-server决定使用某些索引是否有效。 您可以阅读更多相关信息,例如在此博客中 。 因此,回答您的问题,因为您在某些情况下不使用xml参数,只是将其加载到临时表中,执行计划不会受到参数sniffling的影响。 但是,只要您使用临时表,执行计划在大多数情况下都不会兑现 ...