Skip to content

[윤태권] 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 3 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions 3sum/taekwon-dev.java
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;
}
}
22 changes: 22 additions & 0 deletions best-time-to-buy-and-sell-stock/taekwon-dev.java
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)
*/
Comment on lines +1 to +7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

접근 방식을 잘 작성해주셔서 이해가 잘 되었습니다 👍

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;
}
}
35 changes: 35 additions & 0 deletions group-anagrams/taekwon-dev.java
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]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note:

StringBuilder 에 appending 할 때 "^" 와 같이 구분자를 넣어야 하는 이유

  • [2, 0, 1] -> "201"
  • [20, 1] -> "201"

위와 같은 케이스가 발생함. 따라서 구분자를 넣어서 이와 같은 충돌 케이스를 방지함.

}
String candidate = sb.toString();
if (!map.containsKey(candidate)) {
map.put(candidate, new ArrayList<>());
}
map.get(candidate).add(str);
}

return new ArrayList<>(map.values());
}
}