Skip to content

[hoyeongkwak] Week2 Solutions #1772

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 2 commits into from
Aug 2, 2025
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
32 changes: 32 additions & 0 deletions 3sum/hoyeongkwak.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Time Complexity : O(n^2)
Space Complexity : O(1)
*/
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);

for (int i = 0; i < nums.length - 2; i++) {
if (i > 0 && nums[i] == nums[i - 1]) continue;
int low = i + 1;
int high = nums.length - 1;
while (low < high) {
int threeSum = nums[i] + nums[low] + nums[high];
if (threeSum < 0) {
low = low + 1;
} else if (threeSum > 0) {
high = high - 1;
} else {
result.add(Arrays.asList(nums[i], nums[low], nums[high]));
while (low < high && nums[low] == nums[low + 1]) low++;
while (low < high && nums[high] == nums[high - 1]) high--;
low = low + 1;
high = high - 1;
}
}
}

return result;
}
}
18 changes: 18 additions & 0 deletions climbing-stairs/hoyeongkwak.java
Copy link
Contributor

Choose a reason for hiding this comment

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

배열 사용없이 공간효율성까지 챙긴 부분이 좋습니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Time Complexity : O(n)
Space Complexity : O(1)
*/
class Solution {
public int climbStairs(int n) {
if (n < 3) return n;
int prev = 1;
int curr = 2;

for(int i = 0; i < n - 2; i++) {
int temp = prev;
prev = curr;
curr = temp + curr;
}
return curr;
}
}
23 changes: 23 additions & 0 deletions product-of-array-except-self/hoyeongkwak.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Time Complexity : O(n)
Space Complexity : O(n)
*/
class Solution {
public int[] productExceptSelf(int[] nums) {
int[] results = new int[nums.length];
Arrays.fill(results, 1);
int before = 1;
int after = 1;

for (int i = 0; i < nums.length - 1; i++) {
before = before * nums[i];
results[i + 1] = results[i + 1] * before;
}

for (int i = nums.length - 1; i > 0; i--) {
after = after * nums[i];
results[i - 1] = results[i - 1] * after;
}
return results;
}
}
28 changes: 28 additions & 0 deletions valid-anagram/hoyeongkwak.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
- 애너그램 또는 어구정철 : 문장의 순서를 바꾸어 다른 단어로 만드는 것
- 같은 단어가 맞는지 검증
- s와 t의 각 알파벳별로 한쪽은 증가, 한쪽은 감소하면서 갯수 세기
- 갯수가 0이 아닌 경우엔 서로 다른 단어이기에 false
*/
/*
Time Complexity : O(n)
Space Complexity : O(1)
*/
class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length())
return false;

int[] count = new int[26];
for (int i = 0; i < s.length(); i++) {
count[s.charAt(i) - 'a']++;
count[t.charAt(i) - 'a']--;
}

for (int c : count) {
if (c != 0)
return false;
}
return true;
}
}
29 changes: 29 additions & 0 deletions validate-binary-search-tree/hoyeongkwak.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
/*
Time Complexity : O(n)
Space Complexity : O(n)
*/
class Solution {
TreeNode prev;
public boolean isValidBST(TreeNode root) {
if (root == null) return true;
if (!isValidBST(root.left)) return false;
if (prev != null && prev.val >= root.val) return false;
prev = root;
return isValidBST(root.right);
}
}