From 2261b21251e521276989122a2ef704711a568eb6 Mon Sep 17 00:00:00 2001 From: jinvicky Date: Mon, 21 Jul 2025 11:16:24 +0900 Subject: [PATCH 01/13] contains duplicate solution --- contains-duplicate/jinvicky.java | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 contains-duplicate/jinvicky.java diff --git a/contains-duplicate/jinvicky.java b/contains-duplicate/jinvicky.java new file mode 100644 index 000000000..a0536551f --- /dev/null +++ b/contains-duplicate/jinvicky.java @@ -0,0 +1,11 @@ +import java.util.*; + +class Solution { + public boolean containsDuplicate(int[] nums) { + Set set = new HashSet<>(); + for(int num : nums) { + if(!set.add(num)) return true; + } + return false; + } +} From 846f4ae253229c2bf0d64dfb5bec550b709a1a6f Mon Sep 17 00:00:00 2001 From: jinvicky Date: Mon, 21 Jul 2025 12:50:45 +0900 Subject: [PATCH 02/13] two sum solution --- two-sum/jinvicky.java | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 two-sum/jinvicky.java diff --git a/two-sum/jinvicky.java b/two-sum/jinvicky.java new file mode 100644 index 000000000..f7e47f94e --- /dev/null +++ b/two-sum/jinvicky.java @@ -0,0 +1,35 @@ +import java.util.HashMap; +import java.util.Map; + +/** + 본래 brute force로 이중 for문으로 풀었다가 map으로 최적화. + */ +class Solution { + /** + * brute force 풀이 + */ +// public int[] twoSumByBruteForce(int[] nums, int target) { +// for (int i = 0; i < nums.length; i++) { +// for (int j = i+1; j < nums.length; j++) { +// if (nums[i] + nums[j] == target) { +// return new int[] { i, j }; +// } +// } +// } +// return new int[2]; +// } + + public int[] twoSum(int[] nums, int target) { + Map numberMap = new HashMap<>(); + for (int i = 0; i < nums.length; i++) { + int required = target - nums[i]; + Integer index = numberMap.get(required); + + if (index != null) { + return new int[] { index, i }; + } + numberMap.put(nums[i], i); + } + return new int[2]; + } +} \ No newline at end of file From 1fac60a4c52bc0d0e41157bd833037cb67e832f7 Mon Sep 17 00:00:00 2001 From: jinvicky Date: Mon, 21 Jul 2025 12:51:56 +0900 Subject: [PATCH 03/13] lint fix --- contains-duplicate/jinvicky.java | 4 ++-- two-sum/jinvicky.java | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/contains-duplicate/jinvicky.java b/contains-duplicate/jinvicky.java index a0536551f..de8319e22 100644 --- a/contains-duplicate/jinvicky.java +++ b/contains-duplicate/jinvicky.java @@ -3,8 +3,8 @@ class Solution { public boolean containsDuplicate(int[] nums) { Set set = new HashSet<>(); - for(int num : nums) { - if(!set.add(num)) return true; + for (int num : nums) { + if (!set.add(num)) return true; } return false; } diff --git a/two-sum/jinvicky.java b/two-sum/jinvicky.java index f7e47f94e..4429ee3d7 100644 --- a/two-sum/jinvicky.java +++ b/two-sum/jinvicky.java @@ -2,7 +2,7 @@ import java.util.Map; /** - 본래 brute force로 이중 for문으로 풀었다가 map으로 최적화. + * 본래 brute force로 이중 for문으로 풀었다가 map으로 최적화. */ class Solution { /** @@ -18,7 +18,6 @@ class Solution { // } // return new int[2]; // } - public int[] twoSum(int[] nums, int target) { Map numberMap = new HashMap<>(); for (int i = 0; i < nums.length; i++) { @@ -26,7 +25,7 @@ public int[] twoSum(int[] nums, int target) { Integer index = numberMap.get(required); if (index != null) { - return new int[] { index, i }; + return new int[]{index, i}; } numberMap.put(nums[i], i); } From 90b51aa78b9aef193dc2e1e97249b2bee2526368 Mon Sep 17 00:00:00 2001 From: jinvicky Date: Mon, 21 Jul 2025 12:53:38 +0900 Subject: [PATCH 04/13] lint fix --- two-sum/jinvicky.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/two-sum/jinvicky.java b/two-sum/jinvicky.java index 4429ee3d7..550339323 100644 --- a/two-sum/jinvicky.java +++ b/two-sum/jinvicky.java @@ -5,14 +5,15 @@ * 본래 brute force로 이중 for문으로 풀었다가 map으로 최적화. */ class Solution { + /** * brute force 풀이 */ // public int[] twoSumByBruteForce(int[] nums, int target) { // for (int i = 0; i < nums.length; i++) { -// for (int j = i+1; j < nums.length; j++) { +// for (int j = i + 1; j < nums.length; j++) { // if (nums[i] + nums[j] == target) { -// return new int[] { i, j }; +// return new int[]{i, j}; // } // } // } @@ -20,6 +21,7 @@ class Solution { // } public int[] twoSum(int[] nums, int target) { Map numberMap = new HashMap<>(); + for (int i = 0; i < nums.length; i++) { int required = target - nums[i]; Integer index = numberMap.get(required); @@ -27,8 +29,10 @@ public int[] twoSum(int[] nums, int target) { if (index != null) { return new int[]{index, i}; } + numberMap.put(nums[i], i); } + return new int[2]; } -} \ No newline at end of file +} From 4f3d5a2336de76a45d39f51077081947ddba11bd Mon Sep 17 00:00:00 2001 From: jinvicky Date: Mon, 21 Jul 2025 12:54:40 +0900 Subject: [PATCH 05/13] lint fix --- two-sum/jinvicky.java | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/two-sum/jinvicky.java b/two-sum/jinvicky.java index 550339323..aed3a75a5 100644 --- a/two-sum/jinvicky.java +++ b/two-sum/jinvicky.java @@ -6,19 +6,17 @@ */ class Solution { - /** - * brute force 풀이 - */ -// public int[] twoSumByBruteForce(int[] nums, int target) { -// for (int i = 0; i < nums.length; i++) { -// for (int j = i + 1; j < nums.length; j++) { -// if (nums[i] + nums[j] == target) { -// return new int[]{i, j}; -// } -// } -// } -// return new int[2]; -// } + public int[] twoSumByBruteForce(int[] nums, int target) { + for (int i = 0; i < nums.length; i++) { + for (int j = i + 1; j < nums.length; j++) { + if (nums[i] + nums[j] == target) { + return new int[]{i, j}; + } + } + } + return new int[2]; + } + public int[] twoSum(int[] nums, int target) { Map numberMap = new HashMap<>(); From d4ab0ddd9bb6681cda1d26df86ef616b8dac8074 Mon Sep 17 00:00:00 2001 From: jinvicky Date: Tue, 22 Jul 2025 13:20:10 +0900 Subject: [PATCH 06/13] top k frequent elements solution --- top-k-frequent-elements/jinvicky.java | 48 +++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 top-k-frequent-elements/jinvicky.java diff --git a/top-k-frequent-elements/jinvicky.java b/top-k-frequent-elements/jinvicky.java new file mode 100644 index 000000000..eff29106a --- /dev/null +++ b/top-k-frequent-elements/jinvicky.java @@ -0,0 +1,48 @@ +import java.util.HashMap; +import java.util.Map; +import java.util.PriorityQueue; + +/** + * [풀이] + * 1. <숫자: 빈도수>를 저장하는 HashMap과 [빈도수, 숫자]를 저장하는 PriorityQueue를 선언한다. + * 2. HashMap에 숫자별로 빈도수를 함께 저장해서 해시테이블을 만든다. + * 3. + *

+ * [우선순위 큐에 사용된 자료구조] + * 1. 별도 클래스를 선언 + * 2. 요구사항 자료형 배열을 선언한다. + * 처음에는 별도 클래스를 선언했다가 값이 2개이며 알고리즘 로직 자체가 어려워서 int[] 구조로 풀이했다. + * (주로 알고리즘이 어려우면 가독성이 나쁘더라도 자료구조를 단순화하는 습관이 있다) + *

+ *

+ * [어려웠던 점] + * 1. 우선순위 큐는 매번 요소가 추가될 때마다 내부 정렬을 수행하기 때문에 연산을 수행하면서 k개를 유지해야 한다. + * 또한 기존 [빈도수, 숫자]를 버려야만 올바른 답을 도출할 수 있었다. + * 2. [숫자, 빈도수]로 저장하는 것만 생각했더니 내부 정렬을 어떻게 하지 못해서 굉장히 고민했다. 정답은 반대였다. + *

+ */ +class Solution { + public int[] topKFrequent(int[] nums, int k) { + int[] answer = new int[k]; + + Map map = new HashMap<>(); + for (int n : nums) { + map.put(n, map.getOrDefault(n, 0) + 1); + } + PriorityQueue pq = new PriorityQueue<>((a, b) -> a[0] - b[0]); + + for (int key : map.keySet()) { + pq.add(new int[]{map.get(key), key}); + if (pq.size() > k) { + pq.poll(); + } + } + + for (int i = 0; i < k; i++) { + if (!pq.isEmpty()) { + answer[i] = pq.poll()[1]; + } + } + return answer; + } +} \ No newline at end of file From d12f16a04023e44a44d15f4d14bc8a24f97f1776 Mon Sep 17 00:00:00 2001 From: jinvicky Date: Tue, 22 Jul 2025 13:22:06 +0900 Subject: [PATCH 07/13] fix javadoc format, lint --- top-k-frequent-elements/jinvicky.java | 34 ++++++++++++--------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/top-k-frequent-elements/jinvicky.java b/top-k-frequent-elements/jinvicky.java index eff29106a..20027f05d 100644 --- a/top-k-frequent-elements/jinvicky.java +++ b/top-k-frequent-elements/jinvicky.java @@ -2,25 +2,21 @@ import java.util.Map; import java.util.PriorityQueue; -/** - * [풀이] - * 1. <숫자: 빈도수>를 저장하는 HashMap과 [빈도수, 숫자]를 저장하는 PriorityQueue를 선언한다. - * 2. HashMap에 숫자별로 빈도수를 함께 저장해서 해시테이블을 만든다. - * 3. - *

- * [우선순위 큐에 사용된 자료구조] - * 1. 별도 클래스를 선언 - * 2. 요구사항 자료형 배열을 선언한다. - * 처음에는 별도 클래스를 선언했다가 값이 2개이며 알고리즘 로직 자체가 어려워서 int[] 구조로 풀이했다. - * (주로 알고리즘이 어려우면 가독성이 나쁘더라도 자료구조를 단순화하는 습관이 있다) - *

- *

- * [어려웠던 점] - * 1. 우선순위 큐는 매번 요소가 추가될 때마다 내부 정렬을 수행하기 때문에 연산을 수행하면서 k개를 유지해야 한다. - * 또한 기존 [빈도수, 숫자]를 버려야만 올바른 답을 도출할 수 있었다. - * 2. [숫자, 빈도수]로 저장하는 것만 생각했더니 내부 정렬을 어떻게 하지 못해서 굉장히 고민했다. 정답은 반대였다. - *

- */ +///** +// * [풀이] +// * 1. <숫자: 빈도수>를 저장하는 HashMap과 [빈도수, 숫자]를 저장하는 PriorityQueue를 선언한다. +// * 2. HashMap에 숫자별로 빈도수를 함께 저장해서 해시테이블을 만든다. +// * 3. +// * [우선순위 큐에 사용된 자료구조] +// * 1. 별도 클래스를 선언 +// * 2. 요구사항 자료형 배열을 선언한다. +// * 처음에는 별도 클래스를 선언했다가 값이 2개이며 알고리즘 로직 자체가 어려워서 int[] 구조로 풀이했다. +// * (주로 알고리즘이 어려우면 가독성이 나쁘더라도 자료구조를 단순화하는 습관이 있다) +// * [어려웠던 점] +// * 1. 우선순위 큐는 매번 요소가 추가될 때마다 내부 정렬을 수행하기 때문에 연산을 수행하면서 k개를 유지해야 한다. +// * 또한 기존 [빈도수, 숫자]를 버려야만 올바른 답을 도출할 수 있었다. +// * 2. [숫자, 빈도수]로 저장하는 것만 생각했더니 내부 정렬을 어떻게 하지 못해서 굉장히 고민했다. 정답은 반대였다. +// */ class Solution { public int[] topKFrequent(int[] nums, int k) { int[] answer = new int[k]; From 498eef8f8570deb4a11afe05876ce627ac7c4f0d Mon Sep 17 00:00:00 2001 From: jinvicky Date: Tue, 22 Jul 2025 13:22:58 +0900 Subject: [PATCH 08/13] fix lint --- top-k-frequent-elements/jinvicky.java | 29 +++++++++++++-------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/top-k-frequent-elements/jinvicky.java b/top-k-frequent-elements/jinvicky.java index 20027f05d..fd7b9c33d 100644 --- a/top-k-frequent-elements/jinvicky.java +++ b/top-k-frequent-elements/jinvicky.java @@ -2,21 +2,20 @@ import java.util.Map; import java.util.PriorityQueue; -///** -// * [풀이] -// * 1. <숫자: 빈도수>를 저장하는 HashMap과 [빈도수, 숫자]를 저장하는 PriorityQueue를 선언한다. -// * 2. HashMap에 숫자별로 빈도수를 함께 저장해서 해시테이블을 만든다. -// * 3. -// * [우선순위 큐에 사용된 자료구조] -// * 1. 별도 클래스를 선언 -// * 2. 요구사항 자료형 배열을 선언한다. -// * 처음에는 별도 클래스를 선언했다가 값이 2개이며 알고리즘 로직 자체가 어려워서 int[] 구조로 풀이했다. -// * (주로 알고리즘이 어려우면 가독성이 나쁘더라도 자료구조를 단순화하는 습관이 있다) -// * [어려웠던 점] -// * 1. 우선순위 큐는 매번 요소가 추가될 때마다 내부 정렬을 수행하기 때문에 연산을 수행하면서 k개를 유지해야 한다. -// * 또한 기존 [빈도수, 숫자]를 버려야만 올바른 답을 도출할 수 있었다. -// * 2. [숫자, 빈도수]로 저장하는 것만 생각했더니 내부 정렬을 어떻게 하지 못해서 굉장히 고민했다. 정답은 반대였다. -// */ +// [풀이] +// 1. <숫자: 빈도수>를 저장하는 HashMap과 [빈도수, 숫자]를 저장하는 PriorityQueue를 선언한다. +// 2. HashMap에 숫자별로 빈도수를 함께 저장해서 해시테이블을 만든다. +// 3. +// [우선순위 큐에 사용된 자료구조] +// 1. 별도 클래스를 선언 +// 2. 요구사항 자료형 배열을 선언한다. +// 처음에는 별도 클래스를 선언했다가 값이 2개이며 알고리즘 로직 자체가 어려워서 int[] 구조로 풀이했다. +// (주로 알고리즘이 어려우면 가독성이 나쁘더라도 자료구조를 단순화하는 습관이 있다) +// [어려웠던 점] +// 1. 우선순위 큐는 매번 요소가 추가될 때마다 내부 정렬을 수행하기 때문에 연산을 수행하면서 k개를 유지해야 한다. +// 또한 기존 [빈도수, 숫자]를 버려야만 올바른 답을 도출할 수 있었다. +// 2. [숫자, 빈도수]로 저장하는 것만 생각했더니 내부 정렬을 어떻게 하지 못해서 굉장히 고민했다. 정답은 반대였다. +// class Solution { public int[] topKFrequent(int[] nums, int k) { int[] answer = new int[k]; From ed4e7c1b7bb122252d3990497b9c512106b5b0b3 Mon Sep 17 00:00:00 2001 From: jinvicky Date: Tue, 22 Jul 2025 13:24:34 +0900 Subject: [PATCH 09/13] fix lint --- top-k-frequent-elements/jinvicky.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/top-k-frequent-elements/jinvicky.java b/top-k-frequent-elements/jinvicky.java index fd7b9c33d..af6423e9a 100644 --- a/top-k-frequent-elements/jinvicky.java +++ b/top-k-frequent-elements/jinvicky.java @@ -5,7 +5,6 @@ // [풀이] // 1. <숫자: 빈도수>를 저장하는 HashMap과 [빈도수, 숫자]를 저장하는 PriorityQueue를 선언한다. // 2. HashMap에 숫자별로 빈도수를 함께 저장해서 해시테이블을 만든다. -// 3. // [우선순위 큐에 사용된 자료구조] // 1. 별도 클래스를 선언 // 2. 요구사항 자료형 배열을 선언한다. @@ -15,7 +14,7 @@ // 1. 우선순위 큐는 매번 요소가 추가될 때마다 내부 정렬을 수행하기 때문에 연산을 수행하면서 k개를 유지해야 한다. // 또한 기존 [빈도수, 숫자]를 버려야만 올바른 답을 도출할 수 있었다. // 2. [숫자, 빈도수]로 저장하는 것만 생각했더니 내부 정렬을 어떻게 하지 못해서 굉장히 고민했다. 정답은 반대였다. -// + class Solution { public int[] topKFrequent(int[] nums, int k) { int[] answer = new int[k]; From eacf342451c1a706945ffb94f70b367f00f83da6 Mon Sep 17 00:00:00 2001 From: jinvicky Date: Tue, 22 Jul 2025 13:28:36 +0900 Subject: [PATCH 10/13] fix lint --- top-k-frequent-elements/jinvicky.java | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/top-k-frequent-elements/jinvicky.java b/top-k-frequent-elements/jinvicky.java index af6423e9a..7f504ae51 100644 --- a/top-k-frequent-elements/jinvicky.java +++ b/top-k-frequent-elements/jinvicky.java @@ -2,21 +2,21 @@ import java.util.Map; import java.util.PriorityQueue; -// [풀이] -// 1. <숫자: 빈도수>를 저장하는 HashMap과 [빈도수, 숫자]를 저장하는 PriorityQueue를 선언한다. -// 2. HashMap에 숫자별로 빈도수를 함께 저장해서 해시테이블을 만든다. -// [우선순위 큐에 사용된 자료구조] -// 1. 별도 클래스를 선언 -// 2. 요구사항 자료형 배열을 선언한다. -// 처음에는 별도 클래스를 선언했다가 값이 2개이며 알고리즘 로직 자체가 어려워서 int[] 구조로 풀이했다. -// (주로 알고리즘이 어려우면 가독성이 나쁘더라도 자료구조를 단순화하는 습관이 있다) -// [어려웠던 점] -// 1. 우선순위 큐는 매번 요소가 추가될 때마다 내부 정렬을 수행하기 때문에 연산을 수행하면서 k개를 유지해야 한다. -// 또한 기존 [빈도수, 숫자]를 버려야만 올바른 답을 도출할 수 있었다. -// 2. [숫자, 빈도수]로 저장하는 것만 생각했더니 내부 정렬을 어떻게 하지 못해서 굉장히 고민했다. 정답은 반대였다. - class Solution { public int[] topKFrequent(int[] nums, int k) { + // [풀이] + // 1. <숫자: 빈도수>를 저장하는 HashMap과 [빈도수, 숫자]를 저장하는 PriorityQueue를 선언한다. + // 2. HashMap에 숫자별로 빈도수를 함께 저장해서 해시테이블을 만든다. + // [우선순위 큐에 사용된 자료구조] + // 1. 별도 클래스를 선언 + // 2. 요구사항 자료형 배열을 선언한다. + // 처음에는 별도 클래스를 선언했다가 값이 2개이며 알고리즘 로직 자체가 어려워서 int[] 구조로 풀이했다. + // (주로 알고리즘이 어려우면 가독성이 나쁘더라도 자료구조를 단순화하는 습관이 있다) + // [어려웠던 점] + // 1. 우선순위 큐는 매번 요소가 추가될 때마다 내부 정렬을 수행하기 때문에 연산을 수행하면서 k개를 유지해야 한다. + // 또한 기존 [빈도수, 숫자]를 버려야만 올바른 답을 도출할 수 있었다. + // 2. [숫자, 빈도수]로 저장하는 것만 생각했더니 내부 정렬을 어떻게 하지 못해서 굉장히 고민했다. 정답은 반대였다. + int[] answer = new int[k]; Map map = new HashMap<>(); From 50e1fb58a8963ae34c3138f08c26b87713c4d595 Mon Sep 17 00:00:00 2001 From: jinvicky Date: Tue, 22 Jul 2025 13:29:14 +0900 Subject: [PATCH 11/13] fix missing end line breaks --- top-k-frequent-elements/jinvicky.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/top-k-frequent-elements/jinvicky.java b/top-k-frequent-elements/jinvicky.java index 7f504ae51..005411266 100644 --- a/top-k-frequent-elements/jinvicky.java +++ b/top-k-frequent-elements/jinvicky.java @@ -39,4 +39,4 @@ public int[] topKFrequent(int[] nums, int k) { } return answer; } -} \ No newline at end of file +} From c7312a01ec073646f1cd40b4a239157b0876c082 Mon Sep 17 00:00:00 2001 From: jinvicky Date: Wed, 23 Jul 2025 14:19:51 +0900 Subject: [PATCH 12/13] longest consecutive sequence solution --- longest-consecutive-sequence/jinvicky.java | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 longest-consecutive-sequence/jinvicky.java diff --git a/longest-consecutive-sequence/jinvicky.java b/longest-consecutive-sequence/jinvicky.java new file mode 100644 index 000000000..01e9cf62a --- /dev/null +++ b/longest-consecutive-sequence/jinvicky.java @@ -0,0 +1,32 @@ +import java.util.HashSet; +import java.util.Set; + +// 연속적인 숫자의 길이를 구하는 것이기 때문에 이전, 다음 수가 집합의 일부인지를 파악해야 한다. +// map, set 자료구조를 사용하면 조회 성능을 O(1)로 높일 수 있다. +// 어려웠던 점은 연속적인 숫자의 start가 되냐 여부 조건을 떠올리는 것이었다. while문이 약해서 length++하는 로직이 힘들었다. +// 문제의 조건은 배열 내에서의 연속적인 숫자의 길이이기 때문에 while을 사용해도 성능 이슈 걱정할 필요가 없었다. +class Solution { + public int longestConsecutive(int[] nums) { + Set set = new HashSet<>(); + + for (int n : nums) { + set.add(n); + } + + int maxLength = 0; + + for (int n : nums) { + if (!set.contains(n - 1)) { // 내 이전 숫자가 집합에 없다 == 내가 최소 숫자다. + int length = 1; + + while (set.contains(n + length)) { + length++; + } + + maxLength = Math.max(length, maxLength); + } + } + + return maxLength; + } +} From 842cba972a5659cea2d97b874618855259c35749 Mon Sep 17 00:00:00 2001 From: jinvicky Date: Fri, 25 Jul 2025 16:57:09 +0900 Subject: [PATCH 13/13] house robber solution --- house-robber/jinvicky.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 house-robber/jinvicky.java diff --git a/house-robber/jinvicky.java b/house-robber/jinvicky.java new file mode 100644 index 000000000..b6b6354a7 --- /dev/null +++ b/house-robber/jinvicky.java @@ -0,0 +1,19 @@ +//dp[0] -> 1번째 집 털이 수완으로 초기화 +//dp[1] -> -2집 털이+지금집 털이가 -1집 털이보다 수완이 좋다. = -2집 털이(0)+지금집 털이 = 7 +//dp[2] -> -2집 털이+지금집 털이가 -1집 털이보다 수완이 좋다. = -2집 털이+지금집 털이 = 11 +//dp[3] -> -2집 털이+지금집 털이가 -1집 털이보다 수완이 좋다. = -2집 털이+지금집 털이 = 11 (>10) +//dp[4] -> -2집 털이+지금집 털이가 -1집 털이보다 수완이 좋다. = -2집 털이+지금집 털이 = 12 (>10) +class Solution { + public int rob(int[] nums) { + int[] dp = new int[nums.length]; + dp[0] = nums[0]; + + for (int i = 1; i < nums.length; i++) { + int prev2AndNowRob = (i - 2 < 0 ? 0 : dp[i - 2]) + nums[i]; + int prev1Rob = dp[i - 1]; + + dp[i] = Math.max(prev2AndNowRob, prev1Rob); + } + return dp[nums.length - 1]; + } +}