Skip to content

Commit

Permalink
Array
Browse files Browse the repository at this point in the history
  • Loading branch information
uniqueimaginate committed Feb 23, 2021
1 parent 248c757 commit 05e5eb7
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Coding Test/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
num_map = {}
for i, num in enumerate(nums):
if target - num in num_map and i != num_map[target-num]:
return i, num_map[target-num]
num_map[num] = i;



10 changes: 10 additions & 0 deletions Coding Test/121.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit = 0
min_price = sys.maxsize

for price in prices:
min_price = min(min_price, price)
profit = max(price-min_price, profit)

return profit
28 changes: 28 additions & 0 deletions Coding Test/15.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
result = []
nums.sort()


for i in range(len(nums) - 2):
if i > 0 and nums[i] == nums[i-1]:
continue

left, right = i + 1, len(nums) - 1
while left < right:
three_sum = nums[i] + nums[left] + nums[right]

if three_sum < 0:
left += 1
elif three_sum > 0:
right -= 1
else:
result.append([nums[i], nums[left], nums[right]])

while left < right and nums[left] == nums[left + 1]:
left += 1
while left < right and nums[right] == nums[right - 1]:
right -= 1
left += 1
right -= 1
return result
32 changes: 32 additions & 0 deletions Coding Test/238.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
a = [1]
b = [1]
res = []
for i in range(len(nums) - 1):
a.append(a[i] * nums[i])
b.append(b[i] * nums[-i-1])

b.reverse()

for i in range(len(nums)):
res.append(a[i] * b[i])

return res


class Solution2:
def productExceptSelf(self, nums: List[int]) -> List[int]:
out = []
p = 1

for i in range(0, len(nums)):
out.append(p)
p = p * nums[i]
p = 1

for i in range(len(nums)-1, 0 - 1, -1):
out[i] = out[i] * p
p = p * nums[i]

return out
45 changes: 45 additions & 0 deletions Coding Test/42.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Stack
class Solution:
def trap(self, height: List[int]) -> int:
stck = []
result = 0

for i in range(len(height)):
while stck and height[i] > height[stck[-1]]:

top = stck.pop()

if not len(stck):
break

dist = i - stck[-1] - 1
water = min(height[i], height[stck[-1]]) - height[top]

result += dist * water

stck.append(i)

return result

# Two pointers
class Solution2:
def trap(self, height: List[int]) -> int:
if not height:
return 0

volume = 0
left, right = 0, len(height) - 1
left_max, right_max = height[left], height[right]


while left < right:
left_max, right_max = max(height[left], left_max), max(height[right], right_max)

if left_max <= right_max:
volume += left_max - height[left]
left += 1
else:
volume += right_max - height[right]
right -= 1
return volume

15 changes: 15 additions & 0 deletions Coding Test/561.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# normal
class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
result = 0
nums.sort()

for i in range(0, len(nums)-1, 2):
result += nums[i]

return result

# Pythonic
class Solution2:
def arrayPairSum(self, nums: List[int]) -> int:
return sum(sorted(nums)[::2])
3 changes: 3 additions & 0 deletions Coding Test/819.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import re
import collections

class Solution:
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
words = [word for word in re.sub('[^\w]', ' ', paragraph).lower().split() if word not in banned]
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# 자료구조 및 알고리즘 복습

<b>취업을 준비함에 있어 자료구조와 알고리즘 공부는 필수 입니다.</b>
**취업을 준비함에 있어 자료구조와 알고리즘 공부는 필수 입니다.**

필자는 아래의 주제에 맞춰 C++ 또는 Python 으로 각각을 구현하고자 합니다.

모든 문제는 Leetcode 에 있습니다.

숫자는 Leetcode 문제 번호 입니다.

## 준비운동

파이썬 문자열 조작
Expand All @@ -15,7 +17,7 @@
## 자료구조

1. Array
>
> 1, 42, 15, 561, 238, 121
2. Stack
3. Queue
4. Deque
Expand Down

0 comments on commit 05e5eb7

Please sign in to comment.