Skip to content

Commit f978922

Browse files
authored
Merge pull request DaleStudy#2100 from youngDaLee/main
[youngDaLee] WEEK 03 Solutions
2 parents f7c00e7 + 0871993 commit f978922

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

climbing-stairs/youngDaLee.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package youngDaLee
2+
3+
func climbStairs(n int) int {
4+
if n <= 2 {
5+
return n
6+
}
7+
8+
dp := make([]int, n+1)
9+
dp[1] = 1
10+
dp[2] = 2
11+
12+
for i := 3; i <= n; i++ {
13+
dp[i] = dp[i-1] + dp[i-2]
14+
}
15+
return dp[n]
16+
}
17+
18+
/*
19+
Limit Exceeded
20+
func climbStairs(n int) int {
21+
return dfs(0, n)
22+
}
23+
24+
func dfs(now, n int) int {
25+
if now > n {
26+
return 0
27+
}
28+
if now == n {
29+
return 1
30+
}
31+
32+
return dfs(now+1, n) + dfs(now+2, n)
33+
}
34+
*/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package youngDaLee
2+
3+
func productExceptSelf(nums []int) []int {
4+
n := len(nums)
5+
result := make([]int, n)
6+
7+
// Calculate left products
8+
leftProduct := 1
9+
for i := 0; i < n; i++ {
10+
result[i] = leftProduct
11+
leftProduct *= nums[i]
12+
}
13+
14+
// Calculate right products and combine with left products
15+
rightProduct := 1
16+
for i := n - 1; i >= 0; i-- {
17+
result[i] *= rightProduct
18+
rightProduct *= nums[i]
19+
}
20+
return result
21+
}

valid-anagram/youngDaLee.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package youngDaLee
2+
3+
import "strings"
4+
5+
func isAnagram(s string, t string) bool {
6+
if len(s) != len(t) {
7+
return false
8+
}
9+
10+
if s == t {
11+
return true
12+
}
13+
14+
sDict := make(map[string]int)
15+
sList := strings.Split(s, "")
16+
for _, data := range sList {
17+
if num, ok := sDict[data]; ok {
18+
sDict[data] = num + 1
19+
} else {
20+
sDict[data] = 1
21+
}
22+
}
23+
24+
tList := strings.Split(t, "")
25+
for _, data := range tList {
26+
if num, ok := sDict[data]; ok {
27+
sDict[data] = num - 1
28+
} else {
29+
return false
30+
}
31+
32+
if sDict[data] < 0 {
33+
return false
34+
}
35+
}
36+
37+
for _, num := range sDict {
38+
if num != 0 {
39+
return false
40+
}
41+
}
42+
43+
return true
44+
}

0 commit comments

Comments
 (0)