-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrogJump.cs
More file actions
70 lines (59 loc) · 2.11 KB
/
FrogJump.cs
File metadata and controls
70 lines (59 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LeetCode.BackTrack
{
// Tag: Google
[TestClass]
public class FrogJumpBackTracking
{
public bool FrogJump(int[] stones)
{
HashSet<int> hash = new HashSet<int>();
stones.ToList().ForEach(stone => hash.Add(stone));
hash.Remove(stones[0]);
Stack<int> position = new Stack<int>();
Stack<int> jumps = new Stack<int>();
position.Push(stones[0]);
jumps.Push(0);
int positionIndex = -1;
while (position.Any())
{
positionIndex = position.Pop();
int lJump = jumps.Pop();
for(int cJump = lJump -1; cJump <= lJump +1; cJump++)
{
if (positionIndex == stones[stones.Length - 1])
{
return true;
}
int nextStonePosition = positionIndex + cJump;
if (hash.Contains(nextStonePosition) && nextStonePosition > positionIndex)
{
position.Push(nextStonePosition);
jumps.Push(cJump);
}
}
}
return false;
}
[TestMethod]
public void TestMethodFrogCanReach()
{
FrogJumpBackTracking jumpBackTracking = new FrogJumpBackTracking();
bool hasReached = jumpBackTracking.FrogJump(new []{0, 1, 3, 5, 6, 8, 12, 17});
Assert.IsTrue(hasReached, "The frog should reach the destination.");
}
[TestMethod]
public void TestMethodFrogCannotReach()
{
FrogJumpBackTracking jumpBackTracking = new FrogJumpBackTracking();
bool hasReached = jumpBackTracking.FrogJump(new[] { 1, 3, 6, 7, 11 });
Assert.IsFalse(hasReached, "The frog should not reach the destination.");
}
}
}