Tuesday, February 23, 2021

Leetcode Everyday: 1021. Remove Outermost Parentheses. Easy

public class Solution {
    public string RemoveOuterParentheses(string s) {
        var sb = new StringBuilder();        
    
        var opList = new Dictionary<char, int> {
            ['('] = 1,
            [')'] = -1
        };
        
        int depth = 0;
        foreach (var c in s) {
            var op = opList[c];
            depth += op;
            if (op == 1 && depth > 1 || op == -1 && depth > 0) {
                sb.Append(c);
            }
        }
        
        return sb.ToString();
    }
}
Compact condition:
public class Solution {
    public string RemoveOuterParentheses(string s) {
        var sb = new StringBuilder();
         
     
        // KeyValue<op, depth>
        // op = 1 = increment
        // op = -1 = decrement
        var opList = new Dictionary<char, KeyValuePair<int, int>> {
            ['('] = new KeyValuePair<int, int>(1, 1),
            [')'] = new KeyValuePair<int, int>(-1, 0)
        };
         
         
        int depth = 0;
        foreach (var c in s) {
            var (op, opDepth) = opList[c];
            depth += op;
                  
            if (depth > opDepth) {
                sb.Append(c);
            }
        }
         
        return sb.ToString();
    }
}
Source: https://leetcode.com/problems/remove-outermost-parentheses/

Monday, February 22, 2021

Leetcode Everyday: 1295. Find Numbers with Even Number of Digits. Easy

Functional programming:
using static System.Math;

public class Solution {
    public int FindNumbers(int[] nums) =>
        nums.Count(num => CountDigit(num) % 2 == 0);
    
    public int CountDigit(int n) => (int)Max(Log10(n), 0) + 1;
}
Imperative programming:
using static System.Math;

public class Solution {
    public int FindNumbers(int[] nums) {
        int evenCount = 0;
        foreach (var num in nums) {
            if (CountDigit(num) % 2 == 0) {
                ++evenCount;
            }  
        }
        return evenCount;
        
        int CountDigit(int num) {            
            int digit = 0;
            for (; num != 0; num /= 10) {
                ++digit;
            }
            return Max(digit, 1);
        }
    }
}
Source: https://leetcode.com/problems/find-numbers-with-even-number-of-digits/submissions/

Leetcode Everyday: 1266. Minimum Time Visiting All Points. Easy

Functional programming:
using static System.Math;

public class Solution {
    public int MinTimeToVisitAllPoints(int[][] points) =>
        points.Skip(1).Aggregate(
            new { count = 0, source = points.First() },
            (stat, dest) =>
                new
                { 
                    count = stat.count + 
                        Max(Abs(dest[0] - stat.source[0]), Abs(dest[1] - stat.source[1])), 
                    source = dest 
                }
        ).count;
}
Imperative programming:
using static System.Math;

public class Solution {
    public int MinTimeToVisitAllPoints(int[][] points) {
        var count = 0;
        
        var source = points.First();
        foreach (var dest in points.Skip(1)) {                                
            var lengthX = dest[0] - source[0];
            var lengthY = dest[1] - source[1];
            
            count += Max(Abs(lengthX), Abs(lengthY));
        
            source = dest;
        }
        
        return count;
    }
}
Source: https://leetcode.com/problems/minimum-time-visiting-all-points/

Sunday, February 21, 2021

Leetcode Everyday: 709. To Lower Case. Easy

It's in the rules not to use the built-in method. Here's a solution without using the built-in method
public class Solution {
    public string ToLowerCase(string str) {
        var sb = new StringBuilder(str);
        for (var i = 0; i < sb.Length; ++i) {
            char c = sb[i];
            if (c >= 'A' && c <= 'Z') {
                sb[i] =  (char)(c + ('a' - 'A'));
            }
        }
        return sb.ToString();
    }
}
Source: https://leetcode.com/problems/to-lower-case/

Leetcode Everyday: 1534. Count Good Triplets. Easy

public class Solution {
    public int CountGoodTriplets(int[] arr, int a, int b, int c) {
        int goodTriplets = 0;
        
        for (var i = 0; i < arr.Length - 2; ++i) 
            for (var j = i + 1; j < arr.Length - 1; ++j)
                for (var k = j + 1; k < arr.Length; ++k) {
                    if (Math.Abs(arr[i] - arr[j]) <= a && Math.Abs(arr[j] - arr[k]) <= b && Math.Abs(arr[i] - arr[k]) <= c) {
                        ++goodTriplets;
                    }       
                }
        
        return goodTriplets;
    }
}
Source: https://leetcode.com/problems/count-good-triplets/