Skip to content
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

[minji-go] Week 2 #732

Merged
merged 10 commits into from
Dec 21, 2024
18 changes: 18 additions & 0 deletions climbing-stairs/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Problem: https://leetcode.com/problems/climbing-stairs/
Description: how many distinct ways can you climb to the top, if you can either climb 1 or 2 steps
Concept: Dynamic Programming, Memoization, Recursion, Math, Array, Iterator, Combinatorics ...
Time Complexity: O(n), Runtime: 0ms
Space Complexity: O(1), Memory: 40.51MB
*/
class Solution {
public int climbStairs(int n) {
int step1=1, step2=2;
for(int i=3; i<n; i++){
int step3=step1+step2;
step1=step2;
step2=step3;
}
return n==1?step1:step2;
}
Copy link
Member

@dusunax dusunax Dec 18, 2024

Choose a reason for hiding this comment

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

Basecase n이 1인 경우를 삼항연산자로 처리하는 것보다
얼리리턴으로 반환하는 것이 불필요한 연산을 줄이고 가독성이 좋을 것 같아요!

Copy link
Contributor Author

@minji-go minji-go Dec 19, 2024

Choose a reason for hiding this comment

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

코드줄 수를 줄이면 가독성이 높아진다고 생각했었던거 같은데,
n이 1인 경우를 얼리 리턴하면 오히려 의미파악이 쉬워지겠네요!

}
25 changes: 25 additions & 0 deletions valid-anagram/minji-go.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Problem: https://leetcode.com/problems/valid-anagram/
Description: return true if one string is an anagram of the other, one formed by rearranging the letters of the other
Concept:String, Hash Table, Sorting, Array, Counting, String Matching, Ordered Map, Ordered Set, Hash Function ...
Time Complexity: O(n), Runtime: 27ms
Space Complexity: O(n), Memory: 43.11MB
*/
import java.util.HashMap;
import java.util.Map;

class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()) return false;

Map<Character, Integer> count = new HashMap<>();
for(int i=0; i<s.length(); i++){
count.put(s.charAt(i), count.getOrDefault(s.charAt(i), 0)+1);
count.put(t.charAt(i), count.getOrDefault(t.charAt(i), 0)-1);
}
for(Character key : count.keySet()){
if(count.get(key)!=0) return false;
}
Copy link
Member

Choose a reason for hiding this comment

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

keySet()으로 순회하면서 key로 값을 꺼내고 있는데,
values() 메서드로 count 맵에서 바로 가져오는 게 어떨까요?

제가 풀이한 방식과 달라서 흥미롭게 봤습니다😀

Copy link
Contributor Author

Choose a reason for hiding this comment

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

습관적으로 사용하던걸 많이 되짚어볼 수 있었네요. 감사합니다👍
확실히 여기서는 keySet()보다는 values()로 하는게 좋아보여요!

return true;
}
}
Loading