-
Notifications
You must be signed in to change notification settings - Fork 126
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
[minji-go] Week 3 #788
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e05461f
refactor: construct binary tree from preorder and inorder traversal -…
minji-go 8ca1cc0
feat: two sum
minji-go be8f4db
feat: reverse bits
minji-go 2d67021
feat: maximum subarray
minji-go adf2e27
feat: product of array except self
minji-go e9de6c9
Merge branch 'DaleStudy:main' into main
minji-go 6e4c3ab
Merge remote-tracking branch 'origin/main'
minji-go 5bf74c8
feat: combination sum
minji-go 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 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,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를 어떻게 계산해야할지 어렵네요 :( | ||
*/ | ||
class Solution { | ||
public List<List<Integer>> answer = new ArrayList<>(); | ||
|
||
public List<List<Integer>> combinationSum(int[] candidates, int target) { | ||
Arrays.sort(candidates); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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,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; | ||
} | ||
} |
This file contains 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,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; | ||
} | ||
} |
This file contains 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,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; | ||
} | ||
} |
This file contains 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,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[]{}; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저도 이 문제는 복잡도 계산이 어려운 것 같아요.
시간복잡도에 대해 조금 더 자세하게 써보자면,$$O(N^T)$$ 라고 할 수도 있을 것 같습니다. (N은 후보 숫자의 개수, T는 target 값입니다.)
재귀 호출의 깊이가 target에 비례하고, 각 깊이에서 N개의 후보 중 하나를 선택하기 때문에
그리고 재귀 호출 스택의 깊이는 최대 target 값인 t에 비례하고, 현재까지 선택된 숫자들의 조합을 저장하는 공간도 최대 t개까지 저장하므로, 공간 복잡도는$$O(t)$$ 라고 표현할 수 있을 것 같아요.