-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #786 from heypaprika/main
[croucs] Week 03
- Loading branch information
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# μ΄λ ΅λ€μγ λ³΄κ³ νμμ΅λλ€ | ||
|
||
class Solution: | ||
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
ans = [] | ||
def func(cur_remain, arr, idx): | ||
if cur_remain == 0: | ||
ans.append(list(arr)) | ||
return | ||
elif cur_remain < 0: | ||
return | ||
|
||
for i in range(idx, len(candidates)): | ||
arr.append(candidates[i]) | ||
func(cur_remain - candidates[i], arr, i) | ||
arr.pop() | ||
|
||
func(target, [], 0) | ||
return ans | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
""" | ||
볡μ‘λ : μμ -> μμν μ΄μ | ||
μκ° λ³΅μ‘λ : O(n) -> len(nums) λ§νΌ λ°λ³΅ | ||
κ³΅κ° λ³΅μ‘λ : O(n) -> len(nums) ν¬κΈ°μ λ°°μ΄ a μμ± | ||
""" | ||
class Solution: | ||
def maxSubArray(self, nums: List[int]) -> int: | ||
a = [0] * len(nums) | ||
a[0] = nums[0] | ||
for i in range(1, len(nums)): | ||
a[i] = max(nums[i], nums[i]+a[i-1]) | ||
return max(a) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
""" | ||
볡μ‘λ : μμ -> μμν μ΄μ | ||
μκ° λ³΅μ‘λ : O(n) -> for λ¬Έμ΄ μ¬λ¬λ² μμ§λ§, len(nums)λ§νΌ μ¬λ¬λ² λ°λ³΅νλ―λ‘ O(n) | ||
κ³΅κ° λ³΅μ‘λ : O(n) -> len(nums)λ§νΌμ λ°°μ΄ νλκ° λ μκΈ°λ―λ‘ | ||
""" | ||
class Solution: | ||
def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
zeros = 0 | ||
products = 1 | ||
ans_list = [0] * len(nums) | ||
|
||
for i in range(len(nums)): | ||
if nums[i] == 0: | ||
zeros += 1 | ||
products *= nums[i] | ||
|
||
if zeros == 1: | ||
products = 1 | ||
alived_i = -1 | ||
for i in range(len(nums)): | ||
if nums[i] == 0: | ||
alived_i = i | ||
continue | ||
products *= nums[i] | ||
ans_list[alived_i] = products | ||
return ans_list | ||
elif zeros >= 2: | ||
return ans_list | ||
|
||
ans_list = [products] * len(nums) | ||
for i in range(len(nums)): | ||
ans_list[i] //= nums[i] | ||
|
||
return ans_list | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
""" | ||
볡μ‘λ : μμ -> μμν μ΄μ | ||
μκ° λ³΅μ‘λ : O(1) -> μ΄λ€ μκ° λ€μ΄μ€λλΌλ μμλ§νΌ μ°μ° | ||
κ³΅κ° λ³΅μ‘λ : O(1) -> μ΄λ€ μκ° λ€μ΄μ€λλΌλ μμλ§νΌ ν λΉ | ||
""" | ||
class Solution: | ||
def reverseBits(self, n: int) -> int: | ||
ans = 0 | ||
for i in range(32): | ||
if n % 2 == 1: | ||
ans += 2**(31-i) | ||
n = n // 2 | ||
return ans | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
""" | ||
볡μ‘λ : μμ -> μμν μ΄μ | ||
μκ° λ³΅μ‘λ : O(n) -> λμ λ리 ν€λ‘ κ²μνλ κ²μ O(1), λ°λΌμ for λ¬Έ 1κ°λ‘ O(n) | ||
κ³΅κ° λ³΅μ‘λ : O(n) -> n μ λ§νΌ λ°λ³΅λλ©΄μ κ°μ΄ ν λΉλ¨. | ||
""" | ||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
num_dict = {} | ||
n = len(nums) | ||
|
||
for i in range(n): | ||
num_A = nums[i] | ||
num_B = target - num_A | ||
if num_B in num_dict: | ||
return [i, num_dict[num_B]] | ||
num_dict[num_A] = i | ||
return [] | ||
|