-
-
Notifications
You must be signed in to change notification settings - Fork 245
[eunhwa99] Week 03 #761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[eunhwa99] Week 03 #761
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8836835
two sum
eunhwa99 7b9d6ff
reverse bits
eunhwa99 5f2ae96
product of array except self
eunhwa99 1bd51e2
line 추가
eunhwa99 87246fb
Merge branch 'DaleStudy:main' into main
eunhwa99 59071f0
Merge branch 'DaleStudy:main' into main
eunhwa99 5a19363
combination sum
eunhwa99 2dca348
Merge remote-tracking branch 'origin/main'
eunhwa99 a6b54a4
maximum subarray
eunhwa99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
// backtracking | ||
// 시간복잡도: 각 배열 원소마다 target을 만드는 데에 기여를 할 수도 있고 안 할 수도 있음 -> O(2^(target)) | ||
// 공간복잡도: O(k * t) (k는 가능한 조합의 수, t는 각 조합의 크기) | ||
|
||
class Solution { | ||
public List<List<Integer>> combinationSum(int[] candidates, int target) { | ||
List<List<Integer>> result = new ArrayList<>(); | ||
backtrack(candidates, target, 0, new ArrayList<>(), result); | ||
return result; | ||
} | ||
|
||
private void backtrack(int[] candidates, int target, int start, List<Integer> currentCombination, List<List<Integer>> result) { | ||
// 목표값에 도달하면 현재 조합을 결과에 추가 | ||
if (target == 0) { | ||
result.add(new ArrayList<>(currentCombination)); | ||
return; | ||
} | ||
|
||
// 후보 숫자들을 탐색 | ||
for (int i = start; i < candidates.length; i++) { | ||
if (candidates[i] > target) continue; // 목표값보다 큰 숫자는 넘어감 | ||
|
||
currentCombination.add(candidates[i]); // 현재 숫자를 선택 | ||
// 현재 숫자를 다시 사용할 수 있기 때문에 i를 그대로 두고 재귀 호출 | ||
backtrack(candidates, target - candidates[i], i, currentCombination, result); | ||
currentCombination.remove(currentCombination.size() - 1); // 백트래킹: 마지막 숫자 제거 | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// 시간 복잡도: DP -> O(N) | ||
// 공간 복잡도: nums 배열 크기 - O(N) | ||
eunhwa99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class Solution { | ||
public int maxSubArray(int[] nums) { | ||
int currentSum = nums[0]; | ||
int maxSum = currentSum; | ||
for (int i = 1; i < nums.length; ++i) { | ||
currentSum = Math.max(currentSum + nums[i], nums[i]); | ||
maxSum = Math.max(maxSum, currentSum); | ||
} | ||
|
||
return maxSum; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// 풀이 | ||
// 현재 인덱스가 i 일 때, 문제에서 구하고자 하는 값은 아래와 같다. | ||
// 나의 왼쪽(i-1)부터 처음까지의 곱 * 나의 오른쪽(i+1)부터 끝까지의 곱 | ||
// leftProduct[i-1] = 왼쪽(i-1)부터 처음까지의 곱 | ||
// rightProduct[i+1] = 오른쪽(i+1)부터 끝까지의 곱 | ||
// leftProduct는 처음부터 i까지 순회하면서 구하면 된다. leftProduct[i] = leftProduct[i-1] * (나 자신 = nums[i]) | ||
// rightProduct는 끝부터 시작해서 i까지 순회하면서 구하면 된다. rightProduct[i] = rightProduct[i+1] * (나 자신 = nums[i]) | ||
|
||
|
||
// DP 를 사용하면 시간 복잡도는 O(N) | ||
// 공간 복잡도는 2개의 배열이 필요하고, 답으로 보낼 배열까지 해서 O(3*N) = O(N) | ||
|
||
class Solution { | ||
public int[] productExceptSelf(int[] nums) { | ||
int len = nums.length; | ||
int[] leftProduct = new int[len]; | ||
int[] rightProduct = new int[len]; | ||
|
||
leftProduct[0] = nums[0]; | ||
rightProduct[len - 1] = nums[len - 1]; | ||
for (int i = 1; i < len; ++i) { | ||
leftProduct[i] = leftProduct[i - 1] * nums[i]; | ||
rightProduct[len - i - 1] = rightProduct[len - i] * nums[len - i - 1]; | ||
} | ||
|
||
int[] result = new int[len]; | ||
result[0] = rightProduct[1]; | ||
result[len - 1] = leftProduct[len - 2]; | ||
for (int i = 1; i < len - 1; ++i) { | ||
result[i] = leftProduct[i - 1] * rightProduct[i + 1]; | ||
} | ||
return result; | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// 풀이방법 | ||
// n의 경우 끝에서부터 비트연산을 해야 하므로 가장 오른쪽부터 시작해야 함 | ||
// 이는 n과 1을 AND 연산하고, n을 오른쪽으로 미루면서 수행하면 된다. (n >> 1) | ||
// 최종 결과를 위한 값은 result에 저장할 예정이다. result의 경우, n과 반대로 왼쪽으로 한 칸씩 미루면서 n의 비트를 삽입해줘야 함 (OR 연산) | ||
|
||
// 시간 복잡도 (숫자의 비트 수 만큼 필요) -> O(N) (N: 숫자 n의 비트 수, 문제에서는 32로 고정) | ||
// 공간 복잡도: (result 변수 크기) | ||
public class Solution { | ||
|
||
public int reverseBits(int n) { | ||
int result = 0; | ||
for (int i = 0; i < 32; i++) { | ||
result = result << 1; | ||
result |= (n & 1); | ||
n = n >> 1; | ||
} | ||
return result; | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// 문제 요구사항 -> O(n^2) 보다 작은 시간복잡도로 구현 필요 | ||
// 문제를 보자마자 생각난 것 -> 이분탐색 (2 원소 합이 target 인 것을 구해야 하므로) | ||
// 이분 탐색 시간 복잡도 -> 정렬(O(nlogn)) + 이분탐색 (log(n)) -> nlogn | ||
// 공간 복잡도 -> 배열 크기 만큼 공간 필요 (n) | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
|
||
class ValueIndex implements Comparable<ValueIndex> { | ||
int value; | ||
int index; | ||
|
||
// ValueIndex 객체를 정렬할 때 value 기준으로 오름차순 정렬 | ||
@Override | ||
public int compareTo(ValueIndex other) { | ||
return Integer.compare(this.value, other.value); | ||
} | ||
} | ||
|
||
class Solution { | ||
|
||
public int[] twoSum(int[] nums, int target) { | ||
int size = nums.length; | ||
ValueIndex[] valueIndex = new ValueIndex[size]; | ||
for (int i = 0; i < size; ++i) { | ||
valueIndex[i] = new ValueIndex(); | ||
valueIndex[i].value = nums[i]; | ||
valueIndex[i].index = i; // 정렬 전 original index 저장 | ||
} | ||
Arrays.sort(valueIndex); // 정렬 | ||
int left = 0; | ||
int right = nums.length - 1; | ||
|
||
while (left < right) { | ||
int leftVal = valueIndex[left].value; | ||
int rightVal = valueIndex[right].value; | ||
int sum = leftVal + rightVal; | ||
|
||
if (sum < target) { // target 보다 합이 작으면, left 값이 커져야 함 | ||
left += 1; | ||
} else if (sum > target) { | ||
right -= 1; // target 보다 합이 크면, right 값이 작아져야 함 | ||
} else { // sum = target 이면 끝! | ||
break; | ||
} | ||
} | ||
|
||
return new int[]{valueIndex[left].index, valueIndex[right].index}; | ||
} | ||
} | ||
|
||
|
||
/** | ||
* hashMap을 이용한 O(n) 방법도 있다고 해서 추가 구현해보았습니다. (시간/공간 복잡도 O(n)) | ||
eunhwa99 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
|
||
class Solution { | ||
|
||
public int[] twoSum(int[] nums, int target) { | ||
HashMap<Integer, Integer> numMap = new HashMap<>(); | ||
int left = 0, right = 0; | ||
for (int i = 0; i < nums.length; i++) { | ||
int complement = target - nums[i]; // 현재 숫자와 합쳐서 target을 만드는 숫자 | ||
|
||
// 이미 그 숫자가 해시맵에 있다면, 두 인덱스를 반환 | ||
if (numMap.containsKey(complement)) { | ||
left = numMap.get(complement); | ||
right = i; | ||
break; | ||
} | ||
|
||
// 해시맵에 현재 숫자와 인덱스를 추가 | ||
numMap.put(nums[i], i); | ||
} | ||
|
||
return new int[]{left, right}; | ||
|
||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.