Monday, April 5, 2021

Leetcode Everyday: 1748. Sum of Unique Elements. Easy

public class Solution {
    public int SumOfUnique(int[] nums) {
        var hs = 
            from n in nums
            group n by n into g
            where g.Count() == 1
            select g.Key;
        
        return hs.Sum();
    }
}
Source: https://leetcode.com/problems/sum-of-unique-elements/submissions/

Monday, March 29, 2021

Leetcode Everyday: 617. Merge Two Binary Trees. Easy

public class Solution {
    public TreeNode MergeTrees(TreeNode root1, TreeNode root2) =>
        root1 != null || root2 != null ? 
            new TreeNode(
                (root1?.val ?? 0) + (root2?.val ?? 0),
                MergeTrees(root1?.left, root2?.left),
                MergeTrees(root1?.right, root2?.right)            
            )
        : 
            null;                        
}

// TODO: optimize without allocating
Source: https://leetcode.com/problems/merge-two-binary-trees/

Saturday, March 20, 2021

Leetcode Everyday: 728. Self Dividing Numbers. Easy

public class Solution {
    public IList<int> SelfDividingNumbers(int left, int right) {
        var list = new List<int>();
        for (var i = left; i <= right; ++i) {
            for (var n = i; n > 0; n /= 10) {
                var toDivide = n % 10;
                if (toDivide == 0 || i % toDivide != 0) {
                    goto goNextNumber;
                }                
            }
            list.Add(i);
            
goNextNumber:;
        }
        return list;
    }
}
Source: https://leetcode.com/problems/self-dividing-numbers/

Tuesday, March 16, 2021

Leetcode Everyday: 1351. Count Negative Numbers in a Sorted Matrix. Easy

public int CountNegatives(int[][] grid) {
    var count = 0;
    foreach (var line in grid) {
        for (var i = 0; i < line.Length; ++i) {
            if (line[i] <0) {
                count += line.Length - i;
                break;
            }
        }
    }
    return count;
}

// TODO: Create a binary search approach
Functional:
public class Solution {
	public int CountNegatives(int[][] grid) => grid.SelectMany(line => line).Count(n => n < 0);    
}
Source: https://leetcode.com/problems/count-negative-numbers-in-a-sorted-matrix/

Sunday, March 14, 2021

Leetcode Everyday: 1464. Maximum Product of Two Elements in an Array. Easy

public class Solution {
    public int MaxProduct(int[] nums) {
        // first and second highest
        var first = 0;
        var second = 0;
        
        foreach (var n in nums) {
            if (n > first) {
                (first, second) = (n, first);
            } else if (n > second) {
                second = n;
            }
        }
        
        return (first - 1) * (second - 1);
    }
}
Functional Programming:
public class Solution {
    public int MaxProduct(int[] nums) =>
        nums.OrderByDescending(n => n).Take(2)
            .Aggregate((a, b) => (a-1) * (b-1));        
}
Source: https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/