首页 \ 问答 \ InterviewStreet的保龄球计数算法(Bowling Counting algorithm from InterviewStreet)

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
更新时间:2023-04-17 17:04

最满意答案

这没有任何影响 - 实际上,它对性能有害,因为你首先要复制整个表格。

优化器不维护表值参数或表变量的统计信息。 这很容易导致错误的查询计划与基数不匹配; 解决方案通常是一个中间临时表。 在任何情况下,参数嗅探都不是问题 - 表内容从不用于优化查询计划。

顺便提一下,虽然您可以将参数分配给局部变量来规避嗅探,但更灵活的选择是在特别受影响的查询中使用OPTIMIZE FORRECOMPILE提示(或者在整个存储过程中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 or RECOMPILE hints in queries that are particularly affected (or WITH 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 ,但这样做多一点激烈)。 这可以防止程序与所有内容的副本混乱。 ...
  • 我正在尝试做同样的事情。 在我们的例子中,不需要设置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的影响。 但是,只要您使用临时表,执行计划在大多数情况下都不会兑现 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)