코-딩

코-딩/Leetcode

[Leetcode] 11. Container With Most Water

문제 : 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)를 두고 저장할 수 있는..

코-딩/Leetcode

[Leetcode] 219. Contains Duplicate II

문제 : 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])

코-딩/Leetcode

[leetcode] 205. Isomorphic Strings

문제 : 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/12946 난이도 : Lv.2 풀이 def solution(n): return hanoi(1, 2, 3, [], n) def hanoi(src, tran, dst, trace, n): if n == 1: trace.append([src, dst]) return hanoi(src, dst, tran, answer, n - 1) trace.append([src, dst]) hanoi(tran, src, dst, answer, n - 1) return trace 재귀 문제에서 자주 나오는 문제이다. 간단하게 생각해서 1) n-1개의 원판을 비어있는 경유지에 옮긴다. (src -> tran) 2) 남..

코-딩/프로그래머스

프로그래머스::최댓값과 최솟값

문제 : 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..

힞뚜루마뚜루
'코-딩' 카테고리의 글 목록 (2 Page)