-
-
Notifications
You must be signed in to change notification settings - Fork 245
[forest000014] Week 03 #785
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
494d56e
Two Sum
forest000014 b6138ed
Two Sum - 마지막 개행 문자
forest000014 8681b04
Reverse Bits
forest000014 65e2454
Product of Array Except Self
forest000014 9c068d6
Product of Array Except Self - space complexity O(1)
forest000014 9c1fff6
Maximum Subarray
forest000014 d818f00
Combination Sum
forest000014 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 hidden or 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,64 @@ | ||
/* | ||
runtime 23 ms, beats 50.28% | ||
memory 45.12 MB, beats 20.14% | ||
|
||
time complexity: O(nlogn) | ||
- numsArray 정렬: O(nlogn) | ||
- binary search: O(nlogn) | ||
- i iteration: O(n) | ||
- i번째 binary search: O(logn) | ||
|
||
space complexity: O(n) | ||
- numsArray: O(n) | ||
*/ | ||
|
||
class Solution { | ||
public int[] twoSum(int[] nums, int target) { | ||
ArrayList<Tuple> numsArray = IntStream.range(0, nums.length) | ||
.mapToObj(i -> new Tuple(i, nums[i])) | ||
.collect(Collectors.toCollection(ArrayList::new)); | ||
|
||
numsArray.sort(Comparator.comparing(tuple -> tuple.val)); | ||
|
||
int n = numsArray.size(); | ||
|
||
for (int i = 0; i < n; i++) { | ||
int x = target - numsArray.get(i).val; | ||
int j = -1; | ||
int l = i + 1; | ||
int r = n - 1; | ||
boolean found = false; | ||
while (l <= r) { | ||
int m = (r - l) / 2 + l; | ||
if (numsArray.get(m).val == x) { | ||
j = m; | ||
found = true; | ||
break; | ||
} else if (numsArray.get(m).val < x) { | ||
l = m + 1; | ||
} else { | ||
r = m - 1; | ||
} | ||
} | ||
|
||
if (found) { | ||
int[] ans = new int[2]; | ||
ans[0] = numsArray.get(i).ref; | ||
ans[1] = numsArray.get(j).ref; | ||
return ans; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public class Tuple { | ||
public final Integer ref; | ||
public final Integer val; | ||
|
||
public Tuple(Integer ref, Integer val) { | ||
this.ref = ref; | ||
this.val = val; | ||
} | ||
} | ||
} |
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.
안녕하세요, @forest000014 님! 저도 java를 사용하여 이번 스터디에 참여하고 있습니다. 잘 부탁드립니다.
튜플 클래스를 따로 사용하여 주신 점이 저는 개인적으로 가독성이 좋아 코드를 이해하는 것이 더 수월했습니다.
3주차 문제 풀이 수고 많으셨습니다!