Four Problems in 10min

Challenge was to solve 4 LC problems (2 easy, 2 medium) in <= 10min. I picked the ones below, new ones, from today. Solutions might not be the most elegant and/or efficient, but they all passed. Code is down below, cheers, ACC.



Solution to Problem #1:

    public class Solution
    {
        public int LongestSubarray(int[] nums)
        {
            Hashtable maxOnesAtPosition = new Hashtable();

            bool insideOne = false;
            int beginPos = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] == 1 && !insideOne)
                {
                    beginPos = i;
                    insideOne = true;
                }
                else if (nums[i] == 0 && insideOne)
                {
                    maxOnesAtPosition.Add(beginPos, i - beginPos);
                    if (i - 1 > beginPos)
                    {
                        maxOnesAtPosition.Add(i - 1, i - beginPos);
                    }
                    insideOne = false;
                }
                
                if (i == nums.Length - 1 && nums[i] == 1)
                {
                    maxOnesAtPosition.Add(beginPos, i - beginPos + 1);
                    if (i > beginPos)
                    {
                        maxOnesAtPosition.Add(i, i - beginPos + 1);
                    }
                }
            }

            bool atLeastOneZero = false;
            int max = 0;
            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] == 0)
                {
                    int current = 0;
                    if (maxOnesAtPosition.ContainsKey(i - 1)) current += (int)maxOnesAtPosition[i - 1];
                    if (maxOnesAtPosition.ContainsKey(i + 1)) current += (int)maxOnesAtPosition[i + 1];
                    max = Math.Max(max, current);
                    atLeastOneZero = true;
                }
            }

            if (!atLeastOneZero) return nums.Length - 1;
            return max;
        }
    }

Solution to Problem #2:

    public class Solution
    {
        public int KthFactor(int n, int k)
        {
            int count = 0;

            for (int i = 1; i <= n; i++)
            {
                if (n % i == 0)
                {
                    count++;
                    if (count == k)
                    {
                        return i;
                    }
                }
            }

            return -1;
        }
    }

Solution to Problem #3:

    public class Solution
    {
        public double Average(int[] salary)
        {
            Array.Sort(salary);

            int sum = 0;
            for (int i = 1; i < salary.Length - 1; i++)
            {
                sum += salary[i];
            }

            return (1.0 * sum) / (salary.Length - 2);
        }
    }

Solution to Problem #4:

    public class Solution
    {
        public bool IsPathCrossing(string path)
        {
            Hashtable points = new Hashtable();

            int x = 0;
            int y = 0;
            points.Add(x * 20000 + y, true);
            for (int i = 0; i < path.Length; i++)
            {
                switch (path[i])
                {
                    case 'N':
                        y++;
                        break;
                    case 'S':
                        y--;
                        break;
                    case 'E':
                        x++;
                        break;
                    case 'W':
                        x--;
                        break;
                }

                if (points.ContainsKey(x * 20000 + y)) return true;
                points.Add(x * 20000 + y, true);
            }

            return false;
        }
    }

Comments

Post a Comment

Popular posts from this blog

Count Binary Substrings

Count Vowels Permutation: Standard DP

Maximum Number of Balloons