diff --git a/best-time-to-buy-and-sell-stock/Seunghyun-Lim.java b/best-time-to-buy-and-sell-stock/Seunghyun-Lim.java new file mode 100644 index 000000000..3750c95fa --- /dev/null +++ b/best-time-to-buy-and-sell-stock/Seunghyun-Lim.java @@ -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; + } +} diff --git a/contains-duplicate/Seunghyun-Lim.java b/contains-duplicate/Seunghyun-Lim.java new file mode 100644 index 000000000..915a3c69d --- /dev/null +++ b/contains-duplicate/Seunghyun-Lim.java @@ -0,0 +1,12 @@ +class Solution { + public boolean containsDuplicate(int[] nums) { + Set set = new HashSet<>(); + + for (int num : nums) { + if (set.contains(num)) return true; + set.add(num); + } + + return false; + } +} diff --git a/two-sum/Seunghyun-Lim.java b/two-sum/Seunghyun-Lim.java new file mode 100644 index 000000000..fcb127d7e --- /dev/null +++ b/two-sum/Seunghyun-Lim.java @@ -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; + } +} diff --git a/valid-anagram/Seunghyun-Lim.java b/valid-anagram/Seunghyun-Lim.java new file mode 100644 index 000000000..7e5a85b27 --- /dev/null +++ b/valid-anagram/Seunghyun-Lim.java @@ -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) { + if (s.length() != t.length()) return false; + Map 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(); + } +} diff --git a/valid-palindrome/Seunghyun-Lim.java b/valid-palindrome/Seunghyun-Lim.java new file mode 100644 index 000000000..9f56d2ee4 --- /dev/null +++ b/valid-palindrome/Seunghyun-Lim.java @@ -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); + } +}