Skip to content

Commit 3af0cab

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 639f73f + e36dda3 commit 3af0cab

File tree

6 files changed

+129
-0
lines changed

6 files changed

+129
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(1)
3+
4+
/**
5+
* @param {number[]} height
6+
* @return {number}
7+
*/
8+
var maxArea = function (height) {
9+
let maxWater = 0;
10+
let left = 0;
11+
let right = height.length - 1;
12+
13+
while (left < right) {
14+
const lowerHeight = Math.min(height[left], height[right]);
15+
const width = right - left;
16+
maxWater = Math.max(maxWater, lowerHeight * width);
17+
18+
if (height[left] < height[right]) {
19+
left++;
20+
} else {
21+
right--;
22+
}
23+
}
24+
25+
return maxWater;
26+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# O(1) spaces, O(N) times
2+
class Solution:
3+
def maxArea(self, height: List[int]) -> int:
4+
max_area = 0
5+
start, end = 0, len(height)-1
6+
7+
while start != end:
8+
# start, end 포인터에서 물의 넓이 계산 및 최대값 계산
9+
max_area = max((end-start)*min(height[start], height[end]), max_area)
10+
if height[start] < height[end]:
11+
start += 1
12+
elif height[start] >= height[end]:
13+
end -= 1
14+
15+
return max_area
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 공간복잡도 O(n): dictionary 멤버로 set을 사용
2+
# 시간복잡도 O(n*p): 삽입연산은 O(1)을 사용
3+
import re
4+
5+
class WordDictionary:
6+
7+
def __init__(self):
8+
self.dictionary = set()
9+
10+
def addWord(self, word: str) -> None:
11+
self.dictionary.add(word)
12+
13+
def search(self, word: str) -> bool:
14+
if '.' in word:
15+
pattern = re.compile(word)
16+
# O(n) times
17+
for item in self.dictionary:
18+
# O(p) times : 패턴의 길이(p)
19+
if pattern.fullmatch(item):
20+
return True
21+
return False
22+
else:
23+
return word in self.dictionary
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# 공간 복잡도: O(n) => nums 길이만큼 dp 배열 길이만큼의 공간 사용
2+
# 시간 복잡도: O(n^2) => 외부 반복문은 O(N), 내부 반복문은 O(N) 시간이 소요되므로 총 O(N*N) = O(N^2) 소요
3+
class Solution:
4+
def lengthOfLIS(self, nums: List[int]) -> int:
5+
dp = [1] * len(nums)
6+
7+
for cur in range(1, len(nums)):
8+
for prev in range(cur):
9+
if nums[cur] > nums[prev]:
10+
dp[cur] = max(dp[prev]+1, dp[cur])
11+
12+
return max(dp)

valid-parentheses/HerrineKim.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(n)
3+
4+
// 스택을 사용하여 괄호의 유효성을 검사
5+
// 괄호가 열리면 스택에 추가하고 닫히면 스택에서 마지막 요소를 꺼내서 짝이 맞는지 확인
6+
// 스택이 비어있으면 유효한 괄호 문자열
7+
8+
/**
9+
* @param {string} s
10+
* @return {boolean}
11+
*/
12+
var isValid = function (s) {
13+
const bracketStack = [];
14+
const bracketPairs = {
15+
')': '(',
16+
'}': '{',
17+
']': '['
18+
};
19+
20+
for (const char of s) {
21+
if (char in bracketPairs) {
22+
const lastChar = bracketStack.pop();
23+
24+
if (lastChar !== bracketPairs[char]) {
25+
return false;
26+
}
27+
} else {
28+
bracketStack.push(char);
29+
}
30+
}
31+
32+
return bracketStack.length === 0;
33+
};

valid-parentheses/jinah92.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# O(s) 공간 복잡도 : 입력문자열 s 길이에 따라 최대 s 깊이의 stack 생성
2+
# O(s) 시간 복잡도 : 문자열 s 길이에 따라 for문 반복
3+
class Solution:
4+
def isValid(self, s: str) -> bool:
5+
stack = []
6+
pairs = {'[': ']', '(': ')', '{': '}'}
7+
8+
for i, ch in enumerate(s):
9+
if ch == '(' or ch == '{' or ch == '[':
10+
stack.append(ch)
11+
else:
12+
# '(', '[', '{' 문자가 앞에 없이 ')', ']', '}' 문자가 오는 경우
13+
if not stack:
14+
return False
15+
lastCh = stack.pop()
16+
# pair가 맞지 않는 문자인 경우
17+
if pairs[lastCh] != ch:
18+
return False
19+
# stack에 값이 비지 않은 경우, pair가 맞지 않음
20+
return not stack

0 commit comments

Comments
 (0)