-
-
Notifications
You must be signed in to change notification settings - Fork 195
[seunghyun] 1주차 답안 제출 #15
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
fd5293f
a21f6ea
259d1fb
28adfd9
3d16712
47756cf
6220c41
837ccf4
d861a43
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,31 @@ | ||
class Solution { | ||
/** | ||
* 전체 prices 배열을 순회하므로 시간복잡도 : O(n) | ||
* profit은 1개의 값을 저장하므로 공간복잡도 : O(1) | ||
* | ||
* 문제를 푸는 접근이 어려워서 1시간이 넘게 소요되었다... | ||
* @param prices | ||
* @return | ||
*/ | ||
public int maxProfit(int[] prices) { | ||
// 처음 가격을 담아주고 | ||
int buyPrice = prices[0]; | ||
// 다음에 더 저렴한 가격이 있다면 해당 가격으로 담아준다. | ||
int profit = 0; | ||
for (int i = 0; i < prices.length; i++) { | ||
// 현재가격 | ||
int currentPrice = prices[i]; | ||
|
||
if (currentPrice < buyPrice) { | ||
buyPrice = currentPrice; | ||
} | ||
|
||
// 현재가격과 - 내가산가격 = 이익(profit) | ||
if (currentPrice - buyPrice > profit) { | ||
profit = currentPrice - buyPrice; | ||
} | ||
} | ||
|
||
return profit; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Solution { | ||
public boolean containsDuplicate(int[] nums) { | ||
Set<Integer> set = new HashSet<>(); | ||
|
||
for (int num : nums) { | ||
if (set.contains(num)) return true; | ||
set.add(num); | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class Solution { | ||
/** | ||
* 임의의 두 수를 더해서 target과 동일한 경우 | ||
* 해당하는 두 수의 index를 돌려준다. | ||
* | ||
* 두개의 수를 더해서 target과 일치해야 하므로 | ||
* 정답이 항상 2개가 있다고 가정을 하였습니다. | ||
* | ||
* 2중 loop를 순회해야 하므로 시간복잡도는 O(n^2) | ||
* 2개의 값을 저장해야 하므로 공간복잡도는 O(n) 으로 생각됩니다. (틀리다면 리뷰 부탁드립니다 ! ) | ||
* ( => 1개의 값을 저장할때는 공간복잡도가 O(1)이 될까요...? 챗 지피티로 먼저 확인해보겠습니다. ) | ||
* | ||
* 지피티는 "고정된 저장공간을 사용하기때문에" 공간복잡도는 O(1)이라고 말해주네요! | ||
* 그렇다면 공간을 가변으로 가지는 ArrayList와 LinkedList의 저장공간은 O(n)이 맞는지도 궁금해지네요 | ||
* | ||
* @param nums | ||
* @param target | ||
* @return | ||
*/ | ||
public static int[] twoSum(int[] nums, int target) { | ||
int[] result = new int[2]; | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
for (int j = i+1; j < nums.length; j++) { | ||
if (nums[i] + nums[j] == target) { | ||
result[0] = i; | ||
result[1] = j; | ||
|
||
return result; | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
class Solution { | ||
|
||
// anagram | ||
// 문자열의 순서를 바꾸면 다른 단어가 되는 것 | ||
// ex) eat, ate, tea | ||
|
||
/** | ||
* 문자열이 비어있는 경우 | ||
* 문자열의 길이가 서로 다른 경우 | ||
* | ||
* 같은 단어가 존해랄 수 있으므로 Set은 안될것 같음... | ||
* 두 문자열을 비교하기 위해 for loop로 순회하기에는 O(n^2) 으로 시간초과가 날것 같음... | ||
* | ||
* Map으로 key에 문자열 s에 대한 1글자당 카운트를 저장하고 | ||
* 해당 Map을 문자열 t를 순회하면서 1글자당 카운트를 -1 하고 카운트가 0이되면 해당 key를 지운다. | ||
* | ||
* Map에 존재하지 않는 경우는 anagram이 성립할 수 없으므로 false | ||
* | ||
* Map을 이용하면 데이터를 조회 하는 경우는 O(1)이지만 결국 문자열 전체를 순회해야 하므로 O(n) | ||
* @param s | ||
* @param t | ||
* @return | ||
*/ | ||
public static boolean containsDuplicate(String s, String t) { | ||
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. (순수 질문) static 추가되어도 leetcode에서 통과 되나요? 문제에서 기본 제공되는 템플릿에 static이 없었던걸로 기억해서 질문드려봅니다 : ) 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. 앗 저도 leetcode는 static 키워드 제거하고 제출하였습니다. |
||
if (s.length() != t.length()) return false; | ||
Map<Character, Integer> map = new HashMap<>(); | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
char word = s.charAt(i); | ||
map.put(word, (map.getOrDefault(word, 0) + 1)); | ||
} | ||
|
||
for (int i = 0; i < t.length(); i++) { | ||
char word = t.charAt(i); | ||
if (!map.containsKey(word)) { | ||
return false; | ||
} | ||
|
||
if (map.get(word) != 0) { | ||
int v = map.get(word) - 1; | ||
if (v == 0) { | ||
map.remove(word); | ||
} else { | ||
map.put(word, v); | ||
} | ||
} | ||
} | ||
|
||
return map.isEmpty(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution { | ||
/** | ||
* 팰린드롬 | ||
* 1. 소문자로 변환 | ||
* 2. 영어를 제외한 문자는 제거 | ||
* 3. 앞으로 읽어도 뒤로 읽어도 (우영우?) 똑같으면 팰린드롬 이다. | ||
* | ||
* 시간복잡도는 O(n)으로 생각된다. | ||
* 공간복잡도는 O(n)으로 생각된다. | ||
* @param s | ||
* @return | ||
*/ | ||
public boolean isPalindrome(String s) { | ||
s = s.toLowerCase(); | ||
s = s.replaceAll("[^a-z0-9]", ""); | ||
StringBuilder sb = new StringBuilder(s); | ||
String reversString = sb.reverse().toString(); | ||
|
||
return s.equals(reversString); | ||
} | ||
} |
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.
깔끔한 자바 답안 코드네요. 알고리즘의 성능을 분석해셔서 간단히 주석으로 달아주셔도 좋은 연습이 되실 것 같습니다.
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.
@DaleSeo 알고리즘의 어떠한 성능에 대한 분석을 어떻게 진행 해야하는지 몰라서요 😓
혹시 가이드 문서나 링크를 알려주신다면 참고해서 작성해 보도록 하겠습니다!
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.
https://www.algodale.com/guides/big-o-complexities/
시간복잡도와 공간복잡도에 대해서 찾아보시면 좋을 것 같습니다..!
@koreas9408
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.
처음에 스스로 하기 어려우시다면 chatGPT 한테 코드를 주면서 시간복잡도와 공간복잡도를 알려달라고 해보시는걸 추천드립니다 : )
물론 다 믿진 마지고 왜 그렇게 나오는지, 진짜 맞는 답변인지도 꼭 확인해보세요...!
(가끔 틀리게 알려줍니다)
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.
@dev-jonghoonpark 감사합니다!!