Skip to content

Commit a2c03a6

Browse files
authored
Merge pull request #1792 from yhkee0404/main
[yhkee0404] WEEK 03 solutions
2 parents 7a813ab + 87da9b6 commit a2c03a6

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed

combination-sum/yhkee0404.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function combinationSum(candidates: number[], target: number, dp = new Map()): number[][] {
2+
if (target <= 0) {
3+
return [];
4+
}
5+
let ans = dp.get(target);
6+
if (ans !== undefined) {
7+
return ans;
8+
}
9+
ans = [];
10+
for (const candidate of candidates) {
11+
if (target == candidate) {
12+
ans.push([candidate]);
13+
continue;
14+
}
15+
for (const combination of combinationSum(candidates, target - candidate, dp)) {
16+
if (combination[combination.length - 1] > candidate) {
17+
continue;
18+
}
19+
ans.push([...combination, candidate]);
20+
}
21+
}
22+
dp.set(target, ans);
23+
return ans;
24+
};

decode-ways/yhkee0404.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func numDecodings(s string) int {
2+
dp := make([]int, len(s) + 1)
3+
dp[0] = 1
4+
const cnt = rune('Z') - rune('A') + 1
5+
for i, c := range s {
6+
a := 0
7+
if i != 0 {
8+
b := (rune(s[i - 1]) - rune('0')) * 10 + rune(c) - rune('0')
9+
if b > 9 && b <= cnt {
10+
a += dp[i - 1]
11+
}
12+
}
13+
b := rune(c) - rune('0')
14+
if b != 0 && b < cnt {
15+
a += dp[i]
16+
}
17+
dp[i + 1] = a
18+
}
19+
return dp[len(s)]
20+
}

maximum-subarray/yhkee0404.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
impl Solution {
2+
pub fn max_sub_array(nums: Vec<i32>) -> i32 {
3+
return Self::solve(&nums, 0, nums.len()).unwrap_or(0);
4+
}
5+
fn solve(nums: &Vec<i32>, l: usize, r: usize) -> Option<i32> {
6+
if l >= r {
7+
return None
8+
}
9+
if l + 1 == r {
10+
return Some(nums[l])
11+
}
12+
let mid = l + ((r - l) >> 1);
13+
let a = Self::solve(nums, l, mid);
14+
let b = Self::solve(nums, mid, r);
15+
if a.is_none() || b.is_none() {
16+
return a.or(b)
17+
}
18+
let mut ans = a.max(b);
19+
let mut c = 0;
20+
let mut d = 0;
21+
for i in (l..mid).rev() {
22+
c += nums[i];
23+
d = d.max(c);
24+
}
25+
if d == 0 {
26+
return ans
27+
}
28+
c = d;
29+
for i in mid..r {
30+
c += nums[i];
31+
d = d.max(c);
32+
}
33+
ans.max(Some(d))
34+
}
35+
}

number-of-1-bits/yhkee0404.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
@lru_cache
3+
def hammingWeight(self, n: int) -> int:
4+
return 1 + self.hammingWeight(n - (n & - n)) if n else 0

valid-palindrome/yhkee0404.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function isPalindrome(s: string): boolean {
2+
const isAlpha = x => x.toLowerCase() >= 'a' && x.toLowerCase() <= 'z';
3+
const isNumeric = x => x >= '0' && x <= '9';
4+
const isAlphanumeric = x => isAlpha(x) || isNumeric(x);
5+
let i = 0, j = s.length - 1;
6+
while (i < j) {
7+
while (i !== j && ! isAlphanumeric(s[i])) {
8+
i++;
9+
}
10+
while (i !== j && ! isAlphanumeric(s[j])) {
11+
j--;
12+
}
13+
if (s[i].toLowerCase() !== s[j].toLowerCase()) {
14+
return false;
15+
}
16+
i++, j--;
17+
}
18+
return true;
19+
};

0 commit comments

Comments
 (0)