Skip to content

Commit

Permalink
Merge pull request #740 from kdh-92/main
Browse files Browse the repository at this point in the history
[κΆŒλ™ν˜„] Week 2
  • Loading branch information
SamTheKorean authored Dec 22, 2024
2 parents fcb2612 + bc33dc4 commit 420fbb9
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
38 changes: 38 additions & 0 deletions 3sum/kdh-92.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* νŠΉμ΄μ‚¬ν•­
* 문제λ₯Ό μ œλŒ€λ‘œ 이해 λͺ»ν•΄μ„œ 풀이λ₯Ό 보며 μ΄ν•΄ν–ˆλ‹€.
* 핡심은 쀑볡 μ²˜λ¦¬μ™€ 문제λ₯Ό μ°Ύμ•˜μ„ λ•Œ μ’…λ£Œκ°€ μ•„λ‹ˆλΌ λ‹€μŒ ν•­λͺ©μ„ μ°ΎλŠ” 뢀뢄인 것 κ°™λ‹€.
* (μΆ”ν›„ 볡슡 μ˜ˆμ •)
*/

class Solution {
public List<List<Integer>> threeSum(int[] nums) {

// (1) ArrayList
// μ‹œκ°„λ³΅μž‘λ„ : O(N^2), κ³΅κ°„λ³΅μž‘λ„ : O(N)
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);

for (int i = 0; i < nums.length; i++) {
if (i > 0 && nums[i] == nums[i - 1]) continue;

int j = i + 1;
int k = nums.length - 1;

while (j < k) {
int sumNum = nums[i] + nums[j] + nums[k];

if (sumNum > 0) k--;
else if (sumNum < 0) j++;
else {
result.add(Arrays.asList(nums[i], nums[j], nums[k]));
j++;

while (nums[j] == nums[j - 1] && j < k) j++;
}
}
}

return result;
}
}
18 changes: 18 additions & 0 deletions climbing-stairs/kdh-92.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public int climbStairs(int n) {
// dp μ‘μš© 버전
// μ‹œκ°„λ³΅μž‘λ„ : O(N), κ³΅κ°„λ³΅μž‘λ„ : O(1)

int prev = 1, curr = 1;

if (n == 1) return prev;

for (int i = 2; i < n; i++) {
int now = curr + prev;
prev = curr;
curr = now;
}

return curr;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* νŠΉμ΄μ‚¬ν•­
* TreeNode 이해 & right μ„€μ • λΆ€λΆ„ 이해 ν•„μš”
* - μž¬κ·€ 호좜 ν˜•νƒœλ‘œ if 쑰건 μΆ”κ°€ (preStart > preorder.length -1 || inStart > inEnd) 일 κ²½μš°μ—” null λ°˜ν™˜
* - null일 λ•Œ 상황 ν…ŒμŠ€νŠΈν•˜λ©° μ’€ 더 μ‚΄νŽ΄λ³΄κΈ°
*
* - right
* - preStart : preStart + inIdx - inStart + 1 (left λ™μΌν•˜κ²Œ preStart + 1μ—μ„œ inIdx - inStart λ₯Ό μΆ”κ°€ν•΄μ„œ 였λ₯Έμͺ½ μ˜μ—­ 탐색)
*/

class Solution {
public static TreeNode buildTree(int[] preorder, int[] inorder) {
// (1) helper λ©”μ†Œλ“œλ₯Ό μž¬κ·€ν˜ΈμΆœ μ‚¬μš©
// μ‹œκ°„λ³΅μž‘λ„ : O(N^2), κ³΅κ°„λ³΅μž‘λ„ : O(N)

return helper(0, 0, inorder.length - 1, preorder, inorder);
}

public static TreeNode helper(int preStart, int inStart, int inEnd, int[] preorder, int[] inorder) {
if (preStart > preorder.length - 1 || inStart > inEnd) return null;
TreeNode node = new TreeNode(preorder[preStart]);
int inIdx = 0;

for (int i = inStart; i <= inEnd; i++) {
if (node.val == inorder[i]) {
inIdx = i;
break;
}
}

node.left = helper(preStart + 1, inStart, inIdx - 1, preorder, inorder);
node.right = helper(preStart + inIdx - inStart + 1, inIdx + 1, inEnd, preorder, inorder);

return node;
}
}
66 changes: 66 additions & 0 deletions valid-anagram/kdh-92.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* νŠΉμ΄μ‚¬ν•­
* μ‹œκ°„λ³΅μž‘λ„κ°€ (1)번 ν’€μ΄λŠ” O(N log N) / (2), (3)번 ν’€μ΄λŠ” O(N)인데
* λ¦¬νŠΈμ½”λ“œμ—μ„œ Runtime - (1)번 : 3ms, (3)번 : 6ms, (2)번 : 13ms
*
* μ‹œκ°„λ³΅μž‘λ„λ‘œλ§Œ 따지면 λ‹Ήμ—°νžˆ (2), (3)번 풀이가 λΉ¨λΌμ•Όν•˜λŠ”λ°λ„ λΆˆκ΅¬ν•˜κ³  였히렀 (1)번 풀이가 더 λΉ λ₯Έ κ²°κ³Όκ°€ λ‚˜μ˜¨λ‹€.
* chatgpt ν™•μΈν–ˆμ„ λ•Œ κ°€μž₯ 큰 차이점은 λ¬Έμžμ—΄μ„ λ°°μ—΄λ‘œ λ³€κ²½ν•΄μ„œ 정렬을 ν•˜λŠ”λ° 이 λ•Œ μ •λ ¬ μ•Œκ³ λ¦¬μ¦˜μ˜ μ„±λŠ₯에 따라 차이가 λ‚œλ‹€λŠ” λ‚΄μš©μ΄ μžˆμ—ˆλ‹€.
* N이 λ†’μ•„μ§ˆμˆ˜λ‘ (2), (3)의 μ„±λŠ₯이 μ’‹μ•„μ§ˆ κ²ƒμœΌλ‘œ μ˜ˆμƒλ˜λ‚˜ ν•΄λ‹Ή 문제의 κ²°κ³Όμ—μ„œλŠ” (1)의 μ„±λŠ₯이 μ’‹κ²Œ λ‚˜μ˜¬ 수 μžˆλ‹€λŠ” 것을 μ•Œκ²Œ λ˜μ—ˆλ‹€.
*/

class Solution {
public boolean isAnagram(String s, String t) {
// (1) 문자 λ°°μ—΄ - μ •λ ¬ & 비ꡐ
// μ‹œκ°„λ³΅μž‘λ„ : O(N log N), κ³΅κ°„λ³΅μž‘λ„ : O(N)

// char[] sArr = s.toCharArray();
// char[] tArr = t.toCharArray();
// Arrays.sort(sArr);
// Arrays.sort(tArr);

// return new String(sArr).equals(new String(tArr)); // λ¬Έμžμ—΄μ„ λΉ„κ΅ν•œλ‹€ μƒκ°ν–ˆμ„ λ•Œ 방법
// return Arrays.equals(sArr, tArr); // char λ°°μ—΄ 자체λ₯Ό 비ꡐ

// (2) HashMap μ΄μš©ν•΄ μ•ŒνŒŒλ²³ 개수 체크
// μ‹œκ°„λ³΅μž‘λ„ : O(N), κ³΅κ°„λ³΅μž‘λ„ : O(N)

// Map<Character, Integer> count = new HashMap<>();

// for (char x : s.toCharArray()) {
// count.put(x, count.getOrDefault(x, 0) + 1);
// }

// for (char x : t.toCharArray()) {
// count.put(x, count.getOrDefault(x, 0) - 1);
// }

// for (int val : count.values()) {
// if (val != 0) {
// return false;
// }
// }

// return true;

// (3) λ°°μ—΄λ‘œ μ•ŒνŒŒλ²³ 개수 체크
// μ‹œκ°„λ³΅μž‘λ„ : O(N), κ³΅κ°„λ³΅μž‘λ„ : O(N)

// if (s.length() != t.length()) {
// return false;
// }

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

// for (int i = 0; i < freq.length; i++) {
// if (freq[i] != 0) {
// return false;
// }
// }

// return true;
}
}

0 comments on commit 420fbb9

Please sign in to comment.