-
Notifications
You must be signed in to change notification settings - Fork 2
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
50-xxubin04 #191
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ λ¬Έμ μ¬λΌμκΈΈλ μκ²νλλ° λ€νν E-Zν λ¬Έμ λ€... (β'β‘'β)
sList = [list(i) for i in strs] | ||
sList.sort(key=len) |
There was a problem hiding this comment.
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()
λ§ μνν΄λ λ©λλ€ :)
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ€μ€..! μκ°ν΄λ³΄λ κ·Έλ κ΅°μ?! π«Άπ»ππ»
def searchInsert(self, nums, target): | ||
for i in range(len(nums)): | ||
if target <= nums[i]: | ||
return i | ||
return i+1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ¬Έμ μ
μλ§ μ μλλ μ΄μ§ νμμ μ¬μ©νλΌλ κ² κ°μ΅λλ€ :)
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
There was a problem hiding this comment.
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.
λΌκ³ νλλ°.. μ νμΌλ‘ νλ¦° μ΄μ λ ν΄λΉ λ¬Έμ κ° λ€λ₯Έ λ¬Έμ μμλ μ¬μ©λμ κ·Έλ°κ±°κ°μ΅λλ€ γ
γ
γ
γ
리νΈμ½λμμλ κ°λ κ°μ λ¬Έμ μ§λ§ λ€λ₯΄κ² νλΌκ³ μ μλλ κ²½μ°κ° μμ΄μ κ·Έλμ©
κ΅ν©λ λ¬Έμ νμ΄μ²λΌ μ΄λΆνμμΌλ‘ νμ΄μΌνλ λ¬Έμ μμ΅λλ€!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
κ³ μνμ ¨μ΅λλ€!! π
def searchInsert(self, nums, target): | ||
for i in range(len(nums)): | ||
if target <= nums[i]: | ||
return i | ||
return i+1 |
There was a problem hiding this comment.
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.
λΌκ³ νλλ°.. μ νμΌλ‘ νλ¦° μ΄μ λ ν΄λΉ λ¬Έμ κ° λ€λ₯Έ λ¬Έμ μμλ μ¬μ©λμ κ·Έλ°κ±°κ°μ΅λλ€ γ
γ
γ
γ
리νΈμ½λμμλ κ°λ κ°μ λ¬Έμ μ§λ§ λ€λ₯΄κ² νλΌκ³ μ μλλ κ²½μ°κ° μμ΄μ κ·Έλμ©
κ΅ν©λ λ¬Έμ νμ΄μ²λΌ μ΄λΆνμμΌλ‘ νμ΄μΌνλ λ¬Έμ μμ΅λλ€!
There was a problem hiding this 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) |
There was a problem hiding this comment.
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)
μΌλ‘ λ°λ‘ λλκ΅°μ..!
μλ‘μ΄ κ±° μμκ°λλ€!
π λ¬Έμ λ§ν¬
μκ°λ³΄λ€ μ½κ² ꡬνν΄μ λ λ¬Έμ νμμ΅λλ€..!
LeetCode 14: Longest Common Prefix
LeetCode 35: Search Insert Position
βοΈ μμλ μκ°
20λΆ
β¨ μλ μ½λ
λ¬Έμ μ΄ν΄ - 14
strsλ‘ μ£Όμ΄μ§λ λ¬Έμμ΄λ€μμ λͺ¨λ λμΌνκ² κ°μ§λ μ΅λ κΈΈμ΄μ μ λμ¬λ₯Ό μΆλ ₯νλ λ¬Έμ μ΄λ€.
κ·Έλ¬ν 쑰건μ λ§μ‘±νλ μ λμ¬κ° μλ€λ©΄
""
μ μΆλ ₯νλ€.2. μ½λ λΆμ
μ£Όμ΄μ§λ λ¬Έμμ΄μ 리μ€νΈμΈ strsλ₯Ό ν κΈμμ© λΆν΄νμ¬ 2μ°¨μ 리μ€νΈ sListλ‘ λ§λ λ€.
ex) sList = [[f, l, o, w, e, r], [f, l, o, w], [f, l, i, g, h, t]]
sListλ₯Ό 리μ€νΈ κΈΈμ΄λ₯Ό κΈ°μ€μΌλ‘ μ€λ¦μ°¨μμΌλ‘ μ λ ¬νλ€.
sList[0]μ κΈΈμ΄ = μ λμ¬μ μ΅λ κΈΈμ΄ μ΄λ―λ‘
len(sList[0])
λ§νΌ μννλ©°,sListμμ μ μΌ μ§§μ λ¬Έμμ΄μ 리μ€νΈλ₯Ό μ μΈνκ³ λλ¨Έμ§ λ¬Έμμ΄λ€μ κ°μ리μ λ¬Έμλ€κ³Ό λΉκ΅νλ€.
λ§μ½ μ μΌ μ§§μ λ¬Έμμ΄μ μΈλ±μ€ 0λΆν° iκΉμ§μ λ¬Έμκ° λ€λ₯Έ λ¬Έμμ΄λ€μ μΈλ±μ€ 0λΆν° iκΉμ§μ λ¬Έμμ κ°μ§ μλ€λ©΄, μ΄λ―Έ μ μ₯λμ΄ μλ ansλ₯Ό λ¬Έμμ΄λ‘ λ°ννλ€.
μ μΌ μ§§μ λ¬Έμμ΄μ ν΄λΉ μΈλ±μ€(i)μμ λλ¨Έμ§ λ¬Έμμ΄λ€μ λ€ λΉκ΅νμλ€λ©΄(=
for j in sList[1:]
κ° μ’ λ£λμλ€λ©΄) ansμ ν΄λΉ μ λμ¬λ₯Ό μ μ₯νλ€.κ·Έλ¦¬κ³ μ λμ¬μ κΈΈμ΄λ₯Ό νλ λ λλ €μ λ€μ λΉκ΅ν΄μ€λ€.
λ§μ§λ§μ, ansλ₯Ό λ¬Έμμ΄λ‘ λ°ννλ€.
3. μ 체 μ½λ
LeetCode 35λ² μ½λ
μ½κ² νμ΄μ μ½λλ κ°λ΅ν νμ΄λ§ λ¨κΈ°κ² μ΅λλ€!
nums[i]κ° targetλ³΄λ€ μ»€μ§κ±°λ κ°μμ§λ κ·Έ μκ°μ iλ₯Ό λ°ννλ€.
i+1μ λ°ννλ κ²½μ°λ 리μ€νΈμμ μ μΌ ν° μ«μκ° targetλ³΄λ€ μμ κ²½μ°μ΄λ―λ‘ i+1μ λ°ννλ€.
π μλ‘κ² μκ²λ λ΄μ©
35λ²λΆν° νκ³ λ무 μ½λκ° κ°λ΅ν΄μ λ€λ₯Έ λ¬Έμ λ λ€μ νμ΄μ κ°μ΄ μ¬λ¦½λλ€!
μ΄μ μ¬λ Έμ΄μΌ νλλ° μ£μ‘ν©λλ€..π