Skip to content

Commit 03cbc7f

Browse files
authored
Merge pull request #36 from Invidam/week1-invidam
[Invidam] Solve Week01 Problems (5/5) #GoLang
2 parents 33f93b2 + 59f2aed commit 03cbc7f

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func maxProfit(prices []int) int {
2+
purchasePrice := prices[0]
3+
maxBenefit := 0
4+
5+
for _, price := range prices {
6+
purchasePrice = min(purchasePrice, price)
7+
maxBenefit = max(maxBenefit, price-purchasePrice)
8+
}
9+
return maxBenefit
10+
}

contains-duplicate/invidam.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func containsDuplicate(nums []int) bool {
2+
appeared := make(map[int]bool)
3+
4+
for _, num := range nums {
5+
appeared[num] = true
6+
}
7+
8+
return len(appeared) != len(nums)
9+
}

two-sum/invidam.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func twoSum(nums []int, target int) []int {
2+
need := make(map[int]int, len(nums))
3+
for i, n := range nums {
4+
if j, ok := need[n]; ok {
5+
return []int{i, j}
6+
}
7+
need[target-n] = i
8+
}
9+
return nil
10+
}

valid-anagram/invidam.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
func isAnagram(s string, t string) bool {
2+
freqS := make(map[rune]int, 26)
3+
freqT := make(map[rune]int, 26)
4+
5+
for _, ch := range s {
6+
if _, ok := freqS[ch]; ok {
7+
freqS[ch]++
8+
} else {
9+
freqS[ch] = 1
10+
}
11+
}
12+
for _, ch := range t {
13+
freqT[ch]++
14+
}
15+
16+
for ch := 'a'; ch <= 'z'; ch++ {
17+
if diff := freqS[ch] - freqT[ch]; diff != 0 {
18+
return false
19+
}
20+
}
21+
22+
return true
23+
}

valid-palindrome/invidam.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func isPalindrome(s string) bool {
2+
filtered := strings.Map(func(r rune) rune {
3+
if !unicode.IsLetter(r) && !unicode.IsNumber(r) {
4+
return -1
5+
}
6+
return unicode.ToLower(r)
7+
}, s)
8+
9+
for ldx, rdx := 0, len(filtered) - 1; ldx < rdx; ldx, rdx = ldx + 1, rdx - 1 {
10+
if filtered[ldx] != filtered[rdx] {
11+
return false
12+
}
13+
}
14+
return true
15+
}

0 commit comments

Comments
 (0)