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

[minji-go] Week 3 #788

Merged
merged 8 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
33 changes: 33 additions & 0 deletions combination-sum/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Problem: https://leetcode.com/problems/combination-sum/
Description: return a list of all unique combinations of candidates where the chosen numbers sum to target
Concept: Array, Backtracking
Time Complexity: O(Nⁿ), Runtime 2ms
Space Complexity: O(N), Memory 44.88MB

- Time Complexity, Space Complexity를 어떻게 계산해야할지 어렵네요 :(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 이 문제는 복잡도 계산이 어려운 것 같아요.

시간복잡도에 대해 조금 더 자세하게 써보자면,
재귀 호출의 깊이가 target에 비례하고, 각 깊이에서 N개의 후보 중 하나를 선택하기 때문에 $$O(N^T)$$ 라고 할 수도 있을 것 같습니다. (N은 후보 숫자의 개수, T는 target 값입니다.)

그리고 재귀 호출 스택의 깊이는 최대 target 값인 t에 비례하고, 현재까지 선택된 숫자들의 조합을 저장하는 공간도 최대 t개까지 저장하므로, 공간 복잡도는 $$O(t)$$ 라고 표현할 수 있을 것 같아요.

*/
class Solution {
public List<List<Integer>> answer = new ArrayList<>();

public List<List<Integer>> combinationSum(int[] candidates, int target) {
Arrays.sort(candidates);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

불필요한 탐색을 줄이기 위해 정렬을 먼저하셨군요! 저는 생각못했는데, 성능 향상에 도움되는 것 같네요. 배워갑니다.

findCombination(candidates, target, new ArrayList<>(), 0);
return answer;
}

public void findCombination(int[] candidates, int target, List<Integer> combination, int idx) {
if(target == 0) {
answer.add(new ArrayList<>(combination));
return;
}

for(int i=idx; i<candidates.length; i++) {
if(candidates[i] > target) break;

combination.add(candidates[i]);
findCombination(candidates, target-candidates[i], combination, i);
combination.remove(combination.size()-1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Problem: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
Description: Given two integer arrays preorder and inorder, construct and return the binary tree.
Concept: Array, Hash Table, Divide and Conquer, Tree, Binary Tree
Time Complexity: O(NM), Runtime 2ms
Time Complexity: O(), Runtime 2ms
Space Complexity: O(N), Memory 45.02MB
*/
import java.util.HashMap;
Expand Down
18 changes: 18 additions & 0 deletions maximum-subarray/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Problem: https://leetcode.com/problems/maximum-subarray/
Description: return the largest sum of the subarray, contiguous non-empty sequence of elements within an array.
Concept: Array, Divide and Conquer, Dynamic Programming
Time Complexity: O(N), Runtime 1ms
Space Complexity: O(1), Memory 57.02MB
*/
class Solution {
public int maxSubArray(int[] nums) {
int max = nums[0];
int sum = nums[0];
for(int i=1; i<nums.length; i++){
sum = Math.max(nums[i], nums[i]+sum);
max = Math.max(max, sum);
}
return max;
}
}
24 changes: 24 additions & 0 deletions product-of-array-except-self/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Problem: https://leetcode.com/problems/product-of-array-except-self/
Description: return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
Concept: Array, Prefix Sum
Time Complexity: O(N), Runtime 5ms
Space Complexity: O(N), Memory 54.6MB - O(1) except the output array
*/
class Solution {
public int[] productExceptSelf(int[] nums) {
int[] answer = new int[nums.length];
Arrays.fill(answer, 1);

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

return answer;
}
}
19 changes: 19 additions & 0 deletions reverse-bits/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Problem: https://leetcode.com/problems/reverse-bits/
Description: Reverse bits of a given 32 bits unsigned integer
Topics: Divide and Conquer, Bit Manipulation
Time Complexity: O(1), Runtime 1ms
Space Complexity: O(1), Memory 41.72MB
*/
public class Solution {
public int reverseBits(int n) {
long unsignedNum = n > 0 ? n : n + 2 * (long) Math.pow(2,31); //= Integer.toUnsignedLong()

int reversedNum = 0;
for(int i=31; i>=0; i--){
if(unsignedNum % 2 == 1) reversedNum += (long) Math.pow(2,i); //= (1<<i)
unsignedNum/=2;
}
return reversedNum;
}
}
21 changes: 21 additions & 0 deletions two-sum/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
Problem: https://leetcode.com/problems/two-sum/
Description: return indices of the two numbers such that they add up to target. not use the same element twice.
Topics: Array, Hash Table
Time Complexity: O(N), Runtime 2ms
Space Complexity: O(N), Memory 45.1MB
*/
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> numIndex = new HashMap<>();
for(int secondIndex=0; secondIndex<nums.length; secondIndex++){
if(numIndex.containsKey(target-nums[secondIndex])){
int firstIndex = numIndex.get(target-nums[secondIndex]);
return new int[]{firstIndex, secondIndex};
} else {
numIndex.put(nums[secondIndex], secondIndex);
}
}
return new int[]{};
}
}
Loading