File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ // νμ΄
2+ // dpλ₯Ό μ¬μ©νμ¬ νμ¬ νΈ μ μλ μ΅λνμ λμ κ³μ°
3+ // currμ΄ prevκ° λκ³ , prevμλ κ°μ΄ μλ‘μ΄ κ°μ λν κ²κ³Ό curr μ΄μλ κ°μ μ΅λκ°μ λΉκ΅ν κ²μ΄ μλ‘μ΄ currμ΄ λλ€.
4+ // λ§μ§λ§μ prevμ currμ μ΅λκ°μ λΉκ΅
5+ // μ΄λ κ² νλ©΄ νΈ μ μλ μ§μ μ΅λκ°μ κ³μ κ°μ§κ³ μμ μ μκ² λ¨.
6+
7+ // TC
8+ // O(n)
9+
10+ // SC
11+ // λμ΄λμ§ μλ int κ°λ§ μ¬μ©νμΌλ―λ‘ O(1)
12+
13+ func rob (nums []int ) int {
14+ length := len (nums )
15+
16+ if length == 1 {
17+ return nums [0 ]
18+ }
19+
20+ prev := 0
21+ curr := nums [0 ]
22+
23+ for i := 1 ; i < length ; i ++ {
24+ prev , curr = curr , max (nums [i ]+ prev , curr )
25+ }
26+
27+ return max (prev , curr )
28+ }
29+
30+ func max (a , b int ) int {
31+ if a > b {
32+ return a
33+ } else {
34+ return b
35+ }
36+ }
You canβt perform that action at this time.
0 commit comments