Skip to content

Instantly share code, notes, and snippets.

@gogsbread
Created January 2, 2013 23:03
Show Gist options
  • Save gogsbread/4439171 to your computer and use it in GitHub Desktop.
Save gogsbread/4439171 to your computer and use it in GitHub Desktop.
Finding similar jugs in O(n2) time. This is a naive implementation of finding two jugs of similar capacity.
namespace dotNetPlayGround
{
using System;
using System.Collections.Generic;
public struct Jug
{
public int Capacity;
public int Filled;
public Jug(int capacity,int filled){
Capacity = capacity;
Filled = filled;
}
public override bool Equals(object obj)
{
Jug jug = (Jug)obj;
return (this.Filled + jug.Filled == this.Capacity && this.Filled + jug.Filled == jug.Capacity);
}
}
public class WaterJugs
{
static void Main()
{
int total = 4;
Jug[] red = { new Jug(10, 5),new Jug(20,10),new Jug(30,15), new Jug(40,20) };
Jug[] blue = { new Jug(40, 20), new Jug(30, 15), new Jug(20, 10), new Jug(10, 5) };
for (int i = 0; i < total; i++)
{
for (int j = 0; j < total; j++)
{
if (red[i].Equals(blue[j]))
Console.WriteLine("group:({0},{1})", i, j);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment