Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions best-time-to-buy-and-sell-stock/tedkimdev.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// TC: O(n)
// SC: O(1)
func maxProfit(prices []int) int {
l, r := 0, 1
max := 0

for r < len(prices) {
if prices[l] < prices[r] {
profit := prices[r] - prices[l]
if profit > max {
max = profit
}
} else {
l = r
}
r++
}

return max
}
32 changes: 32 additions & 0 deletions encode-and-decode-strings/tedkimdev.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
type Solution struct{}

// TC: O(m)
// SC: O(m + n)
func (s *Solution) Encode(strs []string) string {
encoded := ""
for _, str := range strs {
encoded += fmt.Sprintf("%d|%s", len(str), str)
}

return encoded
}

// TC: O(m)
// SC: O(m + n)
func (s *Solution) Decode(encoded string) []string {
decoded := make([]string, 0)

i := 0
for i < len(encoded) {
j := i
for encoded[j] != '|' {
j++
}
length, _ := strconv.Atoi(encoded[i:j])
i = j + 1
decoded = append(decoded, encoded[i:i+length])
i += length
}

return decoded
}
19 changes: 19 additions & 0 deletions group-anagrams/tedkimdev.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// TC: O(m * n)
// SC: O(m * n)
func groupAnagrams(strs []string) [][]string {
groups := map[[26]int][]string{}

for _, s := range strs {
var count [26]int
for _, c := range s {
count[c-'a']++
}
groups[count] = append(groups[count], s)
}

result := [][]string{}
for _, group := range groups {
result = append(result, group)
}
return result
}
52 changes: 52 additions & 0 deletions implement-trie-prefix-tree/tedkimdev.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// TC: O(n)
// SC: O(t) - Where n is the length of the string and t is the total number of TrieNodes created in the Trie.
type PrefixTree struct {
children map[rune]*PrefixTree
isWord bool
}

func Constructor() PrefixTree {
return PrefixTree{
children: map[rune]*PrefixTree{},
isWord: false,
}
}

func (this *PrefixTree) Insert(word string) {
cur := this
for _, c := range word {
if _, ok := cur.children[c]; !ok {
child := Constructor()
cur.children[c] = &child
cur = cur.children[c]
} else {
cur = cur.children[c]
}
}
cur.isWord = true
}

func (this *PrefixTree) Search(word string) bool {
cur := this
for _, c := range word {
if _, ok := cur.children[c]; ok {
cur = cur.children[c]
} else {
return false
}
}
return cur.isWord
}

func (this *PrefixTree) StartsWith(prefix string) bool {
cur := this
for _, c := range prefix {
if _, ok := cur.children[c]; ok {
cur = cur.children[c]
} else {
return false
}
}

return cur != nil
}
67 changes: 67 additions & 0 deletions word-break/tedkimdev.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// TC: O((n * t) + (m * t))
// SC: O(n + (m * t))
func wordBreak(s string, wordDict []string) bool {
trie := NewTrieNode()
maxLength := 0
for _, word := range wordDict {
trie.Insert(word)
if len(word) > maxLength {
maxLength = len(word)
}
}

dp := make([]bool, len(s)+1)
dp[len(s)] = true

for i := len(s) - 1; i >= 0; i-- {
node := trie
for j := i; j < len(s) && j < i+maxLength; j++ {
c := s[j]
if _, ok := node.children[rune(c)]; !ok {
break
}
node = node.children[rune(c)]

if node.isWord && dp[j+1] {
dp[i] = true
break
}
}
}

return dp[0]
}

type TrieNode struct {
children map[rune]*TrieNode
isWord bool
}

func NewTrieNode() *TrieNode {
return &TrieNode{children: make(map[rune]*TrieNode)}
}

func (t *TrieNode) Insert(word string) {
node := t
for _, char := range word {
if _, ok := node.children[char]; !ok {
node.children[char] = NewTrieNode()
}
node = node.children[char]
}
node.isWord = true
}

// func (t *TrieNode) Search(s string, i, j int) bool {
// node := t
// for idx := i; idx <= j; idx++ {
// char := rune(s[idx])
// if _, ok := node.children[char]; !ok {
// return false
// }
// node = node.children[char]
// }
// return node.isWord
// }


Loading