-
-
Notifications
You must be signed in to change notification settings - Fork 361
[essaysir] WEEK 05 Solutions #2766
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| class Solution { | ||
| public int maxProfit(int[] prices) { | ||
| int result = 0; | ||
| // prices = [7,1,5,3,6,4] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 최소값 대신 최소 인덱스를 들고 가는 접근도 깔끔하네요.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 좋은 의견들 너무 감사합니다! 앞으로, 코딩 테스트 하면서 적용해볼 수 있도록 노력하도록 하겠습니다. |
||
| int lt = 0; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lt는 무슨 약어일까요? 추측하기 어려운 약어로 보여서 의미를 담는 변수명을 사용하시면 좋을 거 같아요. |
||
| for ( int i = 1 ; i < prices.length; i ++){ | ||
| if ( prices[i] < prices[lt] ){ | ||
| lt = i; | ||
| } | ||
| else{ | ||
| result = Math.max(result, prices[i] - prices[lt]); | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
|
Comment on lines
+11
to
+15
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 최대 이익을 나타내는 값이어서 그 의미를 나타내는 변수명을 쓰는건 어떨까요? |
||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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,38 @@ | ||
| public class Solution { | ||
| /* | ||
| * @param strs: a list of strings | ||
| * @return: encodes a list of strings to a single string. | ||
| */ | ||
| public String encode(List<String> strs) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. '#'이 encode/decode 양쪽에 매직 리터럴로 중복되기 때문에, private static final char DELIMITER = '#';로 추출해서 두 메서드의 인코딩 계약이 한 곳에서 관리되면 나중에 구분자 바꿀 때 한쪽만 수정하는 실수를 예방할 수 있을 것 같아요! |
||
| StringBuilder sb = new StringBuilder(); | ||
| for (String s : strs) { | ||
| sb.append(s.length()); | ||
| sb.append('#'); | ||
| sb.append(s); | ||
| } | ||
| return sb.toString(); | ||
| } | ||
|
Comment on lines
+7
to
+14
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 구분자 없이도 가능한데 풀어보셔도 좋을 거 같습니다. |
||
|
|
||
| /* | ||
| * @param str: A string | ||
| * @return: decodes a single string to a list of strings | ||
| */ | ||
| public List<String> decode(String str) { | ||
| List<String> res = new ArrayList<>(); | ||
| int i = 0; | ||
| while (i < str.length()) { | ||
| // '#' 앞의 숫자를 길이로 파싱 | ||
| int len = 0; | ||
| while (str.charAt(i) != '#') { | ||
| len = len * 10 + (str.charAt(i) - '0'); | ||
| i++; | ||
| } | ||
| i++; // '#' 건너뛰기 | ||
|
|
||
| // 정확히 len 글자만 잘라내기 | ||
| res.add(str.substring(i, i + len)); | ||
| i += len; | ||
| } | ||
| return res; | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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,23 @@ | ||
| class Solution { | ||
| public List<List<String>> groupAnagrams(String[] strs) { | ||
| // 아나그램을 그룹별로 만들어라 | ||
| // 아나그램 -> 각 string 을 배치해서 만들 수 있는 가 ? | ||
| // 각 알파벳이 몇개가 있는 가 ? | ||
| List<List<String>> answer = new ArrayList<>(); | ||
| Map<String, List<String>> map = new HashMap<>(); | ||
|
|
||
| for ( int i = 0; i < strs.length; i ++) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 루프 안에서 i 자체를 쓰지 않고 strs[i]로 값만 참조할 때 가독성을 향상시키기 위해서 enhanced for-loop을 써보는 것도 좋을 것 같습니다! |
||
| char[] chars = strs[i].toCharArray(); | ||
| Arrays.sort(chars); | ||
|
|
||
| String s = new String(chars); | ||
| map.computeIfAbsent(s, k -> new ArrayList<>()).add(strs[i]); | ||
| } | ||
|
|
||
| for (List<String> group : map.values()) { | ||
| answer.add(group); | ||
| } | ||
|
|
||
| return answer; | ||
| } | ||
| } | ||
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 한 번의 순회로 최적의 매도 시점을 찾기 위해 최솟값 인덱스를 추적합니다.
개선 제안: 현재 구현이 적절해 보입니다.