Skip to content
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

[eunhwa99] Week 03 #761

Merged
merged 9 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions combination-sum/eunhwa99.java
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); // 백트래킹: 마지막 숫자 제거
eunhwa99 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
15 changes: 15 additions & 0 deletions maximum-subarray/eunhwa99.java
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;
}
}
35 changes: 35 additions & 0 deletions product-of-array-except-self/eunhwa99.java
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;
}
}

20 changes: 20 additions & 0 deletions reverse-bits/eunhwa99.java
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;
}
}

80 changes: 80 additions & 0 deletions two-sum/eunhwa99.java
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};

}
}

Loading