Skip to content

Commit cf658ad

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 7bd6daa + c6a817c commit cf658ad

File tree

14 files changed

+232
-0
lines changed

14 files changed

+232
-0
lines changed

combination-sum/Wonjuny0804.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function combinationSum(candidates: number[], target: number): number[][] {
2+
const result: number[][] = [];
3+
4+
const dfs = (start: number, target: number, path: number[]) => {
5+
if (target === 0) {
6+
result.push([...path]);
7+
return;
8+
}
9+
10+
for (let i = start; i < candidates.length; i++) {
11+
if (candidates[i] > target) continue;
12+
13+
path.push(candidates[i]);
14+
dfs(i, target - candidates[i], path);
15+
path.pop();
16+
}
17+
}
18+
19+
candidates.sort((a, b) => a - b);
20+
dfs(0, target, []);
21+
return result;
22+
};

combination-sum/hoyeongkwak.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function combinationSum(candidates: number[], target: number): number[][] {
2+
const dp: number[][][] = Array(target + 1).fill(null).map(() => [])
3+
dp[0] = [[]]
4+
5+
for (let i = 1; i <= target; i++){
6+
for (const num of candidates) {
7+
if (i - num >= 0 && dp[i - num].length > 0) {
8+
for (const combo of dp[i - num]) {
9+
if (combo.length === 0 || num >= combo[combo.length - 1]) {
10+
dp[i].push([...combo, num])
11+
}
12+
}
13+
}
14+
}
15+
}
16+
return dp[target]
17+
};

decode-ways/Wonjuny0804.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function numDecodings(s: string): number {
2+
const memo: Record<number, number> = {};
3+
4+
const dfs = (index: number): number => {
5+
if (index === s.length) return 1;
6+
if (s[index] === '0') return 0;
7+
8+
if (memo[index] !== undefined) return memo[index];
9+
10+
let res = dfs(index + 1);
11+
12+
if (index + 1 < s.length && Number(s.slice(index, index + 2)) <= 26) {
13+
res += dfs(index + 2);
14+
}
15+
16+
memo[index] = res;
17+
return res;
18+
}
19+
20+
return dfs(0);
21+
}

decode-ways/hoyeongkwak.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
time complexity : O(n)
3+
space complexity : O(n)
4+
*/
5+
function numDecodings(s: string): number {
6+
const memo = new Map<number, number>()
7+
const decode = (index: number): number => {
8+
if (index === s.length) return 1
9+
if (s[index] === '0') return 0
10+
if (memo.has(index)) return memo.get(index)
11+
12+
let ways = decode(index + 1)
13+
if (index + 1 < s.length && (s[index] === '1' || (s[index] === '2' && parseInt(s[index + 1]) <= 6))) {
14+
ways += decode(index + 2)
15+
}
16+
memo.set(index, ways)
17+
return ways
18+
}
19+
return decode(0)
20+
}

maximum-subarray/Wonjuny0804.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function maxSubArray(nums: number[]): number {
2+
const n = nums.length;
3+
const dp = new Array(n).fill(0);
4+
dp[0] = nums[0];
5+
let result = dp[0];
6+
7+
for (let i = 1; i < n; i++) {
8+
dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]);
9+
result = Math.max(result, dp[i]);
10+
}
11+
12+
return result;
13+
}

maximum-subarray/hoyeongkwak.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
time complexity : O(n)
3+
space complexity : O(1)
4+
*/
5+
function maxSubArray(nums: number[]): number {
6+
let maxSum = -Infinity
7+
let currSum = 0
8+
9+
for (let i = 0; i < nums.length; i++) {
10+
currSum += nums[i]
11+
12+
if (currSum > maxSum) {
13+
maxSum = currSum
14+
}
15+
16+
if (currSum < 0) {
17+
currSum = 0
18+
}
19+
}
20+
return maxSum
21+
};

number-of-1-bits/RiaOh.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* @param {number} n
33
* @return {number}
44
*/
5+
6+
// week3 다시 올림
57
var hammingWeight = function (n) {
68
const binary = n.toString(2);
79
const arr = [...binary];

number-of-1-bits/Wonjuny0804.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function hammingWeight(n: number): number {
2+
return n
3+
.toString(2)
4+
.split("")
5+
.reduce((acc, i) => acc + +i, 0);
6+
}

number-of-1-bits/hoyeongkwak.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
time complexity : O(log n)
3+
space complexity : O(1)
4+
*/
5+
function hammingWeight(n: number): number {
6+
let count = 0
7+
while ( n != 0) {
8+
count += n & 1
9+
n >>>= 1
10+
}
11+
return count
12+
13+
/*
14+
time complexity : O(log n)
15+
space complexity : O(log n)
16+
*/
17+
// const twoBits = n.toString(2)
18+
// const bitCount = twoBits.split('').filter((s) => s === '1').length
19+
// return bitCount
20+
};

two-sum/Wonjuny0804.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
3+
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
4+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
5+
You can return the answer in any order.
6+
7+
Example 1:
8+
9+
Input: nums = [2,7,11,15], target = 9
10+
Output: [0,1]
11+
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
12+
13+
Example 2:
14+
15+
Input: nums = [3,2,4], target = 6
16+
Output: [1,2]
17+
18+
Example 3:
19+
20+
Input: nums = [3,3], target = 6
21+
Output: [0,1]
22+
23+
*/
24+
25+
function twoSum(nums: number[], target: number): number[] {
26+
const map = new Map();
27+
28+
for (let i = 0; i < nums.length; i++) {
29+
const diff = target - nums[i];
30+
if (map.has(nums[i])) return [map.get(nums[i]), i];
31+
else map.set(diff, i);
32+
}
33+
34+
return [];
35+
}

0 commit comments

Comments
 (0)