File tree Expand file tree Collapse file tree 2 files changed +40
-11
lines changed
Expand file tree Collapse file tree 2 files changed +40
-11
lines changed Original file line number Diff line number Diff line change 11package climbing_stairs
22
3- import (
4- "fmt"
5- )
6-
3+ /**
4+ * 풀이: 이전 스텝을 계산하여 고정 한후 다음 조건을 확인하는 것에서 dp유형의 문제라는 생각이 들었지만
5+ * dp가 뭔지몰라 무서워 패턴은 못찾았습니다! (감정적)
6+ * 그래서 힌트 보고 피보나치 수열인 것을 파악 후 재귀하지않는 방식으로 풀었습니다.
7+ * 풀면서 피보나치도 dp의 방식으로 풀 수 있다는 걸 명확하게 알았네요. (memoization)
8+ */
79func climbStairs (n int ) int {
810 return fibo (n )
911}
1012
1113func fibo (n int ) int {
1214 ret := []int {0 , 1 }
13-
1415 for i := 2 ; i <= n ; i ++ {
1516 ret = append (ret , ret [i - 1 ]+ ret [i - 2 ])
1617 }
17-
1818 ret = ret [len (ret )- 2 :]
19- fmt . Printf ( "ret=%v \n " , ret )
19+
2020 sum := 0
2121 for _ , n := range ret {
2222 sum += n
Original file line number Diff line number Diff line change 11package valid_anagram
22
3+ /**
4+ * 풀이:
5+ * t가 s의 anagram인지 확인하는 문제!
6+ * s를 hashmap에 맵핑하고, t의 rune r을 하나씩 s[r] 로 갯수를 체크하여 풀음.
7+ * anagram이면 return true, otherwise false
8+ * true 까지 조건 3개가 있음.
9+ *
10+ * TC: O(N) -> s나 t의 길이 만큼만 돌음
11+ * SC: O(N) -> M * N에서 최고차항 정리
12+ */
313func isAnagram (s string , t string ) bool {
414 ht := make (map [rune ]int , len (s ))
515 for _ , r := range s {
@@ -8,21 +18,40 @@ func isAnagram(s string, t string) bool {
818
919 for _ , r := range t {
1020 cnt , ok := ht [r ]
11- if ! ok {
21+ if ! ok { // 1. t에 존재하지 않는 rune -> false
1222 return false
1323 }
1424
15- ht [r ] -= 1
16- if cnt - 1 < 0 {
25+ ht [r ] -= 1 // t의 rune이 s에 존재하므로 갯수 1 차감
26+ if cnt - 1 < 0 { // 2. s에 있는 rune보다 더 많이 가지고 있음
1727 return false
1828 }
1929 }
2030
2131 for _ , v := range ht {
22- if v > 0 {
32+ if v > 0 { // 3. t를 순회하고 s에 남아있는게 있어도 false
2333 return false
2434 }
2535 }
2636
2737 return true
2838}
39+
40+ func isAnagram_faster (s string , t string ) bool {
41+ if len (s ) != len (t ) { // 둘이 길이가 다르면 당연히 실패함
42+ return false
43+ }
44+
45+ ht := make (map [byte ]int , len (s ))
46+ for i , _ := range s {
47+ ht [s [i ]]++
48+ ht [t [i ]]-- // 기존에 푼 방식에서 ok 체크 안해버림? ..
49+ }
50+
51+ for _ , v := range ht {
52+ if v != 0 {
53+ return false
54+ }
55+ }
56+ return true
57+ }
You can’t perform that action at this time.
0 commit comments