diff --git a/combination-sum/Blossssom.ts b/combination-sum/Blossssom.ts new file mode 100644 index 0000000000..31cf62b857 --- /dev/null +++ b/combination-sum/Blossssom.ts @@ -0,0 +1,36 @@ +/** + * @param - candidates: 정수 배열, target: 대상 정수 + * @returns - 선택한 숫자의 합이 해당하는 모든 고유 조합 반환 + * @description + * 1. 요소는 여러번 사용 가능 + */ + +function combinationSum(candidates: number[], target: number): number[][] { + const result: number[][] = []; + + function recursive(remain: number, idx: number, path: number[]) { + if (remain === 0) { + result.push([...path]); + return; + } + + for (let i = idx; i < candidates.length; i++) { + const currentNum = candidates[i]; + if (currentNum > remain) { + break; + } + + path.push(currentNum); + recursive(remain - currentNum, i, path); + path.pop(); + } + } + + recursive(target, 0, []); + return result; +} + +const candidates = [2, 3, 5]; +const target = 8; +combinationSum(candidates, target); + diff --git a/number-of-1-bits/Blossssom.ts b/number-of-1-bits/Blossssom.ts new file mode 100644 index 0000000000..16952fb064 --- /dev/null +++ b/number-of-1-bits/Blossssom.ts @@ -0,0 +1,39 @@ +/** + * @param n - 양의 정수 + * @returns - 이진 변환 시 1의 갯수 + * @description + * - 풀이 1: 직접 이진수로 변환하는 작업을 추가 + * - 이진 수 변환 후 배열 변경, 1만 뽑아내서 길이 반환하기 + * - 시간복잡도: O(n), 공간복잡도: O(n) + */ + +// function hammingWeight(n: number): number { +// if (n === 0) return 0; +// let result = ""; +// while (n > 0) { +// result = (n % 2) + result; +// n = Math.floor(n / 2); +// } +// let count = 0; +// for (const ch of result) { +// if (ch === "1") { +// count++; +// } +// } +// return count; +// } + +function hammingWeight(n: number): number { + const bit = n + .toString(2) + .split("") + .filter((v) => v === "1"); + + return bit.length; +} + +const n = 2147483645; + +hammingWeight(n); + + diff --git a/valid-palindrome/Blossssom.ts b/valid-palindrome/Blossssom.ts new file mode 100644 index 0000000000..5ec437ea0e --- /dev/null +++ b/valid-palindrome/Blossssom.ts @@ -0,0 +1,77 @@ +/** + * @param s - 입력 문자열 + * @returns - 입력 문자열이 palindrome 인지 반환 + * @description + * 풀이 1 - 시간복잡도: O(n), 공간복잡도: O(n) + * 풀이 2 - 시간복잡도: O(n), 공간복잡도: O(1) - 초반 정규식 스캔 및 소문자 변경이 한번에 일어나지 않고, 조기 종료 + * 풀이 3 - 1번과 복잡도는 같지만 split으로 실제 실행시간은 훨씬 빠르게 진행 + */ +// function isPalindrome(s: string): boolean { +// const clearString = s.replace(/[^a-zA-Z0-9]/g, "").toLocaleLowerCase(); +// let strLen = clearString.length - 1; +// if (!clearString.length) { +// return true; +// } + +// for (let i = 0; i < Math.floor(clearString.length / 2); i++) { +// if (clearString[i] !== clearString[strLen]) { +// return false; +// } +// strLen--; +// } +// return true; +// } + +// function isPalindrome(s: string): boolean { +// let i = 0; +// let j = s.length - 1; + +// while (i < j) { +// if (!isAlphaNum[s[i]]) { +// i++; +// continue; +// } + +// if (!isAlphaNum[s[j]]) { +// j--; +// continue; +// } + +// if (s[i].toLocaleLowerCase() !== s[j].toLocaleLowerCase()) { +// return false; +// } + +// i++; +// j--; +// } +// return true; +// } + +// function isAlphaNum(str: string): boolean { +// const charC = str.charCodeAt(0); +// return ( +// (charC >= "0".charCodeAt(0) && charC <= "9".charCodeAt(0)) || +// (charC >= "A".charCodeAt(0) && charC <= "Z".charCodeAt(0)) || +// (charC >= "a".charCodeAt(0) && charC <= "z".charCodeAt(0)) +// ); +// } + +function isPalindrome(s: string): boolean { + const cleaned = s + .toLowerCase() + .replace(/[^a-zA-Z0-9]/g, "") + .split(""); + let left = 0; + let right = cleaned.length - 1; + while (left < right) { + if (cleaned[left] === cleaned[right]) { + left++; + right--; + } else { + return false; + } + } + return true; +} + +