Skip to content

Commit 9955993

Browse files
committed
fix: standardize timestamp generation in algorithm implementations
1 parent f11ab5d commit 9955993

File tree

2 files changed

+45
-18
lines changed

2 files changed

+45
-18
lines changed

src/algorithms/dpAlgorithm.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@ export function generateDPSolution(n: number): {
99
result: number;
1010
timeline: AnimationTimeline[];
1111
} {
12+
// 初始化时间线
13+
const timeline: AnimationTimeline[] = [];
14+
let stepCounter = 0; // 添加步骤计数器
15+
1216
// 处理边界情况
1317
if (n <= 0) {
1418
return {
1519
result: 0,
1620
timeline: [{
17-
timestamp: 0,
21+
timestamp: stepCounter++ * 1000,
1822
description: "输入的阶数小于等于0,没有有效的爬楼梯方法",
1923
visualChanges: {
2024
nodeUpdates: [
@@ -32,7 +36,7 @@ export function generateDPSolution(n: number): {
3236
return {
3337
result: 1,
3438
timeline: [{
35-
timestamp: 0,
39+
timestamp: stepCounter++ * 1000,
3640
description: "只有1阶楼梯,只有1种爬法",
3741
visualChanges: {
3842
nodeUpdates: [
@@ -52,12 +56,9 @@ export function generateDPSolution(n: number): {
5256
dp[0] = 1;
5357
dp[1] = 1;
5458

55-
// 初始化时间线
56-
const timeline: AnimationTimeline[] = [];
57-
5859
// 初始化阶段的时间线
5960
timeline.push({
60-
timestamp: 0,
61+
timestamp: stepCounter++ * 1000,
6162
description: "初始化阶段,第 0 阶和第 1 阶各有 1 种爬法",
6263
visualChanges: {
6364
nodeUpdates: [
@@ -77,7 +78,7 @@ export function generateDPSolution(n: number): {
7778

7879
// 添加到时间线
7980
timeline.push({
80-
timestamp: i * 1000, // 时间戳按步骤递增
81+
timestamp: stepCounter++ * 1000,
8182
description: `计算第 ${i} 阶的爬法数量`,
8283
visualChanges: {
8384
nodeUpdates: [
@@ -99,7 +100,7 @@ export function generateDPSolution(n: number): {
99100

100101
// 滚动数组优化阶段
101102
timeline.push({
102-
timestamp: (n + 1) * 1000,
103+
timestamp: stepCounter++ * 1000,
103104
description: "通过滚动数组,我们将空间复杂度从 O(n) 优化到 O(1)",
104105
visualChanges: {
105106
nodeUpdates: [],

src/algorithms/formulaAlgorithm.ts

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,42 @@ export function generateFormulaSolution(n: number): {
99
result: number;
1010
timeline: AnimationTimeline[];
1111
} {
12-
// 处理边界情况
13-
if (n <= 0) return { result: 0, timeline: [] };
14-
if (n === 1) return { result: 1, timeline: [] };
15-
1612
// 初始化时间线
1713
const timeline: AnimationTimeline[] = [];
14+
let stepCounter = 0;
15+
16+
// 处理边界情况
17+
if (n <= 0) {
18+
timeline.push({
19+
timestamp: stepCounter++ * 1000,
20+
description: "输入的阶数小于等于0,没有有效的爬楼梯方法",
21+
visualChanges: {
22+
nodeUpdates: [],
23+
matrixUpdates: [],
24+
formulaUpdate: "n <= 0,返回0"
25+
},
26+
interactionPoints: []
27+
});
28+
return { result: 0, timeline };
29+
}
30+
31+
if (n === 1) {
32+
timeline.push({
33+
timestamp: stepCounter++ * 1000,
34+
description: "只有1阶楼梯,只有1种爬法",
35+
visualChanges: {
36+
nodeUpdates: [],
37+
matrixUpdates: [],
38+
formulaUpdate: "n = 1,返回1"
39+
},
40+
interactionPoints: []
41+
});
42+
return { result: 1, timeline };
43+
}
1844

1945
// 特征方程推导阶段
2046
timeline.push({
21-
timestamp: 0,
47+
timestamp: stepCounter++ * 1000,
2248
description: "特征方程推导",
2349
visualChanges: {
2450
nodeUpdates: [],
@@ -33,7 +59,7 @@ export function generateFormulaSolution(n: number): {
3359
const psi = (1 - Math.sqrt(5)) / 2;
3460

3561
timeline.push({
36-
timestamp: 1000,
62+
timestamp: stepCounter++ * 1000,
3763
description: "求解特征方程",
3864
visualChanges: {
3965
nodeUpdates: [],
@@ -45,7 +71,7 @@ export function generateFormulaSolution(n: number): {
4571

4672
// 通项公式推导阶段
4773
timeline.push({
48-
timestamp: 2000,
74+
timestamp: stepCounter++ * 1000,
4975
description: "通项公式推导",
5076
visualChanges: {
5177
nodeUpdates: [],
@@ -57,7 +83,7 @@ export function generateFormulaSolution(n: number): {
5783

5884
// 确定系数阶段
5985
timeline.push({
60-
timestamp: 3000,
86+
timestamp: stepCounter++ * 1000,
6187
description: "确定系数",
6288
visualChanges: {
6389
nodeUpdates: [],
@@ -69,7 +95,7 @@ export function generateFormulaSolution(n: number): {
6995

7096
// 最终公式阶段
7197
timeline.push({
72-
timestamp: 4000,
98+
timestamp: stepCounter++ * 1000,
7399
description: "最终公式",
74100
visualChanges: {
75101
nodeUpdates: [],
@@ -82,7 +108,7 @@ export function generateFormulaSolution(n: number): {
82108
// 最终结果计算阶段
83109
const result = calculateBinetFormula(n);
84110
timeline.push({
85-
timestamp: 5000,
111+
timestamp: stepCounter++ * 1000,
86112
description: "计算结果",
87113
visualChanges: {
88114
nodeUpdates: [],

0 commit comments

Comments
 (0)