-
-
Notifications
You must be signed in to change notification settings - Fork 245
[Tessa1217] WEEK 01 solutions #1129
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 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cd99fcb
add: contains duplicate solution
Tessa1217 8377a45
update: add comments
Tessa1217 79067b6
add: two sum solution
Tessa1217 89fbff9
add: house robber solution
Tessa1217 32db53b
update: add line break
Tessa1217 b9037b5
add: longest consecutive sequence solution
Tessa1217 f58ae30
add: top k frequent elements solution
Tessa1217 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,21 @@ | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
class Solution { | ||
|
||
/** 217. 중복된 수 | ||
* 정수 배열 nums가 주어졌을 때 배열 요소 중 한 개 이상이 두 번 이상 중복되어 | ||
* 나타는 경우 true를, 모든 배열의 요소가 고유한 경우 false를 반환 | ||
*/ | ||
public boolean containsDuplicate(int[] nums) { | ||
|
||
Set<Integer> distincts = new HashSet<>(); | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
distincts.add(nums[i]); | ||
} | ||
|
||
return distincts.size() != nums.length; | ||
} | ||
} | ||
|
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,25 @@ | ||
class Solution { | ||
|
||
// 전문적인 강도인 당신은 거리에 있는 집들을 털 계획을 세우고 있다. | ||
// 조건: 인접한 집들은 연결된 보안 경비 시스팀이 있어 인접한 집을 같은 날에 털 경우 자동적으로 경찰에 연락이 간다. | ||
// 각 집에 쌓아둔 돈의 양 배열 (정수 배열)이 주어질 때 경찰한테 걸리지 않고 털 수 있는 최대 돈의 양을 반환하시오. | ||
public int rob(int[] nums) { | ||
|
||
// 조건: 1 == nums.length (털 집이 한 곳 뿐) | ||
if (nums.length == 1) { | ||
return nums[0]; | ||
} | ||
|
||
// DP로 계산 | ||
nums[1] = Math.max(nums[0], nums[1]); | ||
|
||
for (int i = 2; i < nums.length; i++) { | ||
nums[i] = Math.max(nums[i - 1], nums[i - 2] + nums[i]); | ||
} | ||
|
||
return nums[nums.length - 1]; | ||
|
||
} | ||
|
||
} | ||
|
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,50 @@ | ||
import java.util.Map; | ||
import java.util.HashMap; | ||
|
||
class Solution { | ||
// 정수 배열 nums와 정수 target가 주어질 때 두 정수의 합이 target이 되는 배열 요소의 인덱스를 반환 | ||
// 입력받은 배열에는 하나의 해답만 존재한다고 가정할 수 있으며 같은 요소를 한 번 이상 사용할 수는 없다. | ||
// 반환하는 인덱스의 정렬은 신경쓰지 않아도 된다. | ||
|
||
public int[] twoSum(int[] nums, int target) { | ||
|
||
// 힌트를 참고하여 시간 복잡도 O(n^2) 이하로 줄이기 | ||
// 힌트: Like maybe a hash map to speed up the search? | ||
|
||
int[] answer = new int[2]; | ||
|
||
Map<Integer, Integer> numMap = new HashMap<>(); | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
if (numMap.containsKey(target - nums[i])) { | ||
answer[0] = numMap.get(target - nums[i]); | ||
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. target - nums[i]에 대한 변수를 생성해서 연산한 값을 담아주면 가독성과 연산 최소화가 가능해보입니다! |
||
answer[1] = i; | ||
break; | ||
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. 배열은 조건 만족 시점에 생성하고 break없이 바로 리턴해도 될것같습니다. |
||
} | ||
numMap.put(nums[i], i); | ||
} | ||
|
||
return answer; | ||
|
||
} | ||
|
||
// public int[] twoSum(int[] nums, int target) { | ||
|
||
// // 전체 탐색 진행 | ||
// int[] answer = new int[2]; | ||
|
||
// for (int i = 0; i < nums.length - 1; i++) { | ||
// for (int j = i + 1; j < nums.length; j++) { | ||
// if (nums[i] + nums[j] == target) { | ||
// answer[0] = i; | ||
// answer[1] = j; | ||
// } | ||
// } | ||
// } | ||
|
||
// return answer; | ||
// } | ||
|
||
} | ||
|
||
|
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.
중복을 찾는 것이 목적이므로, 전체 배열을 다 순회하지 않고 중복 발견 즉시 리턴하는 방식은 어떨까요?
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.
`
`
리뷰해주신대로 개선했더니 확실히 런타임이 줄었네요. 좋은 리뷰 감사합니다!