-
-
Notifications
You must be signed in to change notification settings - Fork 195
[minji-go] week 06 solutions #1428
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
Conversation
public void add(String word) { | ||
Trie trie = this; | ||
for(char c : word.toCharArray()){ | ||
trie = trie.next.computeIfAbsent(c, k -> new Trie()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
computeIfAbsent
메서드는 Map자료구조에 기본으로 있는 요소인가 보군요..!
메서드 호출부에 전달되는 함수의 c, k 에서 c 는 for loop에서 참조되는 char 변수가 맞나요?
k 는 뭔지 궁금합니다..!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
안녕하세요 😁
computeIfAbsent
메서드는 첫 번째 매개변수로 Map의 key를 전달받고,
두 번째 매개변수로는 해당 key가 존재하지 않을 경우, 생성할 값을 정의하는 mappingFunction을 전달받습니다.
여기서는 첫 번째 매개변수 c는 말씀하신대로 루트 변수입니다 👍
두 번째 매개변수의 k는 이 c 값을 전달받아 new Trie()를 생성하고, k를 키로 하여 Map에 추가하는 역할을 합니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
설명 감사합니다! 이번주도 수고 많으셨습니다 :)
List<Integer> dp = new ArrayList<>(); | ||
|
||
for(int num : nums){ | ||
int idx = Collections.binarySearch(dp, num); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오! Collections에서 이진탐색도 가능한가보네요! 저는 항상 sort만 썼었는데..!🤣
덕분에 좋은것(?) 많이 배워갑니다. 감사합니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오..? 그런데 코드를 보다보니 제가 일반적으로 봐왔던 dp하고는 뭔가 다른 느낌이 오는것 같아서요..!
혹시 괜찮으시다면 알고리즘의 흐름을 간략하게 설명해주실 수 있을까요?? (뭔가 이해는 안되지만 나이스해 보여서,,, 배우고싶습니다!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dp라는 변수명이 다소 적절하지 않았던 것 같아요. 실제로는 “정렬된 숫자 배열”에 더 가까운 역할이라 sortedNums처럼 이름을 바꾸면 더 명확할 것 같아요.
이 알고리즘은 nums 배열을 순회하면서 각 숫자가 들어갈 정렬된 위치를 이진 탐색으로 찾아내고, 해당 위치에 더 큰 값이 있었다면 덮어써서 더 긴 증가 수열을 만들 수 있도록 하는 방식입니다!
저도 이번에 Collections.binarySearch()를 처음 알게 되었는데요,
binarySearch(dp, num)은 dp 리스트에서 num을 찾아서 존재하면 해당 인덱스를, 존재하지 않으면 삽입해야 할 위치를 -index - 1 형식으로 음수로 반환합니다.
예를 들어, 삽입해야 할 위치가 0이라면 단순히 -0이면 0이 되어버리니,
실제 찾은 경우와 구분하기 위해 -(삽입위치 + 1)을 반환하는 방식이에요 ☀️
이해가 되셨을지 모르겠네요...! 😅 코드 리뷰 감사합니다! 🥰
|
||
class Solution { | ||
public boolean isValid(String s) { | ||
Deque<Character> stack = new ArrayDeque<>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stack 말고 Deque를 사용하셨던 특별한 이유가 있었는지 궁금합니다..!
특정 연산( push , pop) 같은게 좀더 빠른 구석이 있나요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stack은 Vector 클래스를 상속받고 있고, ArrayDeque는 내부적으로 배열을 사용하고 있습니다.
Vector는 모든 메서드가 synchronized로 되어 있어서 멀티스레드 환경이 아닌 경우엔 ArrayDeque가 성능상 좋다고 합니다!
ref. https://docs.oracle.com/javase/8/docs/api/java/util/ArrayDeque.html
Space Complexity: O(n), Memory: 58.6MB | ||
*/ | ||
/** | ||
* <a href="https://leetcode.com/problems/valid-palindrome/">week03-1.valid-palindrome</a> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이전 주차에 풀어보셨던 걸 (코드 정리 겸) 다시 풀어보신걸까요?
+) 요건 알고리즘 내용하고 무관한(?) 내용인데, 뭔가 html 태그로 주석을 남겨놓으셔서,,, 요고는 별도로 블로그 포스팅 하는 용도로 하신 건지 궁금합니다!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
valid-palindrome/minji-go.java
Outdated
class Solution { | ||
public boolean isPalindrome(String s) { | ||
String regex ="[^A-Za-z0-9]"; | ||
String palindrome = s.replaceAll(regex,"").toLowerCase(); //replaceAll(), toLowerCase(): O(n) | ||
String str = s.toLowerCase().replaceAll("[^0-9a-z]", ""); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
현재는 새로운 문자열을 생성하기 때문에 공간 복잡도가 O(n)이지만, 투 포인터 방식으로 접근하면 추가 공간 없이 O(1)로 줄일 수 있을 것 같습니다.
spiral-matrix/minji-go.java
Outdated
int hc = matrix[0].length-1; | ||
|
||
while (lr<=hr && lc<=hc) { | ||
for(int c=lc; c<=hc && lr<=hr; c++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lr <= hr 조건은 이미 while 루프에서 체크했으니까, 내부 반복문에서 다시 확인할 필요 없을 것 같아요. 😄
spiral-matrix/minji-go.java
Outdated
} | ||
lr++; | ||
|
||
for(int r=lr; r<=hr && lc<=hc; r++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
앞서 lr 값은 업데이트됐지만, lc <= hc 조건은 이미 while 루프에서 체크했으니까, 내부 반복문에서 다시 확인할 필요 없을 것 같아요. 😄
spiral-matrix/minji-go.java
Outdated
} | ||
hc--; | ||
|
||
for(int c=hc; c>=lc && lr<=hr; c--){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
위에서 lr 값이 업데이트 되었으므로, lr <= hr 조건을 먼저 체크한 후에 반복문을 진행하고, 그 후에 hr 값을 업데이트해야 할 것 같습니다.
spiral-matrix/minji-go.java
Outdated
} | ||
hr--; | ||
|
||
for(int r=hr; r>=lr && lc<=hc; r--){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
위에서 hc 값이 업데이트 되었으므로, lc<=hc 조건을 먼저 체크한 후에 반복문을 진행하고, 그 후에 lc 값을 업데이트해야 할 것 같습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
안녕하세요!
체크 과정이 루프마다 달라지는게 번잡한 거 같아서 유사한 패턴으로 넣어봤는데, 불필요한 동작일 수 있겠네요!
리뷰 감사합니다 👍
답안 제출 문제
답안 제출 문제 (week03)
작성자 체크 리스트
In Review
로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!