문제 : https://leetcode.com/problems/container-with-most-water/ 난이도 : Medium 풀이 class Solution: def maxArea(self, height: List[int]) -> int: l, r = 0, len(height) - 1 water = 0 while l < r: water = max(water, (r - l) * min(height[l], height[r])) # 저장할 수 있는 최대 물 if height[l] < height[r]: l += 1 else: r -= 1 return water 대학생 때 풀었던 거 같은데.. 오랜만에 보는 문제ㅎㅎ 왼쪽에서 움직이는 포인터(l)와 오른쪽에서 움직이는 포인터(r)를 두고 저장할 수 있는..
문제 : https://leetcode.com/problems/contains-duplicate-ii/description/ 난이도 : Easy 풀이 class Solution: def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: nDict = {} for i, n in enumerate(nums): if n in nDict and abs(i - nDict[n])
문제 : https://leetcode.com/problems/isomorphic-strings/description/ 난이도 : Easy 풀이 class Solution: def isIsomorphic(self, s: str, t: str) -> bool: sDict = [] tDict = [] for idx in s: sDict.append(s.index(idx)) for idx in t: tDict.append(t.index(idx)) if sDict == tDict: return True return False 처음 나타내는 문자의 인덱스를 찾아주는 index 함수를 사용해서 각 단어들의 인덱스를 저장해준다. 그리고 그 인덱스 사전들끼리 비교해주면 된다!
문제 : https://school.programmers.co.kr/learn/courses/30/lessons/12939 난이도 : Lv.2 풀이 def solution(s): newList = [] s = s.split(" ") for i in s: newList.append(int(i)) answer = " ".join([str(min(newList)), str(max(newList))]) return answer 그래도 레벨2 문제인데 아무래도 좀 더러운 것 같아서 줄여보고자 노력을 했다.. 하여.. 아래처럼 간단히 줄일 수 있다. 다른 풀이 def solution(s): s = list(map(int, s.split())) return str(min(s)) + " " + str(max(s))
문제 : https://school.programmers.co.kr/learn/courses/30/lessons/12930 난이도 : Lv.1 풀이 def solution(s): # '단어'기준 splitedWords = s.split(" ") # 공백 기준 분리 for i in range(len(splitedWords)): word = list(splitedWords[i]) # 글자 하나하나 분리 for k in range(len(word)): if k % 2 == 0: # 짝수번째 알파벳은 대문자 word[k] = word[k].upper() elif k % 2 == 1: # 홀수번째 알파벳은 소문자 word[k] = word[k].lower() splitedWords[i] = "".join(word..