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

50-xxubin04 #191

Merged
merged 3 commits into from
Sep 5, 2024
Merged

50-xxubin04 #191

merged 3 commits into from
Sep 5, 2024

Conversation

xxubin04
Copy link
Member

@xxubin04 xxubin04 commented Jun 4, 2024

πŸ”— 문제 링크

생각보닀 μ‰½κ²Œ κ΅¬ν˜„ν•΄μ„œ 두 문제 ν’€μ—ˆμŠ΅λ‹ˆλ‹€..!
LeetCode 14: Longest Common Prefix
LeetCode 35: Search Insert Position


βœ”οΈ μ†Œμš”λœ μ‹œκ°„

20λΆ„


✨ μˆ˜λ„ μ½”λ“œ

문제 이해 - 14

# TC 1

strs = ["flower", "flow", "flight"] 
"fl"

# TC 2

strs = ["dog", "racecar", "car"]
""

strs둜 μ£Όμ–΄μ§€λŠ” λ¬Έμžμ—΄λ“€μ—μ„œ λͺ¨λ‘ λ™μΌν•˜κ²Œ κ°€μ§€λŠ” μ΅œλŒ€ 길이의 접두사λ₯Ό 좜λ ₯ν•˜λŠ” λ¬Έμ œμ΄λ‹€.
κ·ΈλŸ¬ν•œ 쑰건을 λ§Œμ‘±ν•˜λŠ” 접두사가 μ—†λ‹€λ©΄ ""을 좜λ ₯ν•œλ‹€.


2. μ½”λ“œ 뢄석

        ans = ""
        sList = [list(i) for i in strs]
        sList.sort(key=len)

μ£Όμ–΄μ§€λŠ” λ¬Έμžμ—΄μ˜ 리슀트인 strsλ₯Ό ν•œ κΈ€μžμ”© λΆ„ν•΄ν•˜μ—¬ 2차원 리슀트 sList둜 λ§Œλ“ λ‹€.
ex) sList = [[f, l, o, w, e, r], [f, l, o, w], [f, l, i, g, h, t]]

sListλ₯Ό 리슀트 길이λ₯Ό κΈ°μ€€μœΌλ‘œ μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬ν•œλ‹€.


        for i in range(len(sList[0])):
            for j in sList[1:]:
                if sList[0][:i+1] != j[:i+1]:
                    return "".join(ans)

sList[0]의 길이 = μ ‘λ‘μ‚¬μ˜ μ΅œλŒ€ 길이 μ΄λ―€λ‘œ len(sList[0])만큼 μˆœνšŒν•˜λ©°,
sListμ—μ„œ 제일 짧은 λ¬Έμžμ—΄μ˜ 리슀트λ₯Ό μ œμ™Έν•˜κ³  λ‚˜λ¨Έμ§€ λ¬Έμžμ—΄λ“€μ˜ 각자리의 λ¬Έμžλ“€κ³Ό λΉ„κ΅ν•œλ‹€.

λ§Œμ•½ 제일 짧은 λ¬Έμžμ—΄μ˜ 인덱슀 0λΆ€ν„° iκΉŒμ§€μ˜ λ¬Έμžκ°€ λ‹€λ₯Έ λ¬Έμžμ—΄λ“€μ˜ 인덱슀 0λΆ€ν„° iκΉŒμ§€μ˜ λ¬Έμžμ™€ 같지 μ•Šλ‹€λ©΄, 이미 μ €μž₯λ˜μ–΄ 있던 ansλ₯Ό λ¬Έμžμ—΄λ‘œ λ°˜ν™˜ν•œλ‹€.


            ans = sList[0][:i+1]
        return "".join(ans)

제일 짧은 λ¬Έμžμ—΄μ˜ ν•΄λ‹Ή 인덱슀(i)μ—μ„œ λ‚˜λ¨Έμ§€ λ¬Έμžμ—΄λ“€μ„ λ‹€ λΉ„κ΅ν•˜μ˜€λ‹€λ©΄(= for j in sList[1:] κ°€ μ’…λ£Œλ˜μ—ˆλ‹€λ©΄) ans에 ν•΄λ‹Ή 접두사λ₯Ό μ €μž₯ν•œλ‹€.
그리고 μ ‘λ‘μ‚¬μ˜ 길이λ₯Ό ν•˜λ‚˜ 더 λŠ˜λ €μ„œ λ‹€μ‹œ 비ꡐ해쀀닀.

λ§ˆμ§€λ§‰μ—, ansλ₯Ό λ¬Έμžμ—΄λ‘œ λ°˜ν™˜ν•œλ‹€.


3. 전체 μ½”λ“œ

class Solution(object):
    def longestCommonPrefix(self, strs):
        ans = ""
        sList = [list(i) for i in strs]
        sList.sort(key=len)
        for i in range(len(sList[0])):
            for j in sList[1:]:
                if sList[0][:i+1] != j[:i+1]:
                    return "".join(ans)
            ans = sList[0][:i+1]
        return "".join(ans)

LeetCode 35번 μ½”λ“œ
class Solution(object):
    def searchInsert(self, nums, target):
        for i in range(len(nums)):
            if target <= nums[i]:
                return i
        return i+1

μ‰½κ²Œ ν’€μ–΄μ„œ μ½”λ“œλž‘ κ°„λž΅ν•œ ν’€μ΄λ§Œ λ‚¨κΈ°κ² μŠ΅λ‹ˆλ‹€!

nums[i]κ°€ target보닀 μ»€μ§€κ±°λ‚˜ κ°™μ•„μ§€λŠ” κ·Έ μˆœκ°„μ— iλ₯Ό λ°˜ν™˜ν•œλ‹€.
i+1을 λ°˜ν™˜ν•˜λŠ” κ²½μš°λŠ” λ¦¬μŠ€νŠΈμ—μ„œ 제일 큰 μˆ«μžκ°€ target보닀 μž‘μ€ κ²½μš°μ΄λ―€λ‘œ i+1을 λ°˜ν™˜ν•œλ‹€.


πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

35λ²ˆλΆ€ν„° ν’€κ³  λ„ˆλ¬΄ μ½”λ“œκ°€ κ°„λž΅ν•΄μ„œ λ‹€λ₯Έ λ¬Έμ œλ„ λ‹€μ‹œ ν’€μ–΄μ„œ 같이 μ˜¬λ¦½λ‹ˆλ‹€!
μ–΄μ œ μ˜¬λ Έμ–΄μ•Ό ν–ˆλŠ”λ° μ£„μ†‘ν•©λ‹ˆλ‹€..😭


Copy link
Collaborator

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

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

두 문제 μ˜¬λΌμ™”κΈΈλž˜ μ‹κ²ν–ˆλŠ”λ° λ‹€ν–‰νžˆ E-Zν•œ λ¬Έμ œλ“€... (●'β—‘'●)

Comment on lines +4 to +5
sList = [list(i) for i in strs]
sList.sort(key=len)
Copy link
Collaborator

Choose a reason for hiding this comment

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

λ¬Έμžμ—΄μ„ μ •λ ¬ν•˜λ©΄ "사전식 μˆœμ„œ"둜 μ •λ ¬λ˜μ£ ? 이게 λ‹¨μˆœνžˆ μ•ŒνŒŒλ²³ μˆœμ„œ μƒμœΌλ‘œ μ•žμ— μ˜€λŠ” 것 뿐만 μ•„λ‹ˆλΌ, 길이가 짧은 λ¬Έμžμ—΄μ΄ μ•žμ— μ˜΅λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄ "abcdefg", "abcd" κ°€ μžˆλ‹€λ©΄ ["abcd", "abcdefg"] 순으둜 μ •λ ¬λ©λ‹ˆλ‹€.

λ”°λΌμ„œ λ‹¨μˆœνžˆ strs.sort()만 μˆ˜ν–‰ν•΄λ„ λ©λ‹ˆλ‹€ :)

Comment on lines +6 to +11
for i in range(len(sList[0])):
for j in sList[1:]:
if sList[0][:i+1] != j[:i+1]:
return "".join(ans)
ans = sList[0][:i+1]
return "".join(ans)
Copy link
Collaborator

Choose a reason for hiding this comment

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

그리고 λ¬Έμžμ—΄λ“€μ„ 사전식 μˆœμ„œλ‘œ μ •λ ¬ν•˜κ²Œ 되면, κ°€μž₯ κΈ΄ 접두사(prefix)λ₯Ό μ°ΎλŠ” 것이기 λ•Œλ¬Έμ— λ”± 2가지 λ¬Έμžμ—΄λ§Œ ν™•μΈν•˜λ©΄ λ©λ‹ˆλ‹€. "맨 μ•ž" λ¬Έμžμ—΄, "맨 λ’€" λ¬Έμžμ—΄.

이 두 λ¬Έμžμ—΄μ˜ 문자λ₯Ό ν•˜λ‚˜μ”© ν™•μΈν•˜λ©΄μ„œ, 같은 문자면 덧뢙이고 λ‹€λ₯΄λ©΄ μ¦‰μ‹œ break μ‹œν‚€λ©΄ λ©λ‹ˆλ‹€ :)

class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        strs.sort()
        ans = ""
        for begin, end in zip(strs[0], strs[-1]):
            if begin != end:
                break
            ans += begin
        return ans

Copy link
Member Author

Choose a reason for hiding this comment

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

였였..! μƒκ°ν•΄λ³΄λ‹ˆ κ·Έλ ‡κ΅°μš”?! πŸ«ΆπŸ»πŸ‘πŸ»

Comment on lines +2 to +6
def searchInsert(self, nums, target):
for i in range(len(nums)):
if target <= nums[i]:
return i
return i+1
Copy link
Collaborator

Choose a reason for hiding this comment

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

λ¬Έμ œμ— $O(logN)$ μ•Œκ³ λ¦¬μ¦˜μœΌλ‘œ 풀어라 λΌμžˆλŠ”λ° μ™œ μ„ ν˜• νƒμƒ‰μœΌλ‘œ ν’€λ¦¬λŠ” κ±°μ£ ...? μ‹ κΈ°ν•˜λ„€μš” γ…‹γ…‹
μ•„λ§ˆ 원 μ˜λ„λŠ” 이진 탐색을 μ‚¬μš©ν•˜λΌλŠ” 것 κ°™μŠ΅λ‹ˆλ‹€ :)

class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        lo, hi = 0, len(nums) - 1

        while lo <= hi:
            mid = (lo + hi) // 2
            if target < nums[mid]:
                hi = mid - 1
            elif nums[mid] < target:
                lo = mid + 1
            else:
                return mid
                
        return lo

Copy link
Collaborator

Choose a reason for hiding this comment

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

You must write an algorithm with O(log n) runtime complexity.

λΌκ³ ν•˜λŠ”λ°.. μ„ ν˜•μœΌλ‘œ ν’€λ¦° μ΄μœ λŠ” ν•΄λ‹Ή λ¬Έμ œκ°€ λ‹€λ₯Έ λ¬Έμ œμ—μ„œλ„ μ‚¬μš©λ˜μ„œ κ·ΈλŸ°κ±°κ°™μŠ΅λ‹ˆλ‹€ γ…‹γ…‹γ…‹ γ… 
λ¦¬νŠΈμ½”λ“œμ—μ„œλŠ” 가끔 같은 λ¬Έμ œμ§€λ§Œ λ‹€λ₯΄κ²Œ 풀라고 μ œμ‹œλ˜λŠ” κ²½μš°κ°€ μžˆμ–΄μ„œ 그래용

κ΅ν™©λ‹˜ λ¬Έμ œν’€μ΄μ²˜λŸΌ μ΄λΆ„νƒμƒ‰μœΌλ‘œ ν’€μ–΄μ•Όν–ˆλ˜ λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€!

Copy link
Collaborator

@mjj111 mjj111 left a comment

Choose a reason for hiding this comment

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

κ³ μƒν•˜μ…¨μŠ΅λ‹ˆλ‹€!! πŸ‘

Comment on lines +2 to +6
def searchInsert(self, nums, target):
for i in range(len(nums)):
if target <= nums[i]:
return i
return i+1
Copy link
Collaborator

Choose a reason for hiding this comment

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

You must write an algorithm with O(log n) runtime complexity.

λΌκ³ ν•˜λŠ”λ°.. μ„ ν˜•μœΌλ‘œ ν’€λ¦° μ΄μœ λŠ” ν•΄λ‹Ή λ¬Έμ œκ°€ λ‹€λ₯Έ λ¬Έμ œμ—μ„œλ„ μ‚¬μš©λ˜μ„œ κ·ΈλŸ°κ±°κ°™μŠ΅λ‹ˆλ‹€ γ…‹γ…‹γ…‹ γ… 
λ¦¬νŠΈμ½”λ“œμ—μ„œλŠ” 가끔 같은 λ¬Έμ œμ§€λ§Œ λ‹€λ₯΄κ²Œ 풀라고 μ œμ‹œλ˜λŠ” κ²½μš°κ°€ μžˆμ–΄μ„œ 그래용

κ΅ν™©λ‹˜ λ¬Έμ œν’€μ΄μ²˜λŸΌ μ΄λΆ„νƒμƒ‰μœΌλ‘œ ν’€μ–΄μ•Όν–ˆλ˜ λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€!

Copy link
Member

@gjsk132 gjsk132 left a comment

Choose a reason for hiding this comment

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

생각보닀 κ°„λ‹¨ν•œ λ¬Έμ œμ˜€λŠ”λ°, μ œλŒ€λ‘œ 문제 μ•ˆ 읽고
제일 μ•ž(Prefix)κ°€ μ•„λ‹Œ 전체 μ€‘μ—μ„œ κ°€λŠ₯ν•œ κΈ΄ λ¬Έμžμ—΄μ„ μ°Ύκ³  μžˆμ—ˆλ„€μš”...γ…œγ… 

λ¬Έμ œλŠ” 꼼꼼히...!
그치만 μ˜μ–΄ μ‹€λ ₯이 λ”Έλ¦¬λ„€μš”...

def longestCommonPrefix(self, strs):
ans = ""
sList = [list(i) for i in strs]
sList.sort(key=len)
Copy link
Member

Choose a reason for hiding this comment

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

길이 μ •λ ¬ν•  λ•Œ 항상 sort(key=lambda x : len(x))둜 ν–ˆμ—ˆλŠ”λ° sort(key=len)으둜 λ°”λ‘œ λ˜λŠ”κ΅°μš”..!

μƒˆλ‘œμš΄ κ±° μ•Œμ•„κ°‘λ‹ˆλ‹€!

@9kyo-hwang 9kyo-hwang merged commit 12c4718 into AlgoLeadMe:main Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants