본문 바로가기

Algorithm101

[이진 검색] 이진 검색 704 | Binary Search https://leetcode.com/problems/binary-search/ 이상 풀이 재귀 풀이 class Solution: def search(self, nums: List[int], target: int) -> int: def binary_search(left, right): if left target: return binary_search(left, mid - 1) else: return mid else: return -1 return binary_search(0, len(nums) - 1) 정렬된 nums 를 입력받기 때문에 따로 정렬이 필요 없음 index 값을 return 하기 때문에 mid return 반복 풀이 class Solution: def s.. 2022. 2. 7.
[BOJ] 17298 | 오큰수 17298 | 오큰수 https://www.acmicpc.net/problem/17298 작성 코드 def solution(n: int, a: str) -> str: nums = list(map(int, a.split())) answer = [] for idx, num in enumerate(nums): if idx != (len(nums)-1) and max(nums[idx + 1:]) 시간 초과 숫자를 한 칸씩 띄어 쓴 형태로 출력해야 돼서 join을 사용하려고 했는데 str 형식에만 적용 가능하다고 해서 일일이 str 형식으로 바꾸어주었다 이상 코드 def solution(n: int, a: str) -> List[int]: nums = list(map(int, a.split())) answer =.. 2022. 2. 4.
[LeetCode] 13 | Roman to Integer 13 | Roman to Integer https://leetcode.com/problems/roman-to-integer/ 작성 코드 class Solution: def romanToInt(self, s: str) -> int: sum = 0 s = list(s) roman_dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000} while s: if len(s) > 1 and roman_dict[s[-1]] > roman_dict[s[-2]]: sum += roman_dict[s.pop()] - roman_dict[s.pop()] else: sum += roman_dict[s.pop()] return sum 2022. 1. 28.
[BOJ] 12871 | 무한 문자열 12871 | 무한 문자열 https://www.acmicpc.net/problem/12871 작성 코드 def gcd(a: int, b: int) -> int : while b != 0: a, b = b, a % b return a def lcm(a: int, b: int) -> int : return a * b // gcd(a, b) a = input() b = input() # 최소 공배수 lcm = lcm(len(a), len(b)) if a*(lcm//len(a)) == b*(lcm//len(b)): print(1) else: print(0) 이상 코드 a, b = input(), input() print(int(a*len(b) == b*len(a))) 문자열 * 숫자 = 문자열을 숫자만큼 반복.. 2022. 1. 27.
[BOJ][#] 1002 | 터렛 1002 | 터렛 https://www.acmicpc.net/problem/1002 이상 코드 import math n = int(input()) for _ in range(n): x1, y1, r1, x2, y2, r2 = map(int, input().split()) distance = math.sqrt((x1-x2)**2 + (y1-y2)**2) # The distance of two circles if distance == 0 and r1 == r2 : # two circles are concentric and have the same radius print(-1) elif abs(r1-r2) == distance or r1 + r2 == distance: # internal or external.. 2022. 1. 25.
[BOJ][#] 1920 | 수 찾기 1920 | 수 찾기 https://www.acmicpc.net/problem/1920 작성 코드 import sys N = int(sys.stdin.readline()) nums = list(map(int, sys.stdin.readline().split())) M = int(sys.stdin.readline()) M_nums = list(map(int, sys.stdin.readline().split())) for M_num in M_nums: if M_num in nums: print('1') else: print('0') 시간 초과 이상 코드 set 사용 import sys N = int(sys.stdin.readline()) nums = set(map(int, sys.stdin.readline(.. 2022. 1. 24.
반응형