Skip to content

Instantly share code, notes, and snippets.

@WennderSantos
WennderSantos / StringProgrammer.cs
Created July 13, 2019 22:21
Determine the maximum number of letters within "s" that are between two "programmer strings"
private static char[] CheckerHelper() => "programmer".ToCharArray();
//Time: O(n) where n is the length of s
//Memory: O(1) using a fixed length char[] checkerHelper
static int StringProgrammer(string s)
{
var checkerHelper = CheckerHelper();
var lettersFound = 0;
for(var i = 0; i < s.Length; i++)
@WennderSantos
WennderSantos / smallestString.cs
Last active July 15, 2019 02:42
Get the smallest string given a weight (all chars have a weight) - using array
//Time: O(n) where n is weight
//Memory: O(1) weights array helper with length . of 26
//In this solution, I am searching in the array of weights from biggest to smallet
//comparing values with weight searched.
//When find a value in the array that fits the weight being searched
//use array index to calculate a char ascii value which represent a
//character wight in the result.
@WennderSantos
WennderSantos / smallestString.cs
Last active July 15, 2019 02:34
Get the smallest string given a weight (all chars have a weight)
//Time: O(n) where n is weight
//Memory: O(1) weights helper with 26 key:value pairs
//In this solution, I am decreasing weights one by one
//and searching its value in the weigths helper.
//As bigger the weight is the longer will take to algorithm execute
//I wrote a better solution using an array instead of a hashmap as a helper
//https://gist.github.com/WennderSantos/2256be4706a4f3cd6121ab322116a918
@WennderSantos
WennderSantos / authorize.clj
Last active June 2, 2019 03:35
Transaction authorization
(defn- is-card-blocked?
[{:keys [account]}]
(when (:cardIsBlocked account)
"CARD BLOCKED"))
(defn- has-no-limit?
[{:keys [account transaction]}]
(when (> (:amount transaction) (:limit account))
"NO LIMIT AVAILABLE"))
@WennderSantos
WennderSantos / 90graus.cs
Created May 17, 2019 00:39
Rotate a matrix NxN 90 degrees
using System;
using static System.Console;
namespace _90graus
{
class Program
{
static void Main(string[] args)
{
var length = 3;
@WennderSantos
WennderSantos / MaxProductOfThreeNumbers.cs
Last active February 20, 2019 14:48
Find max product of three numbers in an array of integers
static void Main(string[] args)
{
var input = new int[] { -60, -3, 5, 4, 8, 1, 10, 15, 17 };
(int first, int second, int third) maxValues = (int.MinValue, int.MinValue, int.MinValue);
(int first, int second) minValues = (int.MaxValue, int.MaxValue);
((int first, int second, int third), (int first, int second)) values = (maxValues, minValues);
for (var i = 0; i < input.Length; i++)
values = Rearenge(values.Item1, values.Item2, input[i]);
@WennderSantos
WennderSantos / SmallestNumberInTwoArrays.cs
Created February 20, 2019 00:06
Find the smallest number present in two arrays
class Program
{
static void Main(string[] args)
{
var arr1 = new int[] { 10, 12, 5, 6, 3 };
var arr2 = new int[] { 1, 9, 4, 8, 2, 3 };
var arr1Sort = new int[] { 5, 8, 9, 13 };
var arr2Sort = new int[] { 6, 7, 10, 11, 12, 13 };
@WennderSantos
WennderSantos / SmallestAndSecondSmallest.cs
Created February 20, 2019 00:03
Find smallest and second smallest elements in an array
static void Main(string[] args)
{
var input = new int[] { 1, 4, 3, 5, 6, 7, 8, 1 };
var smallest = input[0];
var secondSmallest = input[1];
if(smallest > secondSmallest)
{
var aux = smallest;
@WennderSantos
WennderSantos / BSTGetHeight.cs
Last active December 17, 2018 01:44
Get the height of a binary tree. O(n)
class Node
{
public Node(int data) => Data = data;
public int Data { get; }
public Node Left { get; set; }
public Node Rigth { get; set; }
public void Add(int value)
{
@WennderSantos
WennderSantos / IsBST.cs
Created December 16, 2018 17:54
Check if a binary tree is a binary search tree
class Node
{
public Node(int data) => Data = data;
public int Data { get; }
public Node Left { get; set; }
public Node Rigth { get; set; }
public void Add(int value)
{