diff --git a/combination-sum/yhkee0404.ts b/combination-sum/yhkee0404.ts new file mode 100644 index 0000000000..7caf9243de --- /dev/null +++ b/combination-sum/yhkee0404.ts @@ -0,0 +1,24 @@ +function combinationSum(candidates: number[], target: number, dp = new Map()): number[][] { + if (target <= 0) { + return []; + } + let ans = dp.get(target); + if (ans !== undefined) { + return ans; + } + ans = []; + for (const candidate of candidates) { + if (target == candidate) { + ans.push([candidate]); + continue; + } + for (const combination of combinationSum(candidates, target - candidate, dp)) { + if (combination[combination.length - 1] > candidate) { + continue; + } + ans.push([...combination, candidate]); + } + } + dp.set(target, ans); + return ans; +}; diff --git a/decode-ways/yhkee0404.go b/decode-ways/yhkee0404.go new file mode 100644 index 0000000000..f0d3a67a24 --- /dev/null +++ b/decode-ways/yhkee0404.go @@ -0,0 +1,20 @@ +func numDecodings(s string) int { + dp := make([]int, len(s) + 1) + dp[0] = 1 + const cnt = rune('Z') - rune('A') + 1 + for i, c := range s { + a := 0 + if i != 0 { + b := (rune(s[i - 1]) - rune('0')) * 10 + rune(c) - rune('0') + if b > 9 && b <= cnt { + a += dp[i - 1] + } + } + b := rune(c) - rune('0') + if b != 0 && b < cnt { + a += dp[i] + } + dp[i + 1] = a + } + return dp[len(s)] +} diff --git a/maximum-subarray/yhkee0404.rs b/maximum-subarray/yhkee0404.rs new file mode 100644 index 0000000000..20fabda3d8 --- /dev/null +++ b/maximum-subarray/yhkee0404.rs @@ -0,0 +1,35 @@ +impl Solution { + pub fn max_sub_array(nums: Vec) -> i32 { + return Self::solve(&nums, 0, nums.len()).unwrap_or(0); + } + fn solve(nums: &Vec, l: usize, r: usize) -> Option { + if l >= r { + return None + } + if l + 1 == r { + return Some(nums[l]) + } + let mid = l + ((r - l) >> 1); + let a = Self::solve(nums, l, mid); + let b = Self::solve(nums, mid, r); + if a.is_none() || b.is_none() { + return a.or(b) + } + let mut ans = a.max(b); + let mut c = 0; + let mut d = 0; + for i in (l..mid).rev() { + c += nums[i]; + d = d.max(c); + } + if d == 0 { + return ans + } + c = d; + for i in mid..r { + c += nums[i]; + d = d.max(c); + } + ans.max(Some(d)) + } +} diff --git a/number-of-1-bits/yhkee0404.py b/number-of-1-bits/yhkee0404.py new file mode 100644 index 0000000000..74c6bcbca8 --- /dev/null +++ b/number-of-1-bits/yhkee0404.py @@ -0,0 +1,4 @@ +class Solution: + @lru_cache + def hammingWeight(self, n: int) -> int: + return 1 + self.hammingWeight(n - (n & - n)) if n else 0 diff --git a/valid-palindrome/yhkee0404.ts b/valid-palindrome/yhkee0404.ts new file mode 100644 index 0000000000..7f23194b37 --- /dev/null +++ b/valid-palindrome/yhkee0404.ts @@ -0,0 +1,19 @@ +function isPalindrome(s: string): boolean { + const isAlpha = x => x.toLowerCase() >= 'a' && x.toLowerCase() <= 'z'; + const isNumeric = x => x >= '0' && x <= '9'; + const isAlphanumeric = x => isAlpha(x) || isNumeric(x); + let i = 0, j = s.length - 1; + while (i < j) { + while (i !== j && ! isAlphanumeric(s[i])) { + i++; + } + while (i !== j && ! isAlphanumeric(s[j])) { + j--; + } + if (s[i].toLowerCase() !== s[j].toLowerCase()) { + return false; + } + i++, j--; + } + return true; +};