• + 0 comments

    Passing all test cases but exceeding time limit (~5 sec runtime) in a few. Could someone suggest an alternative approach or improvements to my current solution (C#)?

        public static int poisonousPlants(List<int> p)
        {
            int days = 0;
            List<int> plants = p.ToList();
            bool finalDay = false;
            do
            {
                int pCount = p.Count;
                for (int i = 1; i < pCount; i++)
                {
                    if (p[i - 1] < p[i]) plants.RemoveAt(i - (pCount - plants.Count));
                    if (i == pCount - 1)
                    {
                        if (p.SequenceEqual(plants)) finalDay = true;
                        else days++; p = plants.ToList();
                    }
                }
            } while (!finalDay && p.Count > 1);
            return days;
        }