This file contains hidden or 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
def solution(n,a,b): | |
# a < b 이도록 변환. | |
a, b = min(a,b), max(a,b) | |
count = 1 | |
# b-a == 1 이면 a와 b가 만나는 시점. | |
# 더 큰 값인 b가 홀수면, b는 a가 아니라 b 오른쪽 사람과 겨뤄야 한다. | |
# ex) a, b = 2, 3일 경우 | |
# 1-2, 3-4 형태로 먼저 대전한 다음에야 만날 수 있음. | |
while b - a != 1 or b % 2 != 0: | |
o_a, r_a = divmod(a, 2) |
This file contains hidden or 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
class Solution: | |
def lengthOfLongestSubstring(self, s: str) -> int: | |
# 부분 문자열. 연속된 문자열이어야 한다. | |
# 슬라이딩 윈도우 + 투 포인터로 사이즈 조절하기 | |
start, end, max_len = 0, 0, 0 | |
# 이미 존재하는 String -> index 기록 | |
exists = dict() | |
for idx, char in enumerate(s): | |
# 이미 있는 문자열이고, 현재 윈도우 시작위치가 해당 문자열 등장위치보다 이전일 경우 | |
# window의 시작지점을 해당 문자열 위치 다음으로 옮긴다. |
This file contains hidden or 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
class Solution: | |
def dailyTemperatures(self, T: List[int]) -> List[int]: | |
# 현재 온도를 stack에 저장. | |
# 다음날 온도가 현재보다 높으면 stack 값을 꺼내서, 날씨차이 저장. | |
# 낮으면 스택 값은 계속 유지됨. | |
stack = [] | |
answer = [0 for _ in range(len(T))] | |
for idx, value in enumerate(T): | |
while stack and value > T[stack[-1]]: | |
last = stack.pop() |
This file contains hidden or 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
# Definition for singly-linked list. | |
class ListNode: | |
def __init__(self, val=0, next=None): | |
self.val = val | |
self.next = next | |
from collections import deque | |
class Solution: | |
def isPalindrome(self, head: ListNode) -> bool: | |
# Runner 활용한 풀이 | |
# 두칸씩 이동하는 fast, 한칸씩 이동하는 slow를 정의하면 |
This file contains hidden or 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
# Definition for singly-linked list. | |
# class ListNode: | |
# def __init__(self, val=0, next=None): | |
# self.val = val | |
# self.next = next | |
from collections import deque | |
class Solution: | |
def isPalindrome(self, head: ListNode) -> bool: | |
if not head: return True | |
# deque를 활용한 풀이 |
This file contains hidden or 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
import math | |
class Solution: | |
def maxProfit(self, prices: List[int]) -> int: | |
# 최솟값을 매번 갱신하면서 | |
# 현재 값과 최솟값과의 차이를 구해 최댓값을 저장하도로 한다 | |
# 카데인(Kadane) 알고리즘으로, O(n) 에 풀이 가능 | |
profit = 0 | |
min_price = math.inf | |
for price in prices: | |
min_price = min(price, min_price) |
This file contains hidden or 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
class Solution: | |
def productExceptSelf(self, nums: List[int]) -> List[int]: | |
# 나눗셈 연산 없이, O(n)의 시간복잡도로 풀어야 한다 | |
# = "자기 자신을 제외한 왼쪽의 곱셈 결과 * 오른쪽 곱셈 결과" 형태로 값을 구해야 한다. | |
# [1,2,3,4] 예시 | |
# 1. 왼쪽 곱셈 결과값 구하기. | |
# 왼쪽 첫 번째 숫자는 곱한 값이 없어야 하므로 시작값을 1로 초기화한다. | |
n = 1 | |
left, right = [], [] |
This file contains hidden or 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
def solution(arr): | |
def check(y, x, n): | |
# 타일 1개에 도달한 경우 | |
# 타일 값이 0이면 [1,0], 값이 1이면 [0,1]을 리턴한다. | |
if n == 1: | |
return [0, 1] if arr[y][x] == 1 else [1,0] | |
# 전체 사각형의 왼쪽 위, 오른쪽 위, 왼쪽 아래, 오른쪽 아래 순으로 확인한다. | |
left_up = check(y, x, n // 2) | |
right_up = check(y, x + n//2, n//2) |
This file contains hidden or 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
def solution(n): | |
answer = [] | |
while True: | |
n, rest = divmod(n, 3) | |
answer.append(rest) | |
if n == 0: | |
break | |
return sum([i * 3**idx for idx, i in enumerate(reversed(answer))]) |
This file contains hidden or 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
class Solution: | |
def threeSum(self, nums: List[int]) -> List[List[int]]: | |
answer = [] | |
nums.sort() | |
# 가장 작은 값을 고정하고, 중간값과 큰 값을 투 포인터로 계산한다. | |
# 고정값 index = start. | |
for start in range(len(nums) - 2): | |
# 한 번 고정값으로 사용했다면 건너뛴다. | |
if start > 0 and nums[start] == nums[start - 1]: |