Skip to content

[printjin-gmailcom] WEEK 03 solutions #1275

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

Merged
merged 7 commits into from
Apr 16, 2025

Conversation

printjin-gmailcom
Copy link
Contributor

@printjin-gmailcom printjin-gmailcom commented Apr 13, 2025

답안 제출 문제

작성자 체크 리스트

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

검토자 체크 리스트

Important

본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!

  • 바로 이전에 올라온 PR에 본인을 코드 리뷰어로 추가해주세요.
  • 본인이 검토해야하는 PR의 답안 코드에 피드백을 주세요.
  • 토요일 전까지 PR을 병합할 수 있도록 승인해주세요.

@printjin-gmailcom printjin-gmailcom changed the title Solve : Valid Palindrome [printjin-gmailcom] WEEK 03 solutions Apr 13, 2025
@seungriyou seungriyou self-requested a review April 13, 2025 04:01
@printjin-gmailcom printjin-gmailcom moved this from Solving to In Review in 리트코드 스터디 4기 Apr 13, 2025
Copy link
Contributor

@seungriyou seungriyou left a comment

Choose a reason for hiding this comment

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

고생하셨습니다~! 🍀

if '1' <= s[i - 1] <= '9':
dp[i] += dp[i - 1]
if '10' <= s[i - 2:i] <= '26':
dp[i] += dp[i - 2]
Copy link
Contributor

Choose a reason for hiding this comment

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

풀이가 정말 군더더기 없이 깔끔하고 가독성이 좋은 것 같습니다! 👍🏻

여기에 추가로 dp[i]를 구하기 위해 dp[i - 1]dp[i - 2]만 확인하기 때문에, dp 리스트 대신 변수 두 개를 이용해서 트래킹한다면 공간 복잡도를 O(n)에서 O(1)으로 더 최적화 할 수 있을 것 같아요~!

class Solution:
def maxSubArray(self, nums):
max_sum = current_sum = nums[0]
for num in nums[1:]:
Copy link
Contributor

Choose a reason for hiding this comment

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

DP 문제를 max_sumcurrent_sum 변수를 이용하여 O(n) space에서 O(1) space로 최적화 하신 걸로 보입니다!

다만, for 문에서 nums[1:]로 한 번 슬라이싱하신다면 코드 전체의 공간 복잡도가 O(n)으로 계산될 것이기 때문에, O(1)으로 더 최적화 하시려면 인덱스로 for 문을 구성하시는 것도 좋은 방법일 것 같습니다 😆

cnt = 0
while n > 0:
cnt += n % 2
n //= 2
Copy link
Contributor

Choose a reason for hiding this comment

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

저는 처음에 문제의 제목에 충실하게 bit manipulation 관점으로만 접근했었는데요, printjin님의 풀이를 보고 수학적으로도 풀어볼 수 있었습니다! 감사합니다~! 😎

@printjin-gmailcom printjin-gmailcom merged commit 7958e43 into DaleStudy:main Apr 16, 2025
1 check passed
@github-project-automation github-project-automation bot moved this from In Review to Completed in 리트코드 스터디 4기 Apr 16, 2025
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