Skip to content

Instantly share code, notes, and snippets.

@WuchiOnline
WuchiOnline / gist:36896ef9e37190423db61bbf22081a0f
Last active October 11, 2020 23:35
Backtracking String Combination from Set
/*
Given a string and a set of words, break the string into a list of words from the set. If the string can not be segmented fully, return an empty list.
Example:
Set: {"jump", "jumped", "jumpedov", "over", "some", "thing", "something"}
String: "jumpedoversomething",
Can return [“jumped”, “over”, “something”] or [ “jumped”, “over”, “some”, “thing”]
@WuchiOnline
WuchiOnline / gist:db8dfb613f8e1f447e5df7169a6bc377
Last active August 21, 2020 01:55
Kth Smallest Number in M Sorted Lists using MinHeap<T> and custom IComparable class ElementArrayIndexPair
/*
Kth Smallest Number in M Sorted Lists:
Given ‘M’ sorted arrays, find the K’th smallest number among all the arrays.
Example:
Input: L1=[2, 6, 8], L2=[3, 6, 7], L3=[1, 3, 4], K=5
Output: 4
@WuchiOnline
WuchiOnline / gist:64c0f65b715dbb410427f56138cd6183
Last active August 19, 2020 01:54
Top K Most Frequent Numbers using MinHeap<T>, Dictionary<TKey,TValue>, and custom NumCountPair that implements IComparable
/*
Given an unsorted array of numbers, find the top ‘K’ frequently occurring numbers in it.
Input: [1, 3, 5, 12, 11, 12, 11], K = 2
Output: [12, 11]
Approach:
// Create a class NumCountPair that implements IComparable to use in a MinHeap, which CompareTo other NumCountPairs based on their Count properties
// Use a dictionary to hold KeyValuePairs where the key is the number and the value is its frequency // tO(N), sO(N)
@WuchiOnline
WuchiOnline / gist:ccf434fcfbd1a44194780829c7980db3
Last active August 18, 2020 17:01
Top K Most Frequent Numbers using Linq Orderby (Heap-less Solution)
/*
Given an unsorted array of numbers, find the top ‘K’ frequently occurring numbers in it.
Example:
Input: [1, 3, 5, 12, 11, 12, 11], K = 2
Output: [12, 11]
Approach:
@WuchiOnline
WuchiOnline / CSharpMaxHeapGeneric
Last active August 19, 2020 21:46
C# MaxHeap<T> where T: IComparable<T> Implementation
public class MaxHeap<T> where T: System.IComparable<T>
{
public List<T> heapList {get; set;}
public int Count {get => heapList.Count;}
private int GetLeftChildIndex(int elementIndex) => 2 * elementIndex + 1;
private int GetRightChildIndex(int elementIndex) => 2 * elementIndex + 2;
private int GetParentIndex(int elementIndex) => (elementIndex - 1) / 2;
@WuchiOnline
WuchiOnline / gist:3660e0db73ca97df28ad0b6a973340f5
Created July 17, 2020 19:40
Full Solution to Merge K Sorted Linked Lists in C# with MinHeap<T> and ListNode implementation
/*
Full solution to Merge K Sorted Lists with custom MinHeap<T> and ListNode classes implemented.
*/
using System;
using System.Collections;
using System.Collections.Generic;
@WuchiOnline
WuchiOnline / gist:0b4ce6f5f8cc6f2b8d8545c4c21a3a93
Last active July 17, 2020 17:12
Topological Sort for Graph in C#
/*
Given a directed graph, find the topological ordering of its vertices.
Input: Vertices=4, Edges=[3, 2], [3, 0], [2, 0], [2, 1]
Output: Following are the two valid topological sorts for the given graph:
1) 3, 2, 0, 1
2) 3, 2, 1, 0
Underlying concepts:
@WuchiOnline
WuchiOnline / CSharpMinHeapGeneric.txt
Last active July 17, 2020 19:44
C# MinHeap<T> where T: IComparable<T> Implementation
public class MinHeap<T> where T: System.IComparable<T>
{
public List<T> heapList {get; set;}
public int Count {get => heapList.Count;}
private int GetLeftChildIndex(int elementIndex) => 2 * elementIndex + 1;
private int GetRightChildIndex(int elementIndex) => 2 * elementIndex + 2;
private int GetParentIndex(int elementIndex) => (elementIndex - 1) / 2;
@WuchiOnline
WuchiOnline / minHeap_maxHeap.txt
Last active November 30, 2021 21:53
MinHeap and MaxHeap implementation for C#
public class MinHeap
{
private readonly int[] _elements;
private int _size;
public int Count {get => _size;}
public MinHeap(int size)
{
_elements = new int[size];
}
@WuchiOnline
WuchiOnline / System Design.md
Created June 9, 2020 17:09 — forked from vasanthk/System Design.md
System Design Cheatsheet

System Design Cheatsheet

Picking the right architecture = Picking the right battles + Managing trade-offs

Basic Steps

  1. Clarify and agree on the scope of the system
  • User cases (description of sequences of events that, taken together, lead to a system doing something useful)
    • Who is going to use it?
    • How are they going to use it?