• + 0 comments

    C# Easy to Understand Solution

        public static int utopianTree(int n)
        {
            if (n == 0) return 1;
            int start = 1;
            for (int i = 1; i <= n; i++) {
                if (i % 2 == 0) start += 1;
                else start *= 2;
            }
            return start;
        }
    

    First of all, since the tree sapling starts at 1 height, then if "n" is 0 (no growth), then its height stays at 1.

    Then since it's always starts at Spring, then Summer, then Spring again, etc. We can think of it like an index. You might notice that each Spring happens at odd index, while each Summer happens at even index. This means you can just check if the index is odd or even. If it's an odd index (Spring), multiply the height by 2. If it's an even index (Summer), increment the height by 1.