-
-
Notifications
You must be signed in to change notification settings - Fork 195
[윤태권] Week5 문제 풀이 #444
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
[윤태권] Week5 문제 풀이 #444
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,42 @@ | ||
/** | ||
* 시간 복잡도: O(n^2) | ||
* 공간 복잡도: O(n) | ||
*/ | ||
class Solution { | ||
public List<List<Integer>> threeSum(int[] nums) { | ||
int left, right, sum; | ||
List<List<Integer>> results = new ArrayList<>(); | ||
Arrays.sort(nums); | ||
|
||
for (int i = 0; i < nums.length - 2; i++) { | ||
if (i > 0 && nums[i] == nums[i - 1]) { | ||
continue; | ||
} | ||
|
||
left = i + 1; | ||
right = nums.length - 1; | ||
|
||
while (left < right) { | ||
sum = nums[i] + nums[left] + nums[right]; | ||
|
||
if (sum < 0) { | ||
left++; | ||
} else if (sum > 0) { | ||
right--; | ||
} else { | ||
results.add(Arrays.asList(nums[i], nums[left], nums[right])); | ||
|
||
while (left < right && nums[left] == nums[left + 1]) { | ||
left++; | ||
} | ||
while (left < right && nums[right] == nums[right - 1]) { | ||
right--; | ||
} | ||
left++; | ||
right--; | ||
} | ||
} | ||
} | ||
return results; | ||
} | ||
} |
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,22 @@ | ||
/** | ||
* 시간 복잡도: O(n) | ||
* - 문제 조건 1 <= prices.length <= 10^5 를 보고, 우선 O(n^2)는 피해야겠다는 생각을 우선하게 됨. | ||
* - 결국 이 문제는 각 인덱스가 날짜 개념으로 적용되기 때문에, 순차적으로 흘러감. (최대 이익을 계산할 때 결국 각 일자 별로 내가 얼마의 이득을 봤는지를 계산하고, 이 중 최댓값을 고르면 되는 구조) | ||
* - 따라서 한 번의 순회로 문제를 풀 수 있음 | ||
* 공간 복잡도: O(1) | ||
*/ | ||
class Solution { | ||
public int maxProfit(int[] prices) { | ||
int minPrice = prices[0]; | ||
int maxProfit = 0; | ||
|
||
for (int i = 1; i < prices.length; i++) { | ||
if (prices[i] < minPrice) { | ||
minPrice = prices[i]; | ||
} | ||
maxProfit = Math.max(maxProfit, prices[i] - minPrice); | ||
} | ||
|
||
return maxProfit; | ||
} | ||
} |
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,35 @@ | ||
/** | ||
* 시간 복잡도: O (m * n) | ||
* - m: 문자열 개수 | ||
* - n: 각 문자열 길이 | ||
* - 각 문자열에서 빈도수 계산을 위해 O(n)이 소요되는데, 이를 각 문자열마다 진행 | ||
* | ||
* 공간 복잡도: O (m * n) | ||
* - m: 문자열 개수 | ||
* - n: 각 문자열 길이 | ||
* - 주어진 모든 문자열이 모두 서로 anagram을 구성하지 못한 경우, 모든 문자열 수만큼 필요함 | ||
*/ | ||
class Solution { | ||
public List<List<String>> groupAnagrams(String[] strs) { | ||
Map<String, List<String>> map = new HashMap<>(); | ||
|
||
for (String str: strs) { | ||
int[] count = new int[26]; | ||
for (int i = 0; i < str.length(); i++) { | ||
count[str.charAt(i) - 'a']++; | ||
} | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < 26; i++) { | ||
sb.append("^").append(count[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. Note: StringBuilder 에 appending 할 때 "^" 와 같이 구분자를 넣어야 하는 이유
위와 같은 케이스가 발생함. 따라서 구분자를 넣어서 이와 같은 충돌 케이스를 방지함. |
||
} | ||
String candidate = sb.toString(); | ||
if (!map.containsKey(candidate)) { | ||
map.put(candidate, new ArrayList<>()); | ||
} | ||
map.get(candidate).add(str); | ||
} | ||
|
||
return new ArrayList<>(map.values()); | ||
} | ||
} |
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.
접근 방식을 잘 작성해주셔서 이해가 잘 되었습니다 👍