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
@MorrisLaw
MorrisLaw / vimrc
Last active June 8, 2023 14:42
my vimrc
colorscheme evening
set number
set relativenumber
call plug#begin()
Plug 'rust-lang/rust.vim'
call plug#end()
@MorrisLaw
MorrisLaw / .zshrc
Created August 1, 2022 18:13
zsh setup
ZSH_THEME="minimal"
# Turn off all beeps
unsetopt BEEP
@MorrisLaw
MorrisLaw / readfile.go
Created July 13, 2022 17:41
Read file in Go
package main
import (
"bufio"
"fmt"
"log"
"os"
)
func main() {
@MorrisLaw
MorrisLaw / valid_anagram.go
Last active July 5, 2022 20:06
Valid Anagram - LeetCode 242
func isAnagram(s string, t string) bool {
sCount := make([]int, 26)
for _, char := range s {
sCount[char-'a']++
}
tCount := make([]int, 26)
for _, char := range t {
tCount[char-'a']++
}
@MorrisLaw
MorrisLaw / num_of_islands.go
Created June 30, 2022 12:43
Number of Islands
func numIslands(grid [][]byte) int {
if grid == nil {
return 0
}
var numOfIslands int
for i := 0; i < len(grid); i++ {
for j := 0; j < len(grid[0]); j++ {
if grid[i][j] == '1' {
@MorrisLaw
MorrisLaw / running_sum.go
Last active June 29, 2022 14:51
running sum
// O(n) time, O(n) space
func runningSum(nums []int) []int {
if len(nums) <= 1 { return nums }
var result []int // O(n) space complexity
result = append(result, nums[0])
for i := 1; i < len(nums); i++ { // O(n) time complexity
result = append(result, nums[i]+(result[i-1]))
}
return result
@MorrisLaw
MorrisLaw / top_k_frequent_w_heap.gol
Created June 5, 2022 12:10
Top K Frequent Elements (using Heap) - LeetCode 347
type MaxHeap [][]int
func (h MaxHeap) Len() int { return len(h) }
func (h MaxHeap) Less(i, j int) bool { return h[i][0] > h[j][0] }
func (h MaxHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *MaxHeap) Push(x interface{}) {
*h = append(*h, x.([]int))
}
@MorrisLaw
MorrisLaw / top_k_frequent_elements.go
Created June 3, 2022 11:38
Top K Frequent Elements - LeetCode 347
func topKFrequent(nums []int, k int) []int {
freqMap := make(map[int]int)
for _, n := range nums {
freqMap[n]++
}
freqArr := make([][]int, len(nums)+1)
for k, v := range freqMap {
freqArr[v] = append(freqArr[v], k)
}
@MorrisLaw
MorrisLaw / best_time_to_buy_and_sell_stock.go
Last active June 3, 2022 11:29
Best Time to Buy and Sell Stock - LeetCode 121
func maxProfit(prices []int) int {
var max int
l, r := 0, 1
for r < len(prices) {
if prices[l] < prices[r] {
max = int(math.Max(float64(max), float64(prices[r]-prices[l])))
} else {
l = r
}
@MorrisLaw
MorrisLaw / twosum.go
Last active June 3, 2022 11:27
Two Sum - LeetCode 1
func twoSum(nums []int, target int) []int {
hm := make(map[int]int)
for i, n := range nums {
if j, exists := hm[n]; exists {
return []int{i, j}
}
hm[target-n] = i
}