Sunday, February 21, 2021

Leetcode Everyday: 1732. Find the Highest Altitude. Easy

Solution using functional programming:
public class Solution {

    public int LargestAltitude(int[] gains) =>
        gains.Aggregate(      
            // Initial currentData:
            new { runningAlt = 0, maxAlt = 0}, 
        
            (currentData, gained) =>
                new 
                { 
                    runningAlt = 
                        currentData.runningAlt + gained, 
                    maxAlt = 
                        currentData.runningAlt + gained > currentData.maxAlt ? 
                            currentData.runningAlt + gained 
                        : 
                            currentData.maxAlt
                }
        ).maxAlt;
    
}
Imperative programming:
public class Solution {

    public int LargestAltitude(int[] gains) {
        var runningAlt = 0;
        var maxAlt = 0;
        
        foreach (var gained in gains) {
            runningAlt += gained;
            
            if (runningAlt > maxAlt) {
                maxAlt = runningAlt;
            }
        }
        
        return maxAlt;
    }
    
}
Source: https://leetcode.com/problems/find-the-highest-altitude/

Saturday, February 20, 2021

Leetcode Everyday: 1290. Convert Binary Number in a Linked List to Integer

public class Solution {
    public int GetDecimalValue(ListNode head) {    
        var stack = new Stack<int>();        
        while (head != null) {
            stack.Push(head.val);
            head = head.next;
        }
        
        int n = 1;
        int sum = 0;        
        foreach (var digit in stack) {
            sum += digit * n;
            n *= 2;
        }
        return sum;
    }
}
Source: https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/

Leetcode Everyday: 1588. Sum of All Odd Length Subarrays. Easy

Solution for Sum of All Odd Length Subarrays:
public class Solution {
    public int SumOddLengthSubarrays(int[] arr) {
        int globalSum = 0;
           
        for (var odd = 1; odd <= arr.Length; odd += 2) {
            int count = 0;
            int sum = 0;
            for (; count < odd; ++count) {
                sum += arr[count];
            }
            // Log(sum);
            globalSum += sum;
            for (var i = count; i < arr.Length; ++i) {                
                sum += arr[i] - arr[i - odd];
                // Log(sum);
                globalSum += sum;
            }
            // Console.WriteLine();
        }
            
        return globalSum;
    }
    
    void Log(int n) {
          Console.Write("{0} ", n);   
    }
}
Output:
1 4 2 5 3 
7 11 10 
15 
There's a better solution than the above solution

Friday, February 19, 2021

Leetcode Everyday: 1179. Reformat Department Table. Easy

select id, 
    sum(case when month = 'Jan' then revenue end) as "Jan_Revenue",
    sum(case when month = 'Feb' then revenue end) as "Feb_Revenue",
    sum(case when month = 'Mar' then revenue end) as "Mar_Revenue",    
    sum(case when month = 'Apr' then revenue end) as "Apr_Revenue",    
    sum(case when month = 'May' then revenue end) as "May_Revenue",    
    sum(case when month = 'Jun' then revenue end) as "Jun_Revenue",    
    sum(case when month = 'Jul' then revenue end) as "Jul_Revenue",    
    sum(case when month = 'Aug' then revenue end) as "Aug_Revenue",    
    sum(case when month = 'Sep' then revenue end) as "Sep_Revenue",    
    sum(case when month = 'Oct' then revenue end) as "Oct_Revenue",    
    sum(case when month = 'Nov' then revenue end) as "Nov_Revenue",
    sum(case when month = 'Dec' then revenue end) as "Dec_Revenue"
from department
group by id
Source: https://leetcode.com/problems/reformat-department-table/

Leetcode Everyday: 1656. Design an Ordered Stream. Easy

public class OrderedStream {
    
    int _ptr = 1;
    readonly int _max;
    readonly string[] _list;

    public OrderedStream(int n) {
        _list = new string[n + 1];
        _max = n;
    }
    
    public IList<string> Insert(int idKey, string value) {       
        _list[idKey] = value;
        
        int start = _ptr;
        while (_ptr <= _max && _list[_ptr] != null) {
            ++_ptr;
        }
        
      
        // 424 ms.  
        // return _list[start.._ptr];        
        
        // 376 ms. For some reasons, this is faster
        return new Span<string>(_list, start, length: _ptr - start).ToArray();
    }
}
Fastest

Source: https://leetcode.com/problems/design-an-ordered-stream/