Home
icyou
Cancel

N-ary Tree Preorder Traversal

Leetcode Problem N-ary Tree Preorder Traversal tree가 주어졌을 때, preorder 수행 결과를 구하는 문제입니다. class Solution: def preorder(self, root: 'Node') -> List[int]: return [root.val] + sum([self...

Distribute Candies

Leetcode Problem Distribute Candies candyType 배열이 주어졌을 때, Alice는 candyType에서 배열에서, 길이에서 2를 나눈 만큼의 캔디를 먹을 수 있습니다. Alice가 서로 다른 종류의 캔디를 2개 이상 먹을 수 없을 때, Alice가 먹을 수 있는 최대 캔디 수를 구하는 문제입니다. class Sol...

Maximum Subsequence Score

Leetcode Problem Maximum Subsequence Score nums1, nums2 배열이 주어졌을 때, 각 배열에서 k개의 원소를 고른 다음, nums1에서 k개의 원소의 합과 nums2에서 k개의 원소 중 최솟값을 곱한 것들 중 최댓값을 구하는 문제입니다. class Solution: def maxScore(self, n...

Kth Largest Element in a Stream

Leetcode Problem Kth Largest Element in a Stream class 안에는 변수 k와 nums 배열과 add 함수가 주어졌습니다. add 함수로 nums 배열에 val을 추가하였을 때, k번째 수를 구하는 문제입니다. class KthLargest: def __init__(self, k: int, nums: L...

Subtree of Another Tree

Leetcode Problem Subtree of Another Tree tree와 subtree가 주어졌을 때, tree 안에 subtree가 존재하는지 판별하는 문제입니다. class Solution: def matchTree(self, node1, node2): if not node1 and not node2: ...

Reshape the Matrix

Leetcode Problem Reshape the Matrix m * n 행렬을 r * c 행렬로 바꾸는 문제입니다. 바꿀 수 없을 경우 원래 행렬을 그대로 반환합니다. class Solution: def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[i...

Binary Tree Tilt

Leetcode Problem Binary Tree Tilt Tree에서 왼쪽 child node들과 오른쪽 child node들의 합을 tilt라 합니다. Binary Tree가 주어졌을 때, 모든 node의 tilt의 합을 구하는 문제입니다. class Solution: def __init__(self, tilt = 0): ...

Array Partition

Leetcode Problem Array Partition nums 배열을 두 개씩 짝짓고 그 짝들의 작은 수들의 합을 구하는 문제입니다. class Solution: def arrayPairSum(self, nums: List[int]) -> int: arr = sorted(nums) return sum(...

Maximum Depth of N-ary Tree

Leetcode Problem Maximum Depth of N-ary Tree N-ary Tree의 최대 깊이를 구하는 문제입니다. class Solution: def maxDepth(self, root: 'Node') -> int: return 0 if not root else 1 if not root.children...

Reverse Words in a String III

Leetcode Problem Reverse Words in a String III 주어진 문자열 s를 단어별로 뒤집은 문자열을 구하는 문제입니다. class Solution: def reverseWords(self, s: str) -> str: return ' '.join([x[::-1] for x in s.split(...