본문 바로가기

Algorithm/LeetCode24

[LeetCode] 1523 | Count Odd Numbers in an Interval Range 1523 | Count Odd Numbers in an Interval Range https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/ Given two non-negative integers low and high. Return the count of odd numbers between low and high (inclusive). Example 1: Input: low = 3, high = 7 Output: 3 Explanation: The odd numbers between 3 and 7 are [3,5,7]. Example 2: Input: low = 8, high = 10 Output: 1 Explanation: The od.. 2023. 4. 3.
[LeetCode] 300 | Longest Increasing Subsequence 300 | Longest Increasing Subsequence https://leetcode.com/problems/longest-increasing-subsequence/ Given an integer array nums, return the length of the longest strictly increasing subsequence. Example 1: Input: nums = [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. Example 2: Input: nums = [0,1,0,3,2,3] Output: 4 Example.. 2023. 3. 31.
[LeetCode] 896 | Monotonic Array 896 | Monotonic Array https://leetcode.com/problems/monotonic-array An array is monotonic if it is either monotone increasing or monotone decreasing. An array nums is monotone increasing if for all i 2023. 3. 29.
[LeetCode] 217 | Contains Duplicate 217 | Contains Duplicate https://leetcode.com/problems/contains-duplicate/description/ 작성 코드 class Solution: def containsDuplicate(self, nums: List[int]) -> bool: nums_set = set(nums) return len(nums) != len(nums_set) 2023. 3. 9.
[LeetCode] 1588 | Sum of All Odd Length Subarrays 1588 | Sum of All Odd Length Subarrays https://leetcode.com/problems/sum-of-all-odd-length-subarrays/ 작성 코드 class Solution: def sumOddLengthSubarrays(self, arr: List[int]) -> int: answer = 0 for i in range(1, len(arr) + 1, 2): for start in range(len(arr) - i + 1): answer += sum(arr[start : start+i]) return answer 접근 방법 슬라이싱할 간격을 i 로 지정, 시작 값을 start 로 지정 이상 코드 class Solution: def sumOddLengthSuba.. 2022. 7. 5.
[LeetCode] 226 | Invert Binary Tree 226 | Invert Binary Tree https://leetcode.com/problems/invert-binary-tree/ 이상 코드 재귀함수를 이용한 풀이 class Solution: def invertTree(self, root: TreeNode) -> TreeNode: if root: root.left, root.right = \ self.invertTree(root.right), self.invertTree(root.left) return root return None 재귀 함수로 작성 root.right 를 인자를 넘겼기 때문에 맨 오른쪽의 리프 노드부터 스왑이 이루어짐 가장 말단, 리프 노드까지 내려가서 백트래킹하면서 스왑하는 상향 (Bottom-Up) 방식 BFS를 이용한 풀이 c.. 2022. 5. 30.
반응형