Skip to content

Instantly share code, notes, and snippets.

View mikedll's full-sized avatar

Michael Rivera mikedll

View GitHub Profile
@mikedll
mikedll / binary_search.go
Created May 31, 2024 02:19
Binary Search to find an Insertion Index
func (l *Log) MInsertIndex(day string) int {
lBound := 0
uBound := len(l.Measurements)
for uBound > lBound {
mid := (uBound + lBound) / 2
cmpResult := cmp.Compare(l.Measurements[mid].Day, day)
if cmpResult <= 0 && lBound == mid {
lBound += 1
@mikedll
mikedll / Problems3.md
Last active May 5, 2024 08:48
CLRS, Problems 3 Part 2

Problems 3-3

In descending order.

  1. $2^{\lg^* n}$
  2. $\lg^* n$
  3. $\lg (\lg^* n)$

We consider ${\sqrt 2}^{\lg n}$. Let's compare it with $2^{\lg^* n}$. We conjecture

@mikedll
mikedll / Problems3.md
Last active May 1, 2024 12:02
CLRS, Problems 3

Problem 3-1

In all cases we examine only the term $a_d n^d$, since it dominates the subject polynomial's running time. All the lower order terms would do is increase the $n_0$ needed to make the bounds hold true.

a.

We are seeking some constant $c$ such that $0 \le a_d n^d \le c n^k$.

@mikedll
mikedll / Exercise3.3.md
Last active May 7, 2024 05:28
CLRS, Exercise 3.3 Part 3

Exercise 3.3-6

Let $n$ be some input to each of the functions we're evaluating. Presume to calculate $i = \lg^{*} n$. Let $n_i$ be the number less than or equal to 1 that n was reduced to by the iterative logarithm function. Let $g(n) = 2^n$. Then $g^{(i)} n_i = n$.

Consider:

$$ \begin{align}

@mikedll
mikedll / Exponent.md
Created April 16, 2024 13:09
Changing an exponent base

There is a part in CLRS chapter 3 where they have $d^k$ and they convert this to an exponential expression with a different base. The way to see this is as follows:

$$ \begin{align} d^k &= C^{\log_{c} {d^k}} \\ &= C^{k \log_c d} \\ &= C^{k / log_d c} \end{align} $$

@mikedll
mikedll / Exercises3.3.md
Last active April 22, 2024 01:54
CLRS, Exercises 3.3 Part 2

Exercise 3.3-5

a. Restrict the domain to powers of 2, so $n = 2^{c_1}$ where $c_1$ is some integer greater than or equal to 1. Our restriction of the domain of $n$ to powers of 2 still permits a conclusive analysis of $\lceil \lg n \rceil !$ as to how it compares to $c n^k$ as $n$ increases.

We have $\lceil \lg n \rceil ! = \lceil \lg 2^{c_1} \rceil ! = c_1 !$ (by 3.1). By 3.29, this is:

$$ \begin{align}

@mikedll
mikedll / Source.rb
Created March 29, 2024 14:34
Binomial Coefficients by Dynamic Programming, Using Ruby
class Table
def initialize(n, firstValue)
@m = {}
(0...n).each do |i|
@m[i] = {}
(0...n).each do |j|
@m[i][j] = firstValue
end
end
end
@mikedll
mikedll / Source.rb
Created March 29, 2024 14:21
Balanced (AVL) Binary Search Tree Implementation
#!/usr/bin/ruby
#require 'test/unit'
require 'test/unit/testcase'
class TestBST &lt; Test::Unit::TestCase
# ... tests omitted...
def test_no_crashes
@mikedll
mikedll / EnemyTowers.cs
Created March 29, 2024 14:19
Top Coder SRM 450
using System;
class EnemyTowers
{
const int INF = int.MaxValue;
public int sim( int s, int h, int a, int t)
{
int ret = 0;
int p = h;
while( s > 0 && t > 0 ) {
@mikedll
mikedll / Source.js
Last active March 29, 2024 14:17
Strongly Connected Components in JavaScript and Ruby
// requires prototype for some of its synctactic sugar
var Undiscovered = 0;
var Discovered = 1;
var Processed = 2;
var time = 0;
var scc_count = 0;
var states = {};
var et = {};
var low = {};