diff --git a/longest-substring-without-repeating-characters/radiantchoi.py b/longest-substring-without-repeating-characters/radiantchoi.py new file mode 100644 index 000000000..15973dfd0 --- /dev/null +++ b/longest-substring-without-repeating-characters/radiantchoi.py @@ -0,0 +1,30 @@ +class Solution: + def lengthOfLongestSubstring(self, s: str) -> int: + # 문자열 전체를 순회, 서브스트링이란 서로 붙어 있는 문자열 + # 글자를 볼 때 현재 모아둔 글자 모음에 중복 글자가 있다면? + # 일단 지금까지의 길이 업데이트 + # 중복된 글자가 등장한 인덱스 체크 + # 현재 모아둔 문자열의 길이에서 해당 인덱스까지의 "머리"를 덜어내고 지금 보는 문자를 더해서, 서브스트링을 이어나감 + + current = {} + tuning = 0 + result = 0 + + for (index, letter) in enumerate(s): + if current.get(letter, index) < index: + result = max(result, len(current)) + + tail_of_head = current[letter] + + new_current = {} + for key in current: + if current[key] > tail_of_head: + new_current[key] = current[key] + + current = new_current + + current[letter] = index + + result = max(result, len(current)) + + return result diff --git a/number-of-islands/radiantchoi.swift b/number-of-islands/radiantchoi.swift new file mode 100644 index 000000000..5d06e1752 --- /dev/null +++ b/number-of-islands/radiantchoi.swift @@ -0,0 +1,40 @@ +class Solution { + func numIslands(_ grid: [[Character]]) -> Int { + var grid = grid + var result = 0 + + for i in 0.. int: + # 도착을 위해 실제로 움직여야 하는 칸은 아래로 (m - 1)칸, 오른쪽으로 (n - 1)칸 + # 총 (m + 1) + (n + 1)칸을 움직여야 하는데, 이만큼의 "빈 바구니"가 있다고 가정 + # 빈 바구니의 몇 번 박스에 오른쪽으로 가기/아래로 가기를 넣을 것인가? 의 문제 + # 조합을 통해 둘 중 아무거나를 바구니에 넣는 경우의 수를 구하면, 나머지 칸에 나머지 하나를 다 넣을 수 있다. + # 따라서 math.comb(m + n - 2, n - 1)도 동일한 결과 반환 + return math.comb(m + n - 2, m - 1)