This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* type TreeNode struct { | |
* Val int | |
* Left *TreeNode | |
* Right *TreeNode | |
* } | |
*/ | |
func findFrequentTreeSum(root *TreeNode) []int { | |
counts := map[int]int{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* type TreeNode struct { | |
* Val int | |
* Left *TreeNode | |
* Right *TreeNode | |
* } | |
*/ | |
func findBottomLeftValue(root *TreeNode) int { | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func lastStoneWeight(stones []int) int { | |
h := &IntHeap{} | |
for _, val := range stones { | |
heap.Push(h, val) | |
} | |
for h.Len() > 1 { | |
var largest = heap.Pop(h).(int) | |
var smaller = heap.Pop(h).(int) | |
if largest != smaller { | |
heap.Push(h, largest-smaller) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func maxProduct(nums []int) int { | |
h := NewIntHeap(2, 2, true) | |
for _, num := range nums { | |
heap.Push(h, num) | |
h.Prune() | |
} | |
return (heap.Pop(h).(int)-1) * (heap.Pop(h).(int)-1) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// There's definitely faster ways to do this but solution works well. | |
func maxSubsequence(nums []int, k int) []int { | |
h := NewIntHeap(0, 0, true) | |
heap.Init(h) | |
for _, num := range nums { | |
heap.Push(h, num) // push every number onto descending heap | |
} | |
maxSubUnordered := map[int]int{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func kWeakestRows(mat [][]int, k int) []int { | |
// Uses the known constraint that the number of rows is <= 100: | |
// Row score is a combination of soldier count | |
// and the row index, where soldier count is the major base and row index | |
// is the minor base. When sorted, rows with matching | |
// soldier counts will be differentiated and sorted appropriately by their index, which can easily be | |
// obtained later with mod 100. | |
scoreRow := func(index int, row []int) int { | |
count := 0 | |
for _, i := range row { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func NewIntHeap(initialSize int, maxSize int, desc bool) *IntHeap { | |
return &IntHeap{ | |
data: make([]int, initialSize)[0:0], | |
desc: desc, | |
maxSize: maxSize, | |
} | |
} | |
type IntHeap struct { | |
data []int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cachedUglyNumbers []int | |
func nthUglyNumber(n int) int { | |
if cachedUglyNumbers != nil { | |
// give the desired ugly number | |
return cachedUglyNumbers[n-1] | |
} | |
// generate all ugly numbers <= MaxInt32, then used cached values for subsequent calls | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func combinationSum4(nums []int, target int) int { | |
// dynamic programming, combination cache by target | |
targetCounts := map[int]int{} | |
var combinationCount func(int) int | |
combinationCount = func(target int) int { | |
if target <= 0 { | |
return 0 | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func canJump(nums []int) bool { | |
// can-jump cache | |
cache := map[int]bool{ | |
0: true, | |
} | |
var canJump func(int) bool | |
canJump = func(to int) bool { | |
if canJumpTo, ok := cache[to]; ok { |
OlderNewer