diff --git a/combination-sum/jiji-hoon96.ts b/combination-sum/jiji-hoon96.ts new file mode 100644 index 000000000..710faec84 --- /dev/null +++ b/combination-sum/jiji-hoon96.ts @@ -0,0 +1,49 @@ +/** + * + * @param candidates + * @param target + * + * backtracking 알고리즘으로 문제 해결 + * + */ + +function combinationSum(candidates: number[], target: number): number[][] { + const result : number[][] = []; + if(candidates.length === 0){ + return result ; + } + + candidates.sort((a,b)=> a-b); + + const validCandidates : number[] = candidates.filter(num => num <= target); + + if(validCandidates.length ===0) { + return result; + } + + const currentCombination : number[] = []; + + function backtrack (startIndex : number, remainingTarget : number) :void { + if(remainingTarget === 0){ + result.push([...currentCombination]); + return; + } + + for(let i=startIndex; i remainingTarget) { + break; + } + currentCombination.push(currentNum); + + backtrack(i,remainingTarget - currentNum) + + currentCombination.pop() + + } + } + + backtrack(0, target); + return result; + }; diff --git a/decode-ways/jiji-hoon96.ts b/decode-ways/jiji-hoon96.ts new file mode 100644 index 000000000..6ff14393a --- /dev/null +++ b/decode-ways/jiji-hoon96.ts @@ -0,0 +1,35 @@ +function numDecodings(s: string): number { + // 빈 문자열이거나 0으로 시작하면 디코딩 불가 + if (!s || s[0] === '0') return 0; + + const n = s.length; + + // 문자열 길이가 1이면 바로 결과 반환 + if (n === 1) return 1; + + // 초기 상태 + let prev = 1; // dp[0] + let curr = s[0] === '0' ? 0 : 1; // dp[1] + + for (let i = 2; i <= n; i++) { + let temp = 0; + const oneDigit = parseInt(s[i - 1]); + const twoDigit = parseInt(s[i - 2] + s[i - 1]); + + // 한 자리 숫자로 디코딩 (1-9) + if (oneDigit >= 1) { + temp += curr; + } + + // 두 자리 숫자로 디코딩 (10-26) + if (twoDigit >= 10 && twoDigit <= 26) { + temp += prev; + } + + // 상태 업데이트 + prev = curr; + curr = temp; + } + + return curr; +} diff --git a/maximum-subarray/jiji-hoon96.ts b/maximum-subarray/jiji-hoon96.ts new file mode 100644 index 000000000..282274cad --- /dev/null +++ b/maximum-subarray/jiji-hoon96.ts @@ -0,0 +1,20 @@ +function maxSubArray(nums: number[]): number { + // 배열이 비어 있는 경우 체크 (제약조건에 의해 발생하지 않지만, 견고한 코드를 위해) + if (nums.length === 0) return 0; + + // 현재 부분 배열의 합과 전체 최대 부분 배열 합 초기화 + let currentSum = nums[0]; + let maxSum = nums[0]; + + // 두 번째 요소부터 순회 + for (let i = 1; i < nums.length; i++) { + // 현재 위치에서의 최대 부분 배열 합 계산 + // "이전까지의 합 + 현재 요소" vs "현재 요소부터 새로 시작" + currentSum = Math.max(nums[i], currentSum + nums[i]); + + // 전체 최대값 업데이트 + maxSum = Math.max(maxSum, currentSum); + } + + return maxSum; +} diff --git a/number-of-1-bits/jiji-hoon96.ts b/number-of-1-bits/jiji-hoon96.ts new file mode 100644 index 000000000..ad2d34fd4 --- /dev/null +++ b/number-of-1-bits/jiji-hoon96.ts @@ -0,0 +1,31 @@ +/** + * + * @param n + * + * 풀이 1 + * + * function hammingWeight(n: number): number { + * let parse = (n).toString(2); + * let count = 0; + * for (const item of parse){ + * if(+item ===1) count ++ + * } + * return count + * }; + * + * 숫자를 문자열로 변환할때 오버헤드 발생 + * 비트 연산을 사용해야할듯! + * + * 검색해보니 Brian Kernighan 알고리즘을 사용하면 더 효율적이라고 한다 + */ + +function hammingWeight(n: number): number { + let count = 0; + + while (n !== 0) { + n &= (n - 1); + count++; + } + + return count; +} diff --git a/valid-palindrome/jiji-hoon96.ts b/valid-palindrome/jiji-hoon96.ts new file mode 100644 index 000000000..9dab82428 --- /dev/null +++ b/valid-palindrome/jiji-hoon96.ts @@ -0,0 +1,34 @@ +/** + * + * @param s + * + * 풀이 1 + * + * 특수문자 정규 표현식이 복잡하고, 분할과 합치는 과정이 중복된다 + * + * function isPalindrome(s: string): boolean { + * const reg= /[\{\}\[\]\/?.,;:|\)*~`!^\-_+<>@\#$%&\\\=\(\'\"]/gi; + * let palindrome= s.replace(reg,'').toLowerCase().split(''); + * + * return palindrome.join('').replace(/ /g,"")===palindrome.reverse().join('').replace(/ /g,"") + * }; + * + * 그래서 생각한 풀이 2는 s consists only of printable ASCII characters. 을 보고 숫자와 알파벳을 제외하고 나머지는 제거하고 + * 투포인트 방법으로 변경해서 문제 해결 + */ + +function isPalindrome(s: string): boolean { + const cleanStr = s.toLowerCase().replace(/[^a-z0-9]/g, ''); + + let left = 0; + let right = cleanStr.length-1; + + while(left < right){ + if(cleanStr[left] !== cleanStr[right]){ + return false; + } + left++; + right--; + } + return true +};