我发现这个挑战问题说明如下:
假设XY平面上有n个矩形.编写一个程序来计算可以与在该平面上绘制的单条直线交叉的最大可能矩形数.
我一直在集思广益,但找不到任何解决方案.也许在某个阶段,我们使用动态编程步骤,但无法弄清楚如何开始.
algorithm geometry dynamic-programming computational-geometry
在字符串x
中查找包含另一个字符串的所有字符的最小窗口宽度y
.例如:
String x = "coobdafceeaxab"
String y = "abc"
Run Code Online (Sandbox Code Playgroud)
答案应该是5,因为x
其中包含所有三个字母的最短子字符串y
是"bdafc".
我可以想到一个复杂的天真解决方案O(n^2 * log(m))
,在哪里n = len(x)
和m = len(y)
.有谁能建议更好的解决方案?谢谢.
更新:现在想起来,如果我将我的设置更改为tr1::unordered_map
,那么我可以将复杂性降低到O(n^2)
,因为插入和删除都应该是O(1)
.
我正在尝试为此问题制作代码:
(来源:https://www.codewars.com/kata/insane-coloured-triangles/train/c)
彩色三角形由一排颜色创建,每一种颜色都是红色,绿色或蓝色.通过考虑前一行中的两种触摸颜色,生成连续的行,每行包含比最后一种颜色少的颜色.如果这些颜色相同,则在新行中使用相同的颜色.如果它们不同,则在新行中使用缺少的颜色.这将持续到最后一行,只生成一种颜色.
例如,不同的可能性是:
Run Code Online (Sandbox Code Playgroud)Colour here: G G B G R G B R Becomes colour here: G R B G With a bigger example: R R G B R G B B R B R G B R B G G B R G G G R G B G B B R R B G R R B G
您将获得三角形的第一行作为字符串,并且您的工作是返回最终颜色,该颜色将作为字符串显示在底行中.在上面的例子的情况下,你会被给予
'RRGBRGBB'
,你应该返回'G'
.约束:
Run Code Online (Sandbox Code Playgroud)1 <= length(row) <= 10 ** 5
输入字符串只包含大写字母'
B', …
我正在寻找一个函数来计算具有两个坐标 (x, y) 和线段的 numpy 点数组之间的欧几里得距离。我的目标是在 0.01 秒内获得线段和 10k 点的结果。
我已经找到了一个单点函数。但是运行 for 循环非常低效。
我还发现了这个计算到无限线距离的函数:
def line_dists(points, start, end):
if np.all(start == end):
return np.linalg.norm(points - start, axis=1)
vec = end - start
cross = np.cross(vec, start - points)
return np.divide(abs(cross), np.linalg.norm(vec))
Run Code Online (Sandbox Code Playgroud)
它非常有效,我想对有界线采用类似的方法。
感谢您的帮助。
我正在尝试使用JavaScript(无引擎)制作一个小游戏,并且希望摆脱基于帧的动画。
我成功添加了水平运动的增量时间(以60或144fps正常工作)。
但是我不能让它与跳跃一起工作,高度(或力量)并不总是相同的,我也不知道为什么。
我已经尝试过了(仍然有完全相同的问题):
update()
:x += Math.round(dx * dt)
Date.now()
为performance.now()
DeltaY
我做了一个简化的示例,其中包含2种跳跃类型,高度锁定跳跃和常规跳跃(IDK称为它)。两者都有相同的问题。
const canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d'),
canvas2 = document.getElementById('canvas2'),
ctx2 = canvas2.getContext('2d');
// CLASS PLAYER ------------------------
class Actor {
constructor(color, ctx, j) {
this.c = ctx
this.w = 20
this.h = 40
this.x = canvas.width /2 - this.w/2
this.y = canvas.height/2 - this.h/2
this.color = color
// Delta
this.dy = 0
// Movement
this.gravity = 25/1000
this.maxSpeed = 600/1000 …
Run Code Online (Sandbox Code Playgroud)该问题的时间复杂度不同于所提出的类似问题。这是Zauba开发人员招聘挑战(活动在一个月前结束)的一个问题:
f(0) = p
f(1) = q
f(2) = r
for n > 2
f(n) = a*f(n-1) + b*f(n-2) + c*f(n-3) + g(n)
where g(n) = n*n*(n+1)
Run Code Online (Sandbox Code Playgroud)
p, q, r, a, b, c, n
给出。n
可以和一样大10^18
。
在上面的链接中,没有指定时间复杂度,并且我已经在中解决了此问题O(n)
,伪代码在下面(只是一种方法,所有可能的边界和边缘情况都在比赛中处理)。
if(n == 0) return p;
if(n == 1) return q;
if(n == 2) return r;
for(long i=3;i<=n;i++){
now = a*r + b*q + c*p + i*i*(i+1);
p = q; q = r; r = now;
}
Run Code Online (Sandbox Code Playgroud)
请注意,我 …
我有一个屏幕,我想根据角度计算下一个x和y。第一个足迹是从第1步开始的示例。如何计算下一个足迹,在该足迹上我要增加120,而旁边的足迹需要编织进出约60。
请记住,起点可以是x = 100,y = 100,角度为180,因此足迹必须沿着y轴上移。
我尝试了以下Javascript,但足迹似乎很困惑:
this.startingFootPrintX = Math.floor(Math.random() * 1000) + 20; //Random number between 20 and 1000
this.startingFootPrintY = Math.floor(Math.random() * 560) + 20; //Random number between 20 and 560
this.startingAngle = Math.floor(Math.random() * 340) + 20; //Random number between 20 and 340
startFootSteps();
startFootSteps(){
console.log(this.startingFootPrintX);
console.log(this.startingFootPrintY);
this.startingFootPrintX = Math.round(Math.cos(this.startingAngle * Math.PI / 180) * 120 + this.startingFootPrintX);
this.startingFootPrintY = Math.round(Math.sin(this.startingAngle * Math.PI / 180) * 60 + this.startingFootPrintY);
setInterval(function () {
startFootSteps();
}, 3000);
}
Run Code Online (Sandbox Code Playgroud) geometry ×3
math ×3
algorithm ×2
javascript ×2
python ×2
c ×1
canvas ×1
factorial ×1
java ×1
numpy ×1
physics ×1
probability ×1
recurrence ×1
rotation ×1
trigonometry ×1