Skip to content

[Invidam] Solve Week01 Problems (5/5) #GoLang #36

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

Merged
merged 13 commits into from
May 3, 2024
10 changes: 10 additions & 0 deletions best-time-to-buy-and-sell-stock/invidam.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
func maxProfit(prices []int) int {
purchasePrice := prices[0]
maxBenefit := 0

for _, price := range prices {
purchasePrice = min(purchasePrice, price)
maxBenefit = max(maxBenefit, price-purchasePrice)
}
return maxBenefit
}
9 changes: 9 additions & 0 deletions contains-duplicate/invidam.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
func containsDuplicate(nums []int) bool {
appeared := make(map[int]bool)

for _, num := range nums {
appeared[num] = true
}

return len(appeared) != len(nums)
}
10 changes: 10 additions & 0 deletions two-sum/invidam.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
func twoSum(nums []int, target int) []int {
need := make(map[int]int, len(nums))
for i, n := range nums {
if j, ok := need[n]; ok {
return []int{i, j}
}
need[target-n] = i
}
return nil
}
23 changes: 23 additions & 0 deletions valid-anagram/invidam.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
func isAnagram(s string, t string) bool {
freqS := make(map[rune]int, 26)
freqT := make(map[rune]int, 26)

for _, ch := range s {
if _, ok := freqS[ch]; ok {
freqS[ch]++
} else {
freqS[ch] = 1
}
}
for _, ch := range t {
freqT[ch]++
}

for ch := 'a'; ch <= 'z'; ch++ {
if diff := freqS[ch] - freqT[ch]; diff != 0 {
return false
}
}

return true
}
15 changes: 15 additions & 0 deletions valid-palindrome/invidam.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
func isPalindrome(s string) bool {
filtered := strings.Map(func(r rune) rune {
if !unicode.IsLetter(r) && !unicode.IsNumber(r) {
return -1
}
return unicode.ToLower(r)
}, s)

for ldx, rdx := 0, len(filtered) - 1; ldx < rdx; ldx, rdx = ldx + 1, rdx - 1 {
if filtered[ldx] != filtered[rdx] {
return false
}
}
return true
}