Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhao Wang committed Mar 1, 2013
0 parents commit c9b4dd0
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/bin/Debug/*
*.exe

*.mdb
20 changes: 20 additions & 0 deletions ArrayJump.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArrayJump", "ArrayJump\ArrayJump.csproj", "{CE6A6B77-EE1B-4285-9969-CC53DA893773}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CE6A6B77-EE1B-4285-9969-CC53DA893773}.Debug|x86.ActiveCfg = Debug|x86
{CE6A6B77-EE1B-4285-9969-CC53DA893773}.Debug|x86.Build.0 = Debug|x86
{CE6A6B77-EE1B-4285-9969-CC53DA893773}.Release|x86.ActiveCfg = Release|x86
{CE6A6B77-EE1B-4285-9969-CC53DA893773}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = ArrayJump\ArrayJump.csproj
EndGlobalSection
EndGlobal
31 changes: 31 additions & 0 deletions ArrayJump.userprefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Properties>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug|x86" />
<MonoDevelop.Ide.Workbench ActiveDocument="ArrayJump/Problem.txt">
<Files>
<File FileName="ArrayJump/Main.cs" Line="1" Column="1" />
<File FileName="ArrayJump/Problem.txt" Line="7" Column="33" />
</Files>
<Pads>
<Pad Id="ProjectPad">
<State expanded="True">
<Node name="ArrayJump" expanded="True">
<Node name="Problem.txt" selected="True" />
</Node>
</State>
</Pad>
<Pad Id="ClassPad">
<State expanded="True" selected="True" />
</Pad>
<Pad Id="MonoDevelop.Debugger.WatchPad">
<State />
</Pad>
<Pad Id="ConnectionManagerPad">
<State selected="True" />
</Pad>
</Pads>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore />
</MonoDevelop.Ide.DebuggingService.Breakpoints>
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
</Properties>
44 changes: 44 additions & 0 deletions ArrayJump/ArrayJump.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>10.0.0</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{CE6A6B77-EE1B-4285-9969-CC53DA893773}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>ArrayJump</RootNamespace>
<AssemblyName>ArrayJump</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>True</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>False</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<Externalconsole>True</Externalconsole>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<DebugType>none</DebugType>
<Optimize>True</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<Externalconsole>True</Externalconsole>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="Main.cs" />
<Compile Include="AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<None Include="Problem.txt" />
</ItemGroup>
</Project>
27 changes: 27 additions & 0 deletions ArrayJump/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Reflection;
using System.Runtime.CompilerServices;

// Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project.

[assembly: AssemblyTitle("ArrayJump")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("zhaowang")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.

[assembly: AssemblyVersion("1.0.*")]

// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.

//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]

142 changes: 142 additions & 0 deletions ArrayJump/Main.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
using System;
using System.Collections.Generic;
namespace ArrayJump
{
class MainClass
{
static void Main(string[] args)
{
//int[] array = new int[] { 2, 3, -1, 1, 3 }; //4
//int[] array = new int[] { -1 }; //1
//int[] array = new int[] { 0 }; //-1
//int[] array = new int[] { 1, 1, -1, 1 }; //-1
//int[] array = new int[] { 1, 1, 1, 1 }; //4
//int[] array = new int[] { 1, 1, 3, 1 }; //3
int[] array = new int[] { -3 }; //1
int result = arrayJmpHareAndTortoise(array);
Console.WriteLine(result);


Console.WriteLine("Done. Press any key to continue");
Console.ReadKey();
}

/// <summary>
/// Use Set
/// </summary>
/// <param name="A"></param>
/// <returns></returns>
static int arrayJmpWithSet(int[] A)
{
if (A.Length == 0) return -1;
HashSet<int> set = new HashSet<int>();
int nextJump = A[0];
set.Add(0);
while (nextJump >= 0 && nextJump < A.Length)
{
if (set.Contains(nextJump))
{
return -1;
}
set.Add(nextJump);
nextJump = A[nextJump] + nextJump;
}
return set.Count;
}

/// <summary>
/// Use no additional data structure.
/// </summary>
/// <param name="A"></param>
/// <returns></returns>
static int arrayJmp(int[] A)
{
if (A.Length == 0) return -1;
int nextJump = A[0];
int count = 1;
while (nextJump >= 0 && nextJump < A.Length)
{
if (A[nextJump] == int.MinValue)
{
return -1;
}
count++;
int currentIndex = nextJump;
nextJump = A[nextJump] + nextJump;
A[currentIndex] = int.MinValue;
}
return count;
}

static int arrayJmpWithString(int[] A)
{
if (A.Length == 0) return -1;
int nextJump = A[0];
int count = 1;
string result = ",0";
while (nextJump >= 0 && nextJump < A.Length)
{
string temp = nextJump + ",";
if (result.IndexOf(temp) >= 0)
{
return -1;
}
count++;
result += temp;
nextJump = A[nextJump] + nextJump;
}
return count;
}

static int arrayJmpNoTracking(int[] A)
{
if (A.Length == 0) return -1;
int nextJump = A[0];
int count = 1;
while (nextJump >= 0 && nextJump < A.Length)
{
if (count > A.Length)
{
return -1;
}
count++;
nextJump = A[nextJump] + nextJump;
}
return count;
}

static int arrayJmpHareAndTortoise(int[] A)
{
if (A.Length == 0) return -1;
int nextJumpTor = A[0];
int nextJumpHare = A[0];
int count = 1;
if (IsInRangeofArray(A, nextJumpHare))
{
nextJumpHare = A[nextJumpHare] + nextJumpHare;
count++;
}
while (IsInRangeofArray(A, nextJumpHare))
{
if (nextJumpHare == nextJumpTor)
{
return -1;
}
nextJumpTor = A[nextJumpTor] + nextJumpTor;
nextJumpHare = A[nextJumpHare] + nextJumpHare;
count++;
if (IsInRangeofArray(A, nextJumpHare))
{
nextJumpHare = A[nextJumpHare] + nextJumpHare;
count++;
}
}
return count;
}

static bool IsInRangeofArray(int[] A, int nextJumpHare)
{
return nextJumpHare >= 0 && nextJumpHare < A.Length;
}
}
}
7 changes: 7 additions & 0 deletions ArrayJump/Problem.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Given an int array, the value in each element note the next index to jump.
Start from 0, calculate the number of jumps need to jump out of the array

For example {-1} will return 1 jump
{2,3,5,2} will require two jump from 0->2->5(out of array)

This is a test from codility.com

0 comments on commit c9b4dd0

Please sign in to comment.