Skip to content

Instantly share code, notes, and snippets.

View MorrisLaw's full-sized avatar
💭
writing some code

Jeremy L. Morris MorrisLaw

💭
writing some code
View GitHub Profile

Keybase proof

I hereby claim:

  • I am MorrisLaw on github.
  • I am morrislaw (https://keybase.io/morrislaw) on keybase.
  • I have a public key whose fingerprint is 20EB 8493 C6D3 30D0 876C 2DFF 6A12 0D78 BE12 366F

To claim this, I am signing this object:

@MorrisLaw
MorrisLaw / insertion_sort.py
Last active August 10, 2017 17:08
Insertion sort, ascending order based off of the pseudocode in Introduction to Algorithms by C.L.R.S.
A = [10,2,5,7,3,22]
for j in xrange(1, len(A)):
key = A[j]
# Insert A[j] into the sorted sequence A[1..j-1].
i = j - 1
while (i > -1 and A[i] > key):
A[i + 1] = A[i]
i = i - 1
A[i + 1] = key
@MorrisLaw
MorrisLaw / JsonBulkInsert.java
Created July 19, 2017 14:31
Insert json in MySQL using MySQL batch pooling.
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.util.LinkedHashSet;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
@MorrisLaw
MorrisLaw / two_sum.go
Created January 19, 2022 20:47
two sum O(n^2) solution
func twoSum(nums []int, target int) []int {
if nums == nil {
return nil
}
for i := 0; i < len(nums); i++ {
for j := i+1; j < len(nums); j++ {
if nums[i] + nums[j] == target {
return []int{i, j}
}
@MorrisLaw
MorrisLaw / contain_duplicates.go
Last active January 20, 2022 22:19
time complexity is O(n) where n is the max number of elements we look at to check and store in the set and O(n) space complexity because the set is proportionate to the number of elements in the array we're looking at, all of them in the worse case.
func containsDuplicate(nums []int) bool {
if nums == nil {
return false
}
set := make(map[int]bool)
for _, i := range nums {
if set[i] {
return true
@MorrisLaw
MorrisLaw / maximum_subarray.go
Last active January 21, 2022 03:44
time complexity is O(n) because we do at most, one iteration through the array where n is the length of the array. Space complexity is constant since we have an input array and 3 variables we are using (curr, max and i)
import "math"
func maxSubArray(nums []int) int {
if nums == nil { return 0 }
curr := nums[0]
max := curr
for i := 1; i < len(nums); i++ {
curr = int(math.Max(float64(curr + nums[i]), float64(nums[i])))
@MorrisLaw
MorrisLaw / intersection_two_arrays.go
Last active January 22, 2022 13:08
time complexity is O(n + m) where n is the first array and m is the second array. Space complexity is O(n) where n is the sizeof the map that gets created.
func intersect(nums1 []int, nums2 []int) []int {
count := make(map[int]int)
for _, n := range nums1 {
count[n]++
}
var result []int
for _, n := range nums2 {
if count[n] > 0 {
@MorrisLaw
MorrisLaw / merge_sort.go
Last active January 22, 2022 17:41
failed
func merge(nums1 []int, m int, nums2 []int, n int) {
j := n-1
if m == 0 {
for i := len(nums1)-1; i >= 0; i-- {
nums1[i] = nums2[j]
j--
}
}
k := len(nums1)-1
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
type Queue struct {
nodes []*TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
type Queue struct {
nodes []*TreeNode