Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LeetCode 198-House Robber, 139-Word Break #740

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
29 changes: 29 additions & 0 deletions dynamic/houserobber.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// See https://leetcode.com/problems/house-robber/
// author: Danish Eqbal (https://github.com/dcode-github)
package dynamic

func HouseRobber(nums []int) int {

var max = func(i1, i2 int) int {
if i1 > i2 {
return i1
}
return i2
}

if len(nums) == 1 {
return nums[0]
}

if len(nums) == 2 {
return max(nums[0], nums[1])
}
dp := make([]int, len(nums))
dp[0] = nums[0]
dp[1] = max(nums[0], nums[1])
for i := 2; i < len(nums); i++ {
dp[i] = max(nums[i]+dp[i-2], dp[i-1])
}

return dp[len(nums)-1]
}
28 changes: 28 additions & 0 deletions dynamic/houserobber_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dynamic

import "testing"

func TestHouseRobber(t *testing.T) {
testCases := map[string]struct {
nums []int
want int
}{
"one house": {[]int{5}, 5},
"two houses": {[]int{1, 2}, 2},
"three houses": {[]int{1, 2, 3}, 4},
"four houses": {[]int{2, 7, 9, 3}, 11},
"five houses": {[]int{2, 7, 9, 3, 1}, 12},
"non-adjacent houses": {[]int{1, 3, 1, 3, 100}, 103},
"all houses same value": {[]int{10, 10, 10, 10}, 20},
"large values": {[]int{100, 1, 1, 100}, 200},
"stress test": {[]int{1, 2, 3, 1, 5, 1, 100, 1, 2}, 111},
}

for name, test := range testCases {
t.Run(name, func(t *testing.T) {
if got := HouseRobber(test.nums); got != test.want {
t.Errorf("rob(%v) = %v, want %v", test.nums, got, test.want)
}
})
}
}
31 changes: 31 additions & 0 deletions dynamic/wordbreak.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// See https://leetcode.com/problems/word-break/
// author: Danish Eqbal (https://github.com/dcode-github)
package dynamic

func WordBreak(s string, wordDict []string) bool {
return helper(s, wordDict, 0, map[int]bool{})
}

func helper(s string, wordDict []string, i int, dp map[int]bool) bool {
if val, ok := dp[i]; ok {
return val
}
if i == len(s) {
dp[i] = true
return dp[i]
}
for _, word := range wordDict {
if len(s[i:]) < len(word) {
continue
}
if s[i:i+len(word)] != word {
continue
}
if v := helper(s, wordDict, i+len(word), dp); v {
dp[i] = v
return true
}
}
dp[i] = false
return dp[i]
}
32 changes: 32 additions & 0 deletions dynamic/wordbreak_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dynamic

import "testing"

func TestWordBreak(t *testing.T) {
testCases := map[string]struct {
s string
wordDict []string
want bool
}{
"empty string and empty dict": {"", []string{}, true},
"empty string with non-empty dict": {"", []string{"a", "b"}, true},
"single word match": {"apple", []string{"apple"}, true},
"single word no match": {"apple", []string{"orange"}, false},
"two words forming a string": {"leetcode", []string{"leet", "code"}, true},
"two words but no match": {"applepen", []string{"apple", "pen"}, true},
"three words forming a string": {"applepenapple", []string{"apple", "pen"}, true},
"complex case": {"catsanddog", []string{"cat", "cats", "and", "sand", "dog"}, true},
"no possible segmentation": {"abcdef", []string{"ab", "cd", "def", "abcd"}, false},
"dict with overlapping words": {"pineapplepenapple", []string{"pine", "apple", "pen"}, true},
"single character match": {"a", []string{"a"}, true},
"single character no match": {"b", []string{"a"}, false},
}

for name, test := range testCases {
t.Run(name, func(t *testing.T) {
if got := WordBreak(test.s, test.wordDict); got != test.want {
t.Errorf("wordBreak(%q, %v) = %v, want %v", test.s, test.wordDict, got, test.want)
}
})
}
}