-
Notifications
You must be signed in to change notification settings - Fork 3
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
resolve #134-common-child 문제풀이 및 문서추가 #136
base: master
Are you sure you want to change the base?
Changes from all commits
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,35 @@ | ||
/** | ||
* @title Common Child | ||
* @difficulty Medium | ||
* @link https://www.hackerrank.com/challenges/common-child/problem | ||
*/ | ||
|
||
const lca = (string1, string2) => { | ||
const s1 = (string1.length <= string2.length) ? [...string1] : [...string2]; | ||
const s2 = (string1.length <= string2.length) ? [...string2] : [...string1]; | ||
const {length: rowLength} = s1; | ||
const {length: colLength} = s2; | ||
let row = s1.reduce(acc => { | ||
acc.push(0); | ||
|
||
return acc; | ||
}, []); | ||
row.push(0); | ||
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. let row = s1.reduce(acc => {
acc.push(0);
return acc;
}, []);
row.push(0); 이 코드의 의도가 뭔지 궁금합니다. 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. s1 사이즈를 사용해 배열을 사전에 초기ㅗㅘ하려는거 입니다 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. 배열을 초기화 하는 코드는 Array.from, 과 Array.fill 을 사용하는 편이 좋을것 같습니다. |
||
|
||
const cur = [...row]; | ||
|
||
for (let idxS2 = 0; idxS2 < colLength; idxS2++) { | ||
for (let idxS1 = 0; idxS1 < rowLength; idxS1++) { | ||
if (s1[idxS1] === s2[idxS2]) { | ||
cur[idxS1 + 1] = row[idxS1] + 1; | ||
} else { | ||
cur[idxS1 + 1] = (Math.max(cur[idxS1], row[idxS1 + 1])); | ||
} | ||
} | ||
row = [...cur]; | ||
} | ||
|
||
return cur[rowLength]; | ||
}; | ||
|
||
const commonChild = (s1, s2) => lca(s1, s2); |
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.
string1 이 s1 이 될지, string2 가 s1 이될지 불분명해 보이는데. longer, shorter 와 같이 의미있는 이름을 쓰면 더 명확해질것 같습니다.