Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Interview-Preparation-Kit/String-Manipulation/common-child.js
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];
Copy link
Owner

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 와 같이 의미있는 이름을 쓰면 더 명확해질것 같습니다.

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);
Copy link
Owner

Choose a reason for hiding this comment

The 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);

이 코드의 의도가 뭔지 궁금합니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s1 사이즈를 사용해 배열을 사전에 초기ㅗㅘ하려는거 입니다

Copy link
Owner

Choose a reason for hiding this comment

The 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);
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ Generates the README.md.
| --- | --- | --- |
| Easy | [2D Array - DS](https://www.hackerrank.com/challenges/2d-array/problem) | [Solution](./Interview-Preparation-Kit/Arrays/2d-array-ds.js)|
| Easy | [Left Rotation](https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem) | [Solution](./Interview-Preparation-Kit/Arrays/left-rotation.js)|
#### String-Manipulation
| Difficulty | Problem | Solution |
| --- | --- | --- |
| Medium | [Common Child](https://www.hackerrank.com/challenges/common-child/problem) | [Solution](./Interview-Preparation-Kit/String-Manipulation/common-child.js)|
#### Strings
| Difficulty | Problem | Solution |
| --- | --- | --- |
#### Warm-up-Challenges
| Difficulty | Problem | Solution |
| --- | --- | --- |
Expand Down