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

[Week 7] forest000014 #943

Merged
merged 8 commits into from
Jan 25, 2025
Merged

[Week 7] forest000014 #943

merged 8 commits into from
Jan 25, 2025

Conversation

forest000014
Copy link
Contributor

@forest000014 forest000014 commented Jan 22, 2025

답안 제출 문제

체크 리스트

  • 우측 메뉴에서 PR을 Projects에 추가해주세요.
  • Projects의 오른쪽 버튼(▼)을 눌러 확장한 뒤, Week를 현재 주차로 설정해주세요.
  • 바로 앞에 PR을 열어주신 분을 코드 검토자로 지정해주세요.
  • 문제를 모두 푸시면 프로젝트에서 StatusIn Review로 설정해주세요.
  • 코드 검토자 1분 이상으로부터 승인을 받으셨다면 PR을 병합해주세요.

@forest000014 forest000014 requested a review from Yjason-K January 22, 2025 12:21
@forest000014 forest000014 self-assigned this Jan 22, 2025
@forest000014 forest000014 requested a review from a team as a code owner January 22, 2025 12:21
@github-actions github-actions bot added the java label Jan 22, 2025
@forest000014
Copy link
Contributor Author

지난 6주차 longest increasing subsequence 문제의 주석을 수정했었는데, 지난 주 PR에 푸시가 안 되었었는지 이번 PR에 함께 반영되었습니다.
그 부분은 무시하고 리뷰 봐 주셔도 될 것 같습니다! 😄

Copy link
Contributor

@obzva obzva left a comment

Choose a reason for hiding this comment

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

한 주 수고 많으셨습니다 forest님 :)
이번에도 풀이 및 설명을 자세히 적어주셔서 리뷰하기 편했습니다
코멘트를 남겨놓았으니 병합 전에 확인 바랍니다
감사합니다

Comment on lines +27 to +29
while (begin < end && s.charAt(begin) != s.charAt(end)) {
set.remove(s.charAt(begin++));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

왠지 forest님께서는 관심 있으실 것 같아서 key-value hashmap을 이용하는 풀이도 남깁니다 :)

class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        length = 0
        left = 0
        lookup = {}

        for right, char in enumerate(s):
            if char in lookup and lookup[char] >= left:
                left = lookup[char] + 1
            lookup[char] = right
            length = max(length, right + 1 - left)

        return length

시간 복잡도는 둘 다 O(N)인데, key-value로 관리하는 풀이가 연산량이 최대 두 배 적을 것으로 생각합니다

  • set
    • 문자열의 각 문자마다 최대 두 번의 연산 실행 (set에 추가, set에서 삭제)
    • O(2N) = O(N)
  • key-value
    • 삭제하는 과정이 필요 없음
    • O(N)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

삭제 연산도 적고, left 포인터도 한번에 점프할 수 있어서 더 효율적이겠네요! 한 칸씩 전진하는 걸 더 줄여볼 생각을 했더라면 떠올렸을지도 모르겠네요... 다음 번엔 좀 더 끈질기게 고민해봐야겠습니다 😄

Comment on lines +15 to +24
solution 3. matrix 내에 안 쓰이는 값 찾기 (probabilistic 접근)
int 범위 내에서, 쓰이는 값보다는 안 쓰이는 값의 갯수가 압도적으로 많다.(1 - (200 * 200 / 2^32) = 0.99999+)

matrix 내에 안 쓰이는 수를 찾을 때까지 int 범위 내의 랜덤하게 뽑는 행위를 10번만 반복해도,
O(m * n) 시간에 상당히 높은 확률로 안 쓰이는 값을 찾을 수 있다.
(10번 이내에 찾지 못할 확률은 10^(-50) 정도.)
이렇게 찾은 값을 x라고 하자. matrix의 모든 원소를 순회하며, 0인 원소가 있다면 같은 행/열에 존재하는 모든 원소(또다른 0은 제외)를 x로 바꾼 뒤에, 마지막에 한번에 모든 x를 0으로 바꾸는 식으로 풀 수 있다.

그러나 이 접근법의 확률은 문제의 제한 조건 m, n 범위 하에서 계산한 것이라는 한계가 있다.
m, n이 꽤나 커진다면 랜덤 추출로 안 쓰이는 값을 찾을 확률이 낮아지고, 극단적으로 m * n 이 2^32 이상이 되면, 쓸 수 없는 방법이기도 하다.
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 Author

Choose a reason for hiding this comment

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

이 방법을 떠올렸을 땐 꽤나 괜찮아 보였는데, 다시 생각해보니 m, n 크기에 의존하는 풀이더라구요... 🥲 아쉬웠습니다

for (int i = 1; i < m; i++) {
dp[i][0] = 1;
for (int j = 1; j < n; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
Copy link
Contributor

Choose a reason for hiding this comment

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

이 부분도 forest님께서는 관심 있어하실까봐 코멘트 남깁니다 :)
점화식을 잘 보면, 우리가 연산을 실행할 때 필요한 행은 i, i-1 둘 뿐입니다 (dp[i], dp[i-1])
이 사실을 바탕으로 공간 복잡도를 O(mn)에서 O(n)으로 최적화시킬 수 있을까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

안녕하세요 obzva님, 지난 주에 이어서 이번 주도 상세한 리뷰 감사드려요.
사실 말씀해주신 풀이도 아이디어는 떠올렸는데, 구현을 어떻게 할지 고민하다가 애기 재우러 가느라 더 못했습니다 😂 이따 집 가서 한번 더 생각해봐야겠네요

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@obzva
말씀해주신 아이디어로, 공간 복잡도를 O(n)으로 줄여보았습니다 :)

@forest000014 forest000014 merged commit 45f6981 into DaleStudy:main Jan 25, 2025
1 check 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.

2 participants