Skip to content

[Paragon0107] Week2 #749

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 8 commits into from
Dec 21, 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
28 changes: 28 additions & 0 deletions 3sum/paragon0107.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Set<List<Integer>> set = new HashSet<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length-2; i++) {
int start = i + 1;
int end = nums.length - 1;
while (start < end) {
int sum = nums[i] + nums[start] + nums[end];
if (sum < 0 ) {
start++;
} else if (sum > 0) {
end--;
}else {
set.add(Arrays.asList(nums[i], nums[start], nums[end]));
start++;
end--;
}
}
}
return set.stream().toList();
}
}
19 changes: 19 additions & 0 deletions climbing-stairs/paragon0107_230.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
*
* 시간 복잡도:
* 바텀업 형식으로 배열을 훑으며 올라가기 때문에 O(N)
* 공간 복잡도:
* 자연수 마다 해당하는 방법의 갯수를 저장하기 때문에 O(N)
*
* */
class Solution {
public int climbStairs(int n) {
int[] dp = new int[n + 1];
dp[1] = 1;
dp[2] = 2;
for(int i=3;i<=n;i++){
dp[i] = dp[i - 2] + dp[i - 1];
}
return dp[n];
}
}
18 changes: 18 additions & 0 deletions valid-anagram/paragon0107_218.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import java.util.Arrays;
/*
* 시간 복잡도:
* toCharArray는 O(1)의 복잡도를 갖고 ArraySor의 경우 평균O(nlogn), 최악O(n^2)를 갖음(코테시 몇으로 계산하고 진행해야 할 지는 잘 모르겠네요..)
* 공간 복잡도:
* s와t를 사용해서 그대로 배열로 만들기 때문에 O(n)
*
*
* */
class Solution {
public static boolean isAnagram(String s, String t) {
char[] s1 = s.toCharArray();
char[] s2 = t.toCharArray();
Arrays.sort(s1);
Arrays.sort(s2);
Comment on lines +12 to +15
Copy link
Contributor

Choose a reason for hiding this comment

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

저도 잘 몰라서 찾아봤는데, 자바 8이상에서는 parallelSort를 사용하면 시간 복잡도를 O(n logn)으로 개선할 수 있다고 하네요!

return Arrays.equals(s1, s2);
}
}
Loading