diff --git a/Changwhi/duplicate-integer.py b/Changwhi/duplicate-integer.py deleted file mode 100644 index 718d2316..00000000 --- a/Changwhi/duplicate-integer.py +++ /dev/null @@ -1,9 +0,0 @@ -class Solution: - def hasDuplicate(self, nums: List[int]) -> bool: - numbers = [] - for num in nums: - if num in numbers: - return True - else: - numbers.append(num) - return False \ No newline at end of file diff --git a/Changwhi/is-anagram.py b/Changwhi/is-anagram.py deleted file mode 100644 index 9126a8f1..00000000 --- a/Changwhi/is-anagram.py +++ /dev/null @@ -1,16 +0,0 @@ -class Solution: - def isAnagram(self, s: str, t: str) -> bool: - if len(s) != len(t): - return False - - sChars = {} - tChars = {} - - for i in range(len(s)): - currentSValue = sChars.get(s[i], 0) - sChars[s[i]] = currentSValue + 1 - - currentTvalue = tChars.get(t[i], 0) - tChars[t[i]] = currentTvalue + 1 - - return sChars == tChars \ No newline at end of file diff --git a/Changwhi/two-integer-sum.py b/Changwhi/two-integer-sum.py deleted file mode 100644 index 1c20860f..00000000 --- a/Changwhi/two-integer-sum.py +++ /dev/null @@ -1,15 +0,0 @@ -class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - l = 0 - r = 1 - while l < len(nums) - 1: - if nums[l] + nums[r] == target: - return [l, r] - elif r >= len(nums) - 1: - l += 1 - r = l + 1 - else: - r += 1 - - return [] - \ No newline at end of file diff --git a/Soohyeun/2024-09-23-IsAnagram.py b/Soohyeun/2024-09-23-IsAnagram.py deleted file mode 100644 index 1fc3274c..00000000 --- a/Soohyeun/2024-09-23-IsAnagram.py +++ /dev/null @@ -1,18 +0,0 @@ -from collections import defaultdict - - -class Solution: - def isAnagram(self, s: str, t: str) -> bool: - if len(s) != len(t): - return False - - seen = defaultdict(int) - for i in range(len(s)): - seen[s[i]] += 1 - seen[t[i]] -= 1 - if seen[s[i]] == 0: - del seen[s[i]] - if seen[t[i]] == 0: - del seen[t[i]] - - return True if not seen else False diff --git a/Soohyeun/2024-09-23-TwoIntegerSum.py b/Soohyeun/2024-09-23-TwoIntegerSum.py deleted file mode 100644 index add764c0..00000000 --- a/Soohyeun/2024-09-23-TwoIntegerSum.py +++ /dev/null @@ -1,9 +0,0 @@ -class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - seen = dict() - for index, num in enumerate(nums): - rest = target - num - if rest in seen.keys(): - return [seen[rest], index] - seen[num] = index - return -1 \ No newline at end of file diff --git a/Soohyeun/2024-09-26-DuplicateInteger.py b/Soohyeun/2024-09-26-DuplicateInteger.py deleted file mode 100644 index e9dd3888..00000000 --- a/Soohyeun/2024-09-26-DuplicateInteger.py +++ /dev/null @@ -1,5 +0,0 @@ -class Solution: - def hasDuplicate(self, nums: List[int]) -> bool: - set_num = set(nums) - - return False if len(set_num) == len(nums) else True diff --git a/YounSangHo/Week1/2024-09-29-Duplicate-Integer.js b/YounSangHo/Week1/2024-09-29-Duplicate-Integer.js deleted file mode 100644 index cbc0c621..00000000 --- a/YounSangHo/Week1/2024-09-29-Duplicate-Integer.js +++ /dev/null @@ -1,35 +0,0 @@ -// Given an integer array nums, return true if any value appears more than once in the array, otherwise return false. -// -// Example 1: -// -// Input: nums = [1, 2, 3, 3] -// -// Output: true -// Example 2: -// -// Input: nums = [1, 2, 3, 4] -// -// Output: false - -class Solution { - /** - * @param {number[]} nums - * @return {boolean} - */ - hasDuplicate(nums) { - if (nums.length === 0) return false; - - const newMap = new Map(); - let result = false; - - nums.forEach((num) => { - if (newMap.has(num)) { - result = true; - } else { - newMap.set(num, 1); - } - }); - - return result; - } -} diff --git a/YounSangHo/Week1/2024-09-29-Is-Anagram.js b/YounSangHo/Week1/2024-09-29-Is-Anagram.js deleted file mode 100644 index 99aef2f4..00000000 --- a/YounSangHo/Week1/2024-09-29-Is-Anagram.js +++ /dev/null @@ -1,37 +0,0 @@ -class Solution { - /** - * @param {string} s - * @param {string} t - * @return {boolean} - */ - isAnagram(s, t) { - if (s.length !== t.length) return false; - - let sArr = s.split(""); - let tArr = t.split(""); - const checkMap = new Map(); - let result = true; - - sArr.forEach((chr) => { - if (checkMap.has(chr)) { - checkMap.set(chr, checkMap.get(chr) + 1); - } else { - checkMap.set(chr, 1); - } - }); - - tArr.forEach((chr) => { - if (checkMap.has(chr)) { - if (checkMap.get(chr) === 1) { - checkMap.delete(chr); - } else { - checkMap.set(chr, checkMap.get(chr) - 1); - } - } else { - result = false; - } - }); - - return result && checkMap.size === 0; - } -} diff --git a/YounSangHo/Week1/2024-09-29-two-integer-sum.js b/YounSangHo/Week1/2024-09-29-two-integer-sum.js deleted file mode 100644 index 6a0ca8df..00000000 --- a/YounSangHo/Week1/2024-09-29-two-integer-sum.js +++ /dev/null @@ -1,22 +0,0 @@ -class Solution { - /** - * @param {number[]} nums - * @param {number} target - * @return {number[]} - */ - twoSum(nums, target) { - let checkMap = new Map(); - let resultArr = []; - - nums.forEach((num, index) => { - let otherVal = target - num; - if (!checkMap.has(otherVal)) { - checkMap.set(num, index); - } else { - resultArr = [index, checkMap.get(otherVal)]; - } - }); - - return resultArr; - } -} diff --git a/Yunju/2024-09-28-Duplicate-Integer.py b/Yunju/2024-09-28-Duplicate-Integer.py deleted file mode 100644 index 4bccf047..00000000 --- a/Yunju/2024-09-28-Duplicate-Integer.py +++ /dev/null @@ -1,8 +0,0 @@ -# Duplicate Integer -class Solution: - def hasDuplicate(self, nums: List[int]) -> bool: - nums.sort() - for i in range(1, len(nums)): - if nums[i] == nums[i - 1]: - return True - return False \ No newline at end of file diff --git a/Yunju/2024-09-28-Is-Anagram.py b/Yunju/2024-09-28-Is-Anagram.py deleted file mode 100644 index 4cc17d3a..00000000 --- a/Yunju/2024-09-28-Is-Anagram.py +++ /dev/null @@ -1,7 +0,0 @@ -# Is Anagram -class Solution: - def isAnagram(self, s: str, t: str) -> bool: - if sorted(s) == sorted(t): - return True - return False - \ No newline at end of file diff --git a/Yunju/2024-09-28-Two-Integer-Sum.py b/Yunju/2024-09-28-Two-Integer-Sum.py deleted file mode 100644 index f5f7a7c7..00000000 --- a/Yunju/2024-09-28-Two-Integer-Sum.py +++ /dev/null @@ -1,11 +0,0 @@ -#Two Integer Sum -# 해답 참고 -class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - prevMap = {} # val -> index - - for i, n in enumerate(nums): - diff = target - n - if diff in prevMap: - return [prevMap[diff], i] - prevMap[n] = i diff --git a/hj/week 1/AnagramGroups.cpp b/hj/week 1/AnagramGroups.cpp new file mode 100644 index 00000000..3c4958b5 --- /dev/null +++ b/hj/week 1/AnagramGroups.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + vector> groupAnagrams(vector& strs) { + vector> output; + unordered_map> um; + + for (string &str : strs) { + string key = str; + sort(key.begin(), key.end()); + um[key].push_back(str); + } + + for (auto &val : um) { + output.push_back(val.second); + } + + return output; + } +}; diff --git a/hj/week 1/StringEncodeAndDecode .cpp b/hj/week 1/StringEncodeAndDecode .cpp new file mode 100644 index 00000000..eb4a5cca --- /dev/null +++ b/hj/week 1/StringEncodeAndDecode .cpp @@ -0,0 +1,28 @@ +class Solution { +public: + + string encode(vector& strs) { + string encoded = ""; + for (auto &str : strs) { + encoded += to_string(str.size()) + "#" + str; + } + return encoded; + } + + vector decode(string s) { + vector decoded; + int i = 0; + + while (i < s.size()) { + int j = s.find('#', i); + int len = stoi(s.substr(i, j - i)); + + i = j + 1; + decoded.push_back(s.substr(i, len)); + + i += len; + } + + return decoded; + } +}; diff --git a/hj/week 1/TopKElementsInList .cpp b/hj/week 1/TopKElementsInList .cpp new file mode 100644 index 00000000..954df2ef --- /dev/null +++ b/hj/week 1/TopKElementsInList .cpp @@ -0,0 +1,24 @@ +class Solution { +public: + vector topKFrequent(vector& nums, int k) { + unordered_map um; + priority_queue> pq; + vector output; + + for (auto &num : nums) { + um[num]++; + } + + for (auto &pair : um) { + pq.push({pair.second, pair.first}); + } + + while (k > 0) { + output.push_back(pq.top().second); + pq.pop(); + k--; + } + + return output; + } +}; diff --git a/lykim/duplicate_integer.md b/lykim/duplicate_integer.md deleted file mode 100644 index 1a1dfa29..00000000 --- a/lykim/duplicate_integer.md +++ /dev/null @@ -1,24 +0,0 @@ -### Approach -- Use a data structure that doesn't contain duplicates, Hash set. -- Iterate the given array and construct a set, add? method will tell me if the element is already in the set or not - -### Complexity -- Time complexity - O(n) with the array iteration -- Space complexity - O(n) with the Set creation - -### Solution -``` -# @param {Integer[]} nums -# @return {Boolean} -def contains_duplicate(nums) - return false unless nums.size > 1 - - set = Set.new - - nums.each do |num| - return true unless set.add?(num) - end - - false -end -``` diff --git a/lykim/is_anagram.md b/lykim/is_anagram.md deleted file mode 100644 index 66098a4b..00000000 --- a/lykim/is_anagram.md +++ /dev/null @@ -1,31 +0,0 @@ -### Approach -- Use a data structure to record the occurrences by key, Hash map. -- Iterate the given arrays, increase first then decrease for the second one. -- Then check if the result hash value only should contain one 0. - -### Complexity -- Time complexity - O(n) with the array iterations -- Space complexity - O(n) with the Hash creation - -### Solution -``` -def is_anagram(s, t) - return false if s.length != t.length - - hash = Hash.new(0) - - s.each_char do |char| - hash[char] += 1 - end - - t.each_char do |char| - if hash.key?(char) - hash[char] -= 1 - else - return false - end - end - - hash.values.uniq == [0] -end -``` diff --git a/lykim/two_sum.md b/lykim/two_sum.md deleted file mode 100644 index 65522726..00000000 --- a/lykim/two_sum.md +++ /dev/null @@ -1,28 +0,0 @@ -### Approach -- Store the difference to the target for each element of the array, and index. -- To facilitate look-up with key, use Hash to record -- Iterating the array, if the current number is already in the hash map, return the indices, or store the difference. - -### Complexity -- Time complexity - O(n) with the array iteration -- Space complexity - O(n) with the Hash creation - -### Solution -``` -# @param {Integer[]} nums -# @param {Integer} target -# @return {Integer[]} -def two_sum(nums, target) - hash = Hash.new - - nums.each_with_index do |num, i| - difference = target - num - - if hash.key?(num) - return [hash[num], i] - else - hash[difference] = i - end - end -end -```