-
-
Notifications
You must be signed in to change notification settings - Fork 304
[JangAyeon] WEEK 03 Solutions #2092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6acd81c
bac506f
9dc2b08
76d6b82
5e78fe8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /** | ||
| * @param {number[]} candidates | ||
| * @param {number} target | ||
| * @return {number[][]} | ||
| */ | ||
| var combinationSum = function (arr, target) { | ||
| const N = arr.length; | ||
| const answer = []; | ||
| function dfs(total, idx, route) { | ||
| if (total >= target) { | ||
| if (total == target) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 현재는 |
||
| answer.push(route); | ||
| } | ||
| return; | ||
| } | ||
| for (let i = idx; i < N; i++) { | ||
| dfs(total + arr[i], i, [...route, arr[i]]); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 매 dfs 호출마다 배열을 복사하기 때문에 비용이 발생하는데요, |
||
| } | ||
| } | ||
|
|
||
| dfs(0, 0, []); | ||
| return answer; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| var numDecodings = function (s) { | ||
| const N = s.length; | ||
| const memo = {}; | ||
|
|
||
| function dfs(i) { | ||
| if (i === N) return 1; | ||
| if (s[i] === "0") return 0; // 0은 단독으로 decode 불가능 | ||
| if (memo[i] !== undefined) return memo[i]; | ||
|
|
||
| // 1자리 | ||
| let count = dfs(i + 1); | ||
|
|
||
| // 2자리 | ||
| if (i + 1 < N && Number(s.slice(i, i + 2)) <= 26) { | ||
| count += dfs(i + 2); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 코멘트와 코드만으로도 명확하게 이해되는 깔끔한 풀이인 것 같습니다!! 추가로 현재 코드는 memoization + dfs를 이용한 top-down DP로 저는 이를 bottom-up DP로 풀이하고, 이렇게도 풀 수 있어서 참고차 공유드려요~! |
||
| } | ||
|
|
||
| memo[i] = count; | ||
| return count; | ||
| } | ||
|
|
||
| return dfs(0); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number} | ||
| */ | ||
| var maxSubArray = function (nums) { | ||
| let pointer = nums[0]; | ||
| let answer = pointer; | ||
| const N = nums.length; | ||
| for (let idx = 1; idx < N; idx++) { | ||
| if (nums[idx] > pointer + nums[idx]) { | ||
| pointer = nums[idx]; | ||
| } else { | ||
| pointer += nums[idx]; | ||
| } | ||
| // console.log(idx, pointer) | ||
| answer = Math.max(pointer, answer); | ||
| } | ||
| return answer; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /** | ||
| * @param {number} n | ||
| * @return {number} | ||
| */ | ||
| var hammingWeight = function (n) { | ||
| function calDivision(num) { | ||
| return { v: Math.floor(num / 2), rest: num % 2 }; | ||
| } | ||
| let num = n; | ||
| let result = 0; | ||
| while (true) { | ||
| const { v, rest } = calDivision(num); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| result += rest; | ||
| if (v == 0) { | ||
| break; | ||
| } | ||
| num = v; | ||
| } | ||
|
|
||
| return result; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /** | ||
| * @param {string} s | ||
| * @return {boolean} | ||
| */ | ||
| var isPalindrome = function (s) { | ||
| const sLowered = [...s.toLowerCase().replace(" ", "")]; | ||
| const isAlpha = (item) => item.charCodeAt() >= 97 && item.charCodeAt() <= 122; | ||
| const isNumber = (item) => item.charCodeAt() >= 48 && item.charCodeAt() <= 57; | ||
| const str = sLowered.filter((c) => isAlpha(c) || isNumber(c)); | ||
| const N = str.length; | ||
| let [start, end] = [0, N - 1]; | ||
| while (start <= end) { | ||
| if (str[start] != str[end]) { | ||
| return false; | ||
| } | ||
| start++; | ||
| end--; | ||
| } | ||
| return true; | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
arr를 정렬한다면 백트래킹 시 pruning이 가능합니다!