diff --git a/best-time-to-buy-and-sell-stock/leehyeyun.js b/best-time-to-buy-and-sell-stock/leehyeyun.js new file mode 100644 index 000000000..8ff63470b --- /dev/null +++ b/best-time-to-buy-and-sell-stock/leehyeyun.js @@ -0,0 +1,76 @@ +/** + * @param {number[]} prices + * @return {number} + */ +/* +주어진 배열 prices에서 prices[i]는 i번째 날의 주가를 의미한다. + +한 번의 거래(1회 매수 + 1회 매도)만 할 수 있으며, +미래의 어떤 날에 매도해야 한다. (즉, 매수일 < 매도일) + +목표: + - 매수 후 매도하여 만들 수 있는 최대 이익을 계산한다. + - 이익을 낼 수 없다면 0을 반환한다. + +입력 형식 : + - prices: 정수 배열 + - 1 <= prices.length <= 100,000 + - 0 <= prices[i] <= 10,000 + +출력 형식 : + - 만들 수 있는 최대 이익 (정수) + +예시 : + + Example 1 + 입력 : prices = [7,1,5,3,6,4] + 출력 : 5 + 설명 : + - 2번째 날(가격 1)에 매수 + - 5번째 날(가격 6)에 매도 + - 이익 = 6 - 1 = 5 + + ※ 매수일보다 앞선 날짜에 매도하는 것은 불가능함. + + Example 2 + 입력 : prices = [7,6,4,3,1] + 출력 : 0 + 설명 : + - 어떤 날을 매수해도 이후 가격이 상승하지 않음 + - 이익을 낼 수 없으므로 0 반환 +*/ +var maxProfit = function(prices) { + + let min_value = prices[0]; + let min_value_index = 0; + let max_value = 0; + + for (let i = 0; i < prices.length; i ++) { + if(prices[i] < min_value) + { + min_value = prices[i]; + min_value_index = i; + } + } + + if(min_value_index == (prices.length - 1)) + { + return 0; + } + + for (min_value_index; min_value_index < prices.length; min_value_index++) + { + + if (max_value < prices[min_value_index]) { + max_value = prices[min_value_index]; + } + } + + return max_value - min_value; +}; + +console.log(maxProfit([7,1,5,3,6,4])) +console.log(maxProfit([7,6,4,3,1])) + + + diff --git a/encode-and-decode-strings/leehyeyun.js b/encode-and-decode-strings/leehyeyun.js new file mode 100644 index 000000000..c4d2097cf --- /dev/null +++ b/encode-and-decode-strings/leehyeyun.js @@ -0,0 +1,51 @@ +var encode = function (strs) { + // strs: string[] + // return: string + // write your code here + + let encode_result = ""; + + for (const encode_strs of strs) + { + const encode_strs_length = encode_strs.length; + + if(encode_result != undefined) + { + encode_result = encode_result + encode_strs_length + "|" + encode_strs; + } + + } + + return encode_result; +}; + +var decode = function (str) { + // str: string + // return: string[] + // write your code here + + let results = []; + + while (str.length > 0) { + let i = 0; + while (str[i] >= '0' && str[i] <= '9') { + i++; + } + + let decode_str_length = str.substring(0, i); + let len = parseInt(decode_str_length, 10); + + let newStr = str.substring(i + 1); + let result = newStr.substr(0,decode_str_length) + + str = newStr.substring(len); + + results.push(result); + } + + return results; +}; + +console.log(decode(encode(["lint","code","love","you"]))); +console.log(decode(encode(["we","say",":","yes"]))); + diff --git a/group-anagrams/leehyeyun.js b/group-anagrams/leehyeyun.js new file mode 100644 index 000000000..ee28a1ab7 --- /dev/null +++ b/group-anagrams/leehyeyun.js @@ -0,0 +1,114 @@ +/** + * @param {string[]} strs + * @return {string[][]} + */ +/* +주어진 문자열 배열 strs에서, +서로 아나그램(anagram) 관계인 문자열들을 묶어 그룹으로 반환하는 문제이다. + +아나그램이란? + - 문자열의 문자들을 재배열하여 서로 같은 구성을 만들 수 있는 문자열을 의미한다. + - 예: "eat", "tea", "ate"는 같은 아나그램 그룹에 속함. + +목표: + - strs 내 모든 문자열을 아나그램끼리 묶은 그룹들의 배열로 반환한다. + - 그룹의 순서나, 그룹 내부 문자열의 순서는 중요하지 않다. + +입력 형식 : + - strs: 문자열 배열 + - 1 <= strs.length <= 10,000 + - 0 <= strs[i].length <= 100 + - strs[i]는 모두 소문자 영어 알파벳으로 구성됨 + +출력 형식 : + - 아나그램끼리 묶인 그룹들의 배열 (2차원 배열) + +예시 : + + Example 1 + 입력 : ["eat","tea","tan","ate","nat","bat"] + 출력 : [["bat"], ["nat","tan"], ["ate","eat","tea"]] + 설명 : + - "eat", "tea", "ate"는 문자 구성이 동일하므로 같은 그룹 + - "tan", "nat"도 서로 아나그램이므로 같은 그룹 + - "bat"는 다른 어떤 문자열로도 아나그램을 만들 수 없으므로 단독 그룹 + + Example 2 + 입력 : [""] + 출력 : [[""]] + 설명 : + - 빈 문자열도 하나의 문자열로 취급되며, 자신과 함께 단일 그룹 생성 + + Example 3 + 입력 : ["a"] + 출력 : [["a"]] + 설명 : + - 단일 문자 하나만 있으므로 그대로 그룹화됨 +*/ +var groupAnagrams = function (strs) { + + const processed = new Set(); + const result = []; + + for (let i = 0; i < strs.length; i++) { + + const base_word = strs[i]; + if (processed.has(base_word)) continue; + + const baseMap = new Map(); + + for (const base_word_char of base_word) { + if (baseMap.has(base_word_char)) { + let base_word_char_count = baseMap.get(base_word_char); + + base_word_char_count++; + baseMap.set(base_word_char, base_word_char_count) + } else { + baseMap.set(base_word_char, 1) + } + } + + const group = [base_word]; + processed.add(base_word); + + for (let j = i + 1; j < strs.length; j++) { + + const candidate_word = strs[j]; + + if (processed.has(candidate_word)) continue; + + const compareMap = new Map([...baseMap]); + + for (const candidate_word_char of candidate_word) { + if (compareMap.has(candidate_word_char)) { + + let candidate_word_char_count = compareMap.get(candidate_word_char) + candidate_word_char_count--; + + if (candidate_word_char_count === 0) { + compareMap.delete(candidate_word_char) + } else if (candidate_word_char_count > 0) { + compareMap.set(candidate_word_char, candidate_word_char_count) + } + } + } + + //최종 리턴 + if (compareMap.size == 0) { + processed.add(candidate_word); + group.push(candidate_word); + } + } + + result.push(group); + + } + + return result; +}; + +console.log(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"])) +console.log(groupAnagrams([""])) +console.log(groupAnagrams(["a"])) + +