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

[윤태권] Week2 문제 풀이 #353

Merged
merged 6 commits into from
Aug 25, 2024
Merged

Conversation

*
* 처음 문제를 보고 들었던 생각: 정렬 시켜서 같으면 anagram?
* -> 아, 그러면 등장한 문자의 빈도수가 같네?
* -> 결국 26 사이즈가 인풋에 영향을 받지 않으므로 공간 복잡도를 O(1)로 개선할 수 있고,
Copy link
Contributor

Choose a reason for hiding this comment

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

s.toCharArray()t.toCharArray() 에서 배열을 위한 공간을 추가로 요구하게 되기때문에
charAt 을 사용하도록 변경해야 O(1) 으로 개선될 것 같습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dev-jonghoonpark 오 그러네요! 리뷰 감사합니다 ㅎㅎ 👍

@taekwon-dev
Copy link
Contributor Author

taekwon-dev commented Aug 24, 2024

이번 주는 월요일 제외하고는 정말 여유가 없었던 것 같아요 🥲 그럼에도 풀기 위해 필요한 내용 정도는 미리미리 챙기며 2일에 한 문제라도 해결 할 수 있었을 것 같은데.. 3주차에는 도전해봅니다. 일정상 이번 주는 #218, #233, #253 까지 제출하고 PR Ready 로 전환합니다. :-)

@taekwon-dev taekwon-dev marked this pull request as ready for review August 24, 2024 01:29
* https://www.algodale.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
*
* <풀이1 기준> 시도!
* - 공간 복잡도: O(N^2) - Java 에서는 Call-by-value, 메서드 호출 시 인자 값이 복사되어 전달됨 / 배열 길이에 따라서 재귀 호출 스택이 점점 깊어짐 (각 호출마다 서브 배열 생성)
Copy link
Contributor

Choose a reason for hiding this comment

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

공간 복잡도가 O(n) 이 아니라 O(n^2) 인 이유가 무엇일까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dev-jonghoonpark 님 우선 리뷰 감사합니다!

재귀 함수 호출 과정에서 Call by value 로 Array 를 인자로 넘길 때 Copy가 일어난다고 판단해서 O(n^2)으로 본 것 같아요.
근데 생각해보니, Array는 값 자체를 복사하는 게 아니라 reference를 복사해서 넘기기 때문에 실질적으로 값 자체가 복사되는 것은 아닐 것 같고 - 그러면 말씀해주신 것 처럼 O(n)이 맞는 것 같네요.!

TreeNode root = new TreeNode(rootVal);

int rootIdxOnInorder = 0;
for (int i = inorderStartIdx; i <= inorderEndIdx; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

제가 자바 문법을 정확히 모르지만, 여기서 inorderEndIdx를 모두 for문으로 순회하지 않고 값의 인덱스를 찾는 방법은 없을까요? 그러면 더 시간복잡도가 줄어들 것 같아 여쭤봅니다!

Copy link
Contributor

Choose a reason for hiding this comment

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

설사 그런 내장 함수를 사용하더라도, 내부적으로 결국 순회하지 않을까요? 🤔
시간 복잡도를 줄이기는 어려울 것 같고, 코드 양을 좀 줄일 수는 있을 것 같습니당 😁

Copy link
Contributor

@DaleSeo DaleSeo left a comment

Choose a reason for hiding this comment

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

이번 주는 월요일 제외하고는 정말 여유가 없었던 것 같아요 🥲 그럼에도 풀기 위해 필요한 내용 정도는 미리미리 챙기며 2일에 한 문제라도 해결 할 수 있었을 것 같은데.. 3주차에는 도전해봅니다. 일정상 이번 주는 #218, #233, #253 까지 제출하고 PR Ready 로 전환합니다. :-)

일주일에 3문제 풀이가 어디에요? 수고하셨습니다!

TreeNode root = new TreeNode(rootVal);

int rootIdxOnInorder = 0;
for (int i = inorderStartIdx; i <= inorderEndIdx; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

설사 그런 내장 함수를 사용하더라도, 내부적으로 결국 순회하지 않을까요? 🤔
시간 복잡도를 줄이기는 어려울 것 같고, 코드 양을 좀 줄일 수는 있을 것 같습니당 😁

Comment on lines +6 to +9
* - a >> b: a 의 이진 표현을 오픈쪽으로 b 비트만큼 이동
* - i >> 1: a 의 이진 표현에서 가장 오픈쪽 하나가 잘림 (i는 양수 범위를 가지므로, 왼쪽은 0으로 채워짐)
* - a & b: AND 연산
* - i & 1: 이전 결과 값이 메모되어 있으므로, 내가 궁금한 것 가장 마지막 자리 수 값이 1인지 여부.
Copy link
Contributor

Choose a reason for hiding this comment

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

설명이 좋아용 👍

@taekwon-dev taekwon-dev merged commit f02b7bf into DaleStudy:main Aug 25, 2024
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

4 participants