File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Runtime: 0ms
3+ * Time Complexity: O(n)
4+ *
5+ * Memory: 42.72MB
6+ * Space Complexity: O(1)
7+ *
8+ * Approach: λ κ°μ§ λ³μλ§ μ¬μ©ν DP μ κ·Όλ²
9+ * - withoutPrevHouse: μ΄μ μ§μ νΈμ§ μμμ λμ μ΅λ κΈμ‘
10+ * - withPrevHouse: μ΄μ μ§κΉμ§ κ³ λ €ν μ΅λ κΈμ‘
11+ * - κ° μ§μμ "νΈκ±°λ μ νΈκ±°λ" λ κ°μ§ μ νμ§ μ€ μ΅λκ° μ ν
12+ * 1) νμ¬ μ§μ μ νΈλ©΄: μ΄μ μ§κΉμ§μ μ΅λκ° μ μ§
13+ * 2) νμ¬ μ§μ νΈλ©΄: μ μ μ§κΉμ§μ μ΅λκ° + νμ¬ μ§ κΈμ‘
14+ */
15+ class Solution {
16+ public int rob (int [] nums ) {
17+ int withoutPrevHouse = 0 ;
18+ int withPrevHouse = 0 ;
19+
20+ for (int money : nums ) {
21+ int maxMoney = Math .max (withPrevHouse , withoutPrevHouse +money );
22+ withoutPrevHouse = withPrevHouse ;
23+ withPrevHouse = maxMoney ;
24+ }
25+
26+ return withPrevHouse ;
27+ }
28+ }
You canβt perform that action at this time.
0 commit comments