Skip to content

Commit 29a2b40

Browse files
authored
Merge pull request #788 from minji-go/main
[minji-go] Week 3
2 parents 5d19a8c + 5bf74c8 commit 29a2b40

File tree

6 files changed

+116
-1
lines changed

6 files changed

+116
-1
lines changed

combination-sum/minji-go.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Problem: https://leetcode.com/problems/combination-sum/
3+
Description: return a list of all unique combinations of candidates where the chosen numbers sum to target
4+
Concept: Array, Backtracking
5+
Time Complexity: O(Nⁿ), Runtime 2ms
6+
Space Complexity: O(N), Memory 44.88MB
7+
8+
- Time Complexity, Space Complexity를 어떻게 계산해야할지 어렵네요 :(
9+
*/
10+
class Solution {
11+
public List<List<Integer>> answer = new ArrayList<>();
12+
13+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
14+
Arrays.sort(candidates);
15+
findCombination(candidates, target, new ArrayList<>(), 0);
16+
return answer;
17+
}
18+
19+
public void findCombination(int[] candidates, int target, List<Integer> combination, int idx) {
20+
if(target == 0) {
21+
answer.add(new ArrayList<>(combination));
22+
return;
23+
}
24+
25+
for(int i=idx; i<candidates.length; i++) {
26+
if(candidates[i] > target) break;
27+
28+
combination.add(candidates[i]);
29+
findCombination(candidates, target-candidates[i], combination, i);
30+
combination.remove(combination.size()-1);
31+
}
32+
}
33+
}

construct-binary-tree-from-preorder-and-inorder-traversal/minji-go.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Problem: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
33
Description: Given two integer arrays preorder and inorder, construct and return the binary tree.
44
Concept: Array, Hash Table, Divide and Conquer, Tree, Binary Tree
5-
Time Complexity: O(NM), Runtime 2ms
5+
Time Complexity: O(), Runtime 2ms
66
Space Complexity: O(N), Memory 45.02MB
77
*/
88
import java.util.HashMap;

maximum-subarray/minji-go.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Problem: https://leetcode.com/problems/maximum-subarray/
3+
Description: return the largest sum of the subarray, contiguous non-empty sequence of elements within an array.
4+
Concept: Array, Divide and Conquer, Dynamic Programming
5+
Time Complexity: O(N), Runtime 1ms
6+
Space Complexity: O(1), Memory 57.02MB
7+
*/
8+
class Solution {
9+
public int maxSubArray(int[] nums) {
10+
int max = nums[0];
11+
int sum = nums[0];
12+
for(int i=1; i<nums.length; i++){
13+
sum = Math.max(nums[i], nums[i]+sum);
14+
max = Math.max(max, sum);
15+
}
16+
return max;
17+
}
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Problem: https://leetcode.com/problems/product-of-array-except-self/
3+
Description: return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
4+
Concept: Array, Prefix Sum
5+
Time Complexity: O(N), Runtime 5ms
6+
Space Complexity: O(N), Memory 54.6MB - O(1) except the output array
7+
*/
8+
class Solution {
9+
public int[] productExceptSelf(int[] nums) {
10+
int[] answer = new int[nums.length];
11+
Arrays.fill(answer, 1);
12+
13+
int prefixProduct = 1;
14+
int suffixProduct = 1;
15+
for(int i=1; i<nums.length; i++){
16+
prefixProduct = prefixProduct * nums[i-1];
17+
suffixProduct = suffixProduct * nums[nums.length-i];
18+
answer[i] *= prefixProduct;
19+
answer[nums.length-i-1] *= suffixProduct;
20+
}
21+
22+
return answer;
23+
}
24+
}

reverse-bits/minji-go.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Problem: https://leetcode.com/problems/reverse-bits/
3+
Description: Reverse bits of a given 32 bits unsigned integer
4+
Topics: Divide and Conquer, Bit Manipulation
5+
Time Complexity: O(1), Runtime 1ms
6+
Space Complexity: O(1), Memory 41.72MB
7+
*/
8+
public class Solution {
9+
public int reverseBits(int n) {
10+
long unsignedNum = n > 0 ? n : n + 2 * (long) Math.pow(2,31); //= Integer.toUnsignedLong()
11+
12+
int reversedNum = 0;
13+
for(int i=31; i>=0; i--){
14+
if(unsignedNum % 2 == 1) reversedNum += (long) Math.pow(2,i); //= (1<<i)
15+
unsignedNum/=2;
16+
}
17+
return reversedNum;
18+
}
19+
}

two-sum/minji-go.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Problem: https://leetcode.com/problems/two-sum/
3+
Description: return indices of the two numbers such that they add up to target. not use the same element twice.
4+
Topics: Array, Hash Table
5+
Time Complexity: O(N), Runtime 2ms
6+
Space Complexity: O(N), Memory 45.1MB
7+
*/
8+
class Solution {
9+
public int[] twoSum(int[] nums, int target) {
10+
Map<Integer, Integer> numIndex = new HashMap<>();
11+
for(int secondIndex=0; secondIndex<nums.length; secondIndex++){
12+
if(numIndex.containsKey(target-nums[secondIndex])){
13+
int firstIndex = numIndex.get(target-nums[secondIndex]);
14+
return new int[]{firstIndex, secondIndex};
15+
} else {
16+
numIndex.put(nums[secondIndex], secondIndex);
17+
}
18+
}
19+
return new int[]{};
20+
}
21+
}

0 commit comments

Comments
 (0)