Home

C# Reducing The Stones

using System.Text;

public class Program
{
    //https://leetcode.com/problems/last-stone-weight/
    static void Main()
    {
        while (true)
        {
            Console.WriteLine("Enter the Stone Array");
            string myArrayString = Console.ReadLine();

            myArrayString = myArrayString.Replace("[", "");
            myArrayString = myArrayString.Replace("]", "");

            string[] myStringArray = myArrayString.Split(",");

            int[] myIntArray = new int[myStringArray.Length];

            for (int i = 0; i < myStringArray.Length; i++)
            {
                myIntArray[i] = Convert.ToInt32(myStringArray[i]);
            }

            Console.WriteLine(LastStoneWeight(myIntArray));
        }
    }

    public static int LastStoneWeight(int[] stones)
    {
        StoneList = new List<int>(stones);

        while (StoneList.Count > 1)
        {
            int biggest = FindBiggest();
            int nextBiggest = FindBiggest();

            StoneList.Add(biggest - nextBiggest);
        }

        return StoneList[0];
    }

    static List<int> StoneList { get; set; }

    static int FindBiggest()
    {
        int isThisBiggest = 0;
        int biggestPosition = 0;
        for (int i = 0; i < StoneList.Count; i++)
        {
            if (StoneList[i] > isThisBiggest)
            {
                isThisBiggest = StoneList[i];
                biggestPosition = i;
            }
        }

        StoneList.RemoveAt(biggestPosition);

        return isThisBiggest;
    }
}

Reader's Comments

Post Your Comment Posts/Links Rules

Name

Comment

Add a RELEVANT link (not required)

Upload an image (not required)

No uploaded image
Real person number
Please enter the above number below




Home
Admin Ren's Biking Blog