WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 94b7f3c

Browse files
authored
Merge pull request #2189 from ZetBe/main
[ZetBe] WEEK 06 solutions
2 parents 710188c + ee68484 commit 94b7f3c

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'''
2+
๋ฌธ์ œ: ์ปจํ…Œ์ด๋„ˆ ์ตœ๋Œ€ ๋ฌผ ๋‹ด๊ธฐ
3+
ํ’€์ด: ์–‘์ชฝ ๋์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ๋” ์ž‘์€ ์ชฝ์˜ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™์‹œํ‚ค๋ฉฐ ์ตœ๋Œ€ ๋„“์ด๋ฅผ ๊ณ„์‚ฐ
4+
์‹œ๊ฐ„๋ณต์žก๋„: O(n)
5+
๊ณต๊ฐ„๋ณต์žก๋„: O(1)
6+
์‚ฌ์šฉํ•œ ์ž๋ฃŒ๊ตฌ์กฐ: ํˆฌ ํฌ์ธํ„ฐ
7+
'''
8+
9+
10+
class Solution:
11+
def maxArea(self, height: List[int]) -> int:
12+
r, l = len(height)-1, 0
13+
answer = (r-l)*(min(height[r], height[l]))
14+
while r > l:
15+
answer = max(answer, (r-l)*(min(height[r], height[l])))
16+
if height[r] < height[l]:
17+
r -= 1
18+
else:
19+
l += 1
20+
return answer
21+
22+
23+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'''
2+
๋ฌธ์ œ: ์ตœ์žฅ ์ฆ๊ฐ€ ๋ถ€๋ถ„ ์ˆ˜์—ด
3+
ํ’€์ด: ์ด๋ถ„ ํƒ์ƒ‰์„ ํ™œ์šฉํ•˜์—ฌ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆ˜์—ด์„ ์œ ์ง€ํ•˜๋ฉฐ ๊ธธ์ด๋ฅผ ๊ณ„์‚ฐ, ์ƒˆ๋กœ์šด ์ˆ˜๊ฐ€ ๊ธฐ์กด ์ˆ˜์—ด์˜ ๋งˆ์ง€๋ง‰ ์ˆ˜๋ณด๋‹ค ํฌ๋ฉด ์ถ”๊ฐ€, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ด๋ถ„ ํƒ์ƒ‰์œผ๋กœ ์ ์ ˆํ•œ ์œ„์น˜๋ฅผ ์ฐพ์•„ ๊ต์ฒด
4+
์‹œ๊ฐ„๋ณต์žก๋„: O(n log n)
5+
๊ณต๊ฐ„๋ณต์žก๋„: O(n)
6+
์‚ฌ์šฉํ•œ ์ž๋ฃŒ๊ตฌ์กฐ: ๋ฆฌ์ŠคํŠธ
7+
'''
8+
9+
10+
from bisect import bisect_left
11+
12+
class Solution:
13+
def lengthOfLIS(self, nums: List[int]) -> int:
14+
answer = [nums[0]]
15+
for i in range(1, len(nums)):
16+
if answer[-1] < nums[i]:
17+
answer.append(nums[i])
18+
else:
19+
now = bisect_left(answer, nums[i])
20+
answer[now] = nums[i]
21+
22+
return len(answer)
23+
24+
25+

โ€Žspiral-matrix/ZetBe.pyโ€Ž

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'''
2+
๋ฌธ์ œ: ๋‚˜์„ ํ˜•์œผ๋กœ ํ–‰๋ ฌ์„ ์ˆœํšŒํ•˜๋ฉฐ ๋ชจ๋“  ์›์†Œ๋ฅผ ๋ฐ˜ํ™˜
3+
ํ’€์ด: ๋ฐฉ๋ฌธํ•œ ์œ„์น˜๋ฅผ ๊ธฐ๋กํ•˜๋Š” ํ–‰๋ ฌ์„ ๋งŒ๋“ค์–ด ํ˜„์žฌ ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ฉฐ ์ด๋™, ์ด๋™ํ•  ์ˆ˜ ์—†์œผ๋ฉด ๋ฐฉํ–ฅ์„ ๋ฐ”๊ฟˆ
4+
์‹œ๊ฐ„๋ณต์žก๋„: O(m*n) (m: ํ–‰์˜ ์ˆ˜, n: ์—ด์˜ ์ˆ˜)
5+
๊ณต๊ฐ„๋ณต์žก๋„: O(m*n) (๋ฐฉ๋ฌธ ๊ธฐ๋ก์„ ์œ„ํ•œ ํ–‰๋ ฌ)
6+
์‚ฌ์šฉํ•œ ์ž๋ฃŒ๊ตฌ์กฐ: ๋ฆฌ์ŠคํŠธ
7+
๊ฐœ์ธ์ ์ธ ํšŒ๊ณ : ๋ฌธ์ œ ์ž์ฒด๋Š” ๋‹จ์ˆœํ•˜์ง€๋งŒ ๊ฒฝ๊ณ„ ์กฐ๊ฑด๊ณผ ๋ฐฉํ–ฅ ์ „ํ™˜ ๋กœ์ง์„ ๊ผผ๊ผผํžˆ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•ด์„œ ๊ตฌํ˜„์— ์‹ ๊ฒฝ์„ ์จ์•ผ ํ–ˆ๋‹ค. ๋ฐฉ๋ฌธ ๊ธฐ๋ก ํ–‰๋ ฌ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ์œ„์น˜๋ฅผ ํ”ผํ•˜๋Š” ๋ฐฉ์‹์ด ์ง๊ด€์ ์ด๊ณ  ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์› ๋‹ค.
8+
'''
9+
10+
class Solution:
11+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
12+
v = [[0 for i in range(len(matrix[0]))] for j in range(len(matrix))]
13+
if len(v) == 1:
14+
return [i for i in matrix[0]]
15+
if len(v[0]) == 1:
16+
answer = []
17+
for i in matrix:
18+
for j in i:
19+
answer.append(j)
20+
return answer
21+
answer = []
22+
d = [[0, 1], [1, 0], [0, -1], [-1, 0]]
23+
i = 0
24+
y, x = 0, 0
25+
while True:
26+
if len(answer) == len(v)*len(v[0]):
27+
break
28+
v[y][x] = 1
29+
answer.append(matrix[y][x])
30+
ny = y + d[i][0]
31+
nx = x + d[i][1]
32+
if not(0 <= nx < len(v[0]) and 0 <= ny < len(v) and v[ny][nx] == 0):
33+
i = (i+1)%4
34+
ny, nx = y + d[i][0], x + d[i][1]
35+
y, x = ny, nx
36+
37+
38+
return answer
39+
40+

โ€Žvalid-parentheses/ZetBe.pyโ€Ž

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
๋ฌธ์ œ: ๊ด„ํ˜ธ ์Œ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ(์„œ๋กœ ์—‡๊ฐˆ๋ ค์„œ ์ง์ด ๋งž์œผ๋ฉด ๋ฌดํšจํ•จ)
3+
ํ’€์ด: ์Šคํƒ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์—ฌ๋Š” ๊ด„ํ˜ธ๋ฅผ ๋งŒ๋‚˜๋ฉด ์Šคํƒ์— ์ถ”๊ฐ€, ๋‹ซ๋Š” ๊ด„ํ˜ธ๋ฅผ ๋งŒ๋‚˜๋ฉด ์Šคํƒ์˜ ์ตœ์ƒ๋‹จ ์š”์†Œ์™€ ๋น„๊ตํ•˜์—ฌ ์ง์ด ๋งž์œผ๋ฉด pop, ์•„๋‹ˆ๋ฉด False ๋ฐ˜ํ™˜
4+
์‹œ๊ฐ„๋ณต์žก๋„: O(n)
5+
๊ณต๊ฐ„๋ณต์žก๋„: O(n)
6+
์‚ฌ์šฉํ•œ ์ž๋ฃŒ๊ตฌ์กฐ: ์Šคํƒ(๋ฆฌ์ŠคํŠธ)
7+
'''
8+
9+
10+
class Solution:
11+
def isValid(self, s: str) -> bool:
12+
stack = []
13+
14+
for i in s:
15+
if len(stack) == 0 and (i == '}' or i == ')' or i == ']'):
16+
return False
17+
if i == '[' or i == '(' or i == '{':
18+
stack.append(i)
19+
if i == '}' or i == ')' or i == ']':
20+
if i == '}' and stack[len(stack)-1] == '{':
21+
stack.pop()
22+
elif i == ')' and stack[len(stack)-1] == '(':
23+
stack.pop()
24+
elif i == ']' and stack[len(stack)-1] == '[':
25+
stack.pop()
26+
else:
27+
return False
28+
if len(stack) == 0:
29+
return True
30+
return False
31+
32+
33+

0 commit comments

Comments
ย (0)