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

[mintheon] Week3 #790

Merged
merged 4 commits into from
Dec 29, 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
38 changes: 38 additions & 0 deletions combination-sum/mintheon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

class Solution {
/**
시간복잡도: O(2^n)
공간복잡도: O(n)
*/
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> answer = new ArrayList<>();
Deque<Integer> nums = new ArrayDeque<>();

backtracking(candidates, answer, nums, target, 0, 0);

return answer;
}

protected void backtracking(int[] candidates, List<List<Integer>> answer, Deque<Integer> nums, int target, int start, int total) {
if(total > target) {
return;
}

if (total == target) {
answer.add(new ArrayList<>(nums));
return;
}

for(int i = start; i < candidates.length; i++) {
int num = candidates[i];
nums.push(num);

backtracking(candidates, answer, nums, target, i, total + num);
nums.pop();
}
}
}
23 changes: 23 additions & 0 deletions product-of-array-except-self/mintheon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
/**
공간복잡도: O(n)
시간복잡도: O(n)
*/
public int[] productExceptSelf(int[] nums) {
int[] answer = new int[nums.length];

answer[0] = 1;
for(int i = 1; i < nums.length; i++) {
answer[i] = answer[i - 1] * nums[i - 1];
}

int value = 1;
for(int i = nums.length - 1; i >= 0; i--) {

answer[i] = answer[i] * value;
value *= nums[i];
}

return answer;
}
}
24 changes: 24 additions & 0 deletions reverse-bits/mintheon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public class Solution {
/**
시간복잡도: O(1) -> 루프는 항상 32번 반복되기 때문
공간복잡도: O(1)
*/

// you need treat n as an unsigned value
public int reverseBits(int n) {
int answer = 0;
int index = 31;

while(n != 0) {
// n&1 : 마지막 비트를 추출
// << : 0을 패딩처리 시켜서 상위 자리수로 올려버림
answer += (n & 1) << index;

// >>> : 부호 상관없이 오른쪽으로 비트 이동
n = n >>> 1;
index--;
}

return answer;
}
}
26 changes: 26 additions & 0 deletions two-sum/mintheon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.HashMap;
import java.util.Map;

class Solution {
/**
공간복잡도: O(n)
시간복잡도: O(n)
*/

public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> numMap = new HashMap<>();

for(int i = 0; i < nums.length; i++) {
numMap.put(nums[i], i);
}

for(int i = 0; i < nums.length; i++) {
int pairNum = target - nums[i];
if(numMap.containsKey(pairNum) && numMap.get(pairNum) != i) {
return new int[]{i, numMap.get(target - nums[i])};
}
}

return new int[2];
}
}
Loading