Skip to content

Instantly share code, notes, and snippets.

View claytonjwong's full-sized avatar
🚂
🚂 I think I can... 🚂 I think I can... 🚂 I think I can...

Clayton Wong claytonjwong

🚂
🚂 I think I can... 🚂 I think I can... 🚂 I think I can...
View GitHub Profile
/*
Heap Property:
* MinHeap: For every object x, the key of x is <= the keys of its children
* MaxHeap: For every object x, the key of x is >= the keys of its children
----------------------------------------------------------------------------------------------------
Insert:
@claytonjwong
claytonjwong / permutations.js
Created November 20, 2021 16:34
Returns the permutations of the array A
let permutations = A => {
let N = A.length;
let perms = [];
let go = (i = 0) => {
if (i == N) {
perms.push([...A]);
return;
}
for (let k = i; k < N; ++k) {
[A[i], A[k]] = [A[k], A[i]];
fun permutations(A: IntArray): Array<IntArray> {
var N = A.size
var perms = mutableListOf<IntArray>()
fun go(i: Int = 0) {
if (i == N) {
perms.add(A.copyOf())
return
}
for (k in i until N) {
A[i] = A[k].also{ A[k] = A[i] }
/*
* Kotlin version of C++ equal_range via lower_bound and upper_bound
*
* Gist: https://gist.github.com/claytonjwong/2f9c3b33f8697d77a1d442006d4947d6
*/
fun lowerBound(A: IntArray, target: Int): Int {
val N = A.size
var i = 0
var j = N
@claytonjwong
claytonjwong / transpose_matrix.md
Created April 8, 2022 19:19
transposes a matrix, ie. transform rows into columns

Kotlin

var transpose = { A: Array<IntArray> -> A[0].mapIndexed{ j, _ -> A.mapIndexed{ i, _ -> A[i][j] }.toIntArray() }.toTypedArray() }

Javascript

let transpose = A => A[0].map((_, j) => A.map((_, i) => A[i][j]));
@claytonjwong
claytonjwong / rev_linked_list.kt
Created December 22, 2021 15:26
reverse linked list
//
// Recursively change each node of the linked list such that next
// is the last node seen during a linear scan of the linked list.
//
class Solution {
fun reverseList(head: ListNode?): ListNode? {
fun go(node: ListNode? = head, last: ListNode? = null): ListNode? {
var next = node?.next
node?.next = last
if (next == null)
@claytonjwong
claytonjwong / rev_linked_list.js
Created December 22, 2021 15:26
reverse linked list
//
// Recursively change each node of the linked list such that next
// is the last node seen during a linear scan of the linked list.
//
let reverseList = head => {
let go = (node = head, last = null) => {
let next = node.next;
node.next = last;
if (!next)
return node;
@claytonjwong
claytonjwong / rev_linked_list.py
Created December 22, 2021 15:25
reverse linked list
#
# Recursively change each node of the linked list such that next
# is the last node seen during a linear scan of the linked list.
#
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
def go(node = head, last = None):
next = node.next
node.next = last
if not next:
@claytonjwong
claytonjwong / rev_linked_list.cpp
Created December 22, 2021 15:24
reverse linked list
//
// Recursively change each node of the linked list such that next
// is the last node seen during a linear scan of the linked list.
//
class Solution {
public:
using fun = function<ListNode*(ListNode*, ListNode*)>;
ListNode* reverseList(ListNode* head) {
fun go = [&](auto node, auto last) {
auto next = node->next;
template< typename T >
class Solution
{
using Vector = vector< T >;
using Iter = typename Vector::iterator;
Vector go( Vector&& A )
{
auto N{ A.size() };
if( N < 2 ) return A;