From 6669dc692d6cf52debb1d6d5b0d357927eadbec0 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 2 Nov 2024 22:00:25 +0530 Subject: [PATCH] Flatten a Multilevel Doubly Linked List --- src/data/problemData.ts | 1432 +++++++++++++++++++++------------------ 1 file changed, 780 insertions(+), 652 deletions(-) diff --git a/src/data/problemData.ts b/src/data/problemData.ts index 02a389dc6..a8a1efd3f 100644 --- a/src/data/problemData.ts +++ b/src/data/problemData.ts @@ -1,15 +1,15 @@ const problemsData = { - twoSum: { - title: "1. Two Sum", - description: - "Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.", - examples: [ - { input: "[2,7,11,15], target = 9", output: "[0,1]" }, - { input: "[3,2,4], target = 6", output: "[1,2]" }, - { input: "[3,3], target = 6", output: "[0,1]" }, - ], - solution: { - cpp: ` + twoSum: { + title: "1. Two Sum", + description: + "Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.", + examples: [ + { input: "[2,7,11,15], target = 9", output: "[0,1]" }, + { input: "[3,2,4], target = 6", output: "[1,2]" }, + { input: "[3,3], target = 6", output: "[0,1]" }, + ], + solution: { + cpp: ` class Solution { public: vector twoSum(vector& nums, int target) { @@ -28,7 +28,7 @@ const problemsData = { return result; } };`, - java: ` + java: ` import java.util.HashMap; public class Solution { @@ -47,7 +47,7 @@ const problemsData = { return new int[] {}; } }`, - python: ` + python: ` class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: res = {} @@ -59,20 +59,20 @@ const problemsData = { return [res[complement], i] res[num] = i`, + }, }, - }, - containerWithMostWater: { - title: "2. Container With Most Water", - description: - "Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). Find two lines that form a container with the maximum area of water.", - examples: [ - { input: "[1,8,6,2,5,4,8,3,7]", output: "49" }, - { input: "[1,1]", output: "1" }, - { input: "[4,3,2,1,4]", output: "16" }, - { input: "[1,2,1]", output: "2" }, - ], - solution: { - cpp: ` + containerWithMostWater: { + title: "2. Container With Most Water", + description: + "Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). Find two lines that form a container with the maximum area of water.", + examples: [ + { input: "[1,8,6,2,5,4,8,3,7]", output: "49" }, + { input: "[1,1]", output: "1" }, + { input: "[4,3,2,1,4]", output: "16" }, + { input: "[1,2,1]", output: "2" }, + ], + solution: { + cpp: ` class Solution { public: int maxArea(vector& height) { @@ -88,7 +88,7 @@ const problemsData = { return area; } };`, - java: ` + java: ` public class Solution { public int maxArea(int[] height) { int left = 0, right = height.length - 1; @@ -108,7 +108,7 @@ const problemsData = { return maxArea; } }`, - python: ` + python: ` class Solution: def maxArea(self, height: List[int]) -> int: left, right = 0, len(height) - 1 @@ -124,19 +124,19 @@ class Solution: right -= 1 return max_area`, + }, }, - }, - threeSum: { - title: "3. 3Sum", - description: - "Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.", - examples: [ - { input: "[-1,0,1,2,-1,-4]", output: "[[-1,-1,2],[-1,0,1]]" }, - { input: "[]", output: "[]" }, - { input: "[0]", output: "[]" }, - ], - solution: { - cpp: ` + threeSum: { + title: "3. 3Sum", + description: + "Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.", + examples: [ + { input: "[-1,0,1,2,-1,-4]", output: "[[-1,-1,2],[-1,0,1]]" }, + { input: "[]", output: "[]" }, + { input: "[0]", output: "[]" }, + ], + solution: { + cpp: ` vector> threeSum(vector& nums) { vector> res; if(nums.size() < 3) return res; @@ -160,7 +160,7 @@ class Solution: return res; } };`, - java: ` + java: ` import java.util.*; public class Solution { public List> threeSum(int[] nums) { @@ -185,7 +185,7 @@ public class Solution { return res; } };`, - python: ` + python: ` class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: res = [] @@ -209,22 +209,22 @@ class Solution: j += 1 k -= 1 return res`, + }, }, - }, - - isValidParentheses: { - title: "4. Valid Parentheses", - description: - "Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.", - examples: [ - { input: "(){}", output: "true" }, - { input: "()[]{}", output: "true" }, - { input: "(]", output: "false" }, - { input: "([)]", output: "false" }, - { input: "{[]}", output: "true" }, - ], - solution: { - cpp: ` + + isValidParentheses: { + title: "4. Valid Parentheses", + description: + "Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.", + examples: [ + { input: "(){}", output: "true" }, + { input: "()[]{}", output: "true" }, + { input: "(]", output: "false" }, + { input: "([)]", output: "false" }, + { input: "{[]}", output: "true" }, + ], + solution: { + cpp: ` class Solution { public: bool isValid(string s) { @@ -239,7 +239,7 @@ public: return stk.empty(); } };`, - java: ` + java: ` import java.util.Stack; public class Solution { @@ -254,7 +254,7 @@ public class Solution { return stack.isEmpty(); } };`, - python: ` + python: ` class Solution: def isValid(self, s: str) -> bool: stack = [] @@ -267,20 +267,20 @@ class Solution: else: stack.append(char) return not stack`, + }, }, - }, - - mergeTwoLists: { - title: "5. Merge Two Sorted Lists", - description: - "Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists.", - examples: [ - { input: "[1,2,4], [1,3,4]", output: "[1,1,2,3,4,4]" }, - { input: "[], []", output: "[]" }, - { input: "[], [0]", output: "[0]" }, - ], - solution: { - cpp: ` + + mergeTwoLists: { + title: "5. Merge Two Sorted Lists", + description: + "Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists.", + examples: [ + { input: "[1,2,4], [1,3,4]", output: "[1,1,2,3,4,4]" }, + { input: "[], []", output: "[]" }, + { input: "[], [0]", output: "[0]" }, + ], + solution: { + cpp: ` class Solution { public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { @@ -295,7 +295,7 @@ public: } } };`, - java: ` + java: ` public class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if (l1 == null) return l2; @@ -309,7 +309,7 @@ public class Solution { } } };`, - python: ` + python: ` class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: if not l1: @@ -322,28 +322,28 @@ class Solution: else: l2.next = self.mergeTwoLists(l1, l2.next) return l2`, + }, }, - }, - - nextPermutation: { - title: "6. Next Permutation", - description: - "Implement next permutation which rearranges numbers into the lexicographically next greater permutation of numbers.", - examples: [ - { input: "[1,2,3]", output: "[1,3,2]" }, - { input: "[3,2,1]", output: "[1,2,3]" }, - { input: "[1,5]", output: "[5,1]" }, - { input: "[5]", output: "[5]" }, - ], - solution: { - cpp: ` + + nextPermutation: { + title: "6. Next Permutation", + description: + "Implement next permutation which rearranges numbers into the lexicographically next greater permutation of numbers.", + examples: [ + { input: "[1,2,3]", output: "[1,3,2]" }, + { input: "[3,2,1]", output: "[1,2,3]" }, + { input: "[1,5]", output: "[5,1]" }, + { input: "[5]", output: "[5]" }, + ], + solution: { + cpp: ` class Solution { public: void nextPermutation(vector& nums) { next_permutation(nums.begin(), nums.end()); } };`, - java: ` + java: ` import java.util.Arrays; public class Solution { public void nextPermutation(int[] nums) { @@ -370,7 +370,7 @@ public class Solution { } } };`, - python: ` + python: ` class Solution: def nextPermutation(self, nums: List[int]) -> None: i = len(nums) - 2 @@ -382,19 +382,19 @@ class Solution: j -= 1 nums[i], nums[j] = nums[j], nums[i] nums[i + 1:] = nums[i + 1:][::-1]`, + }, }, - }, - searchInsert: { - title: "7. Search Insert Position", - description: - "Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You must write an algorithm with O(log n) runtime complexity.", - examples: [ - { input: "[1,3,5,6], target=5", output: "2" }, - { input: "[1,3], target=0", output: "0" }, - { input: "[3], target=4", output: "1" }, - ], - solution: { - cpp: ` + searchInsert: { + title: "7. Search Insert Position", + description: + "Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You must write an algorithm with O(log n) runtime complexity.", + examples: [ + { input: "[1,3,5,6], target=5", output: "2" }, + { input: "[1,3], target=0", output: "0" }, + { input: "[3], target=4", output: "1" }, + ], + solution: { + cpp: ` class Solution { public: int searchInsert(vector& nums, int target) { @@ -408,7 +408,7 @@ public: return low; } };`, - java: ` + java: ` class Solution { public int searchInsert(int[] nums, int target) { int low = 0, high = nums.length - 1; @@ -421,7 +421,7 @@ class Solution { return low; } };`, - python: ` + python: ` class Solution: def searchInsert(self, nums: List[int], target: int) -> int: low, high = 0, len(nums) - 1 @@ -434,25 +434,25 @@ class Solution: else: low = mid + 1 return low`, + }, }, - }, - - isValidSudoku: { - title: "8. Valid Sudoku", - description: - "Determine if a 9 x 9 Sudoku board is valid by checking that each row, column, and 3 x 3 sub-box contains the digits 1-9 without repetition. The board may be partially filled, with empty cells represented by the character '.'. A board is considered valid if it adheres to these rules.", - examples: [ - { - input: `[['5','3','.','.','7','.','.','.','.'],['6','.','.','1','9','5','.','.','.'],['.','9','8','.','.','.','.','6','.'],['8','.','.','.','6','.','.','.','3'],['4','.','.','8','.','3','.','.','1'],['7','.','.','.','2','.','.','.','6'],['.','6','.','.','.','.','2','8','.'],['.','.' ,'.' ,'4' ,'1' ,'9' ,'.' ,'.' ,'5'],['.' ,'.' ,'.' ,'.' ,'8' ,'.' ,'.' ,'7' ,'9']]`, - output: "true", - }, - { - input: `[['8','3','.','.','7','.','.','.','.'],['6','.','.','1','9','5','.','.','.'],['.','9','8','.','.','.','.','6', '.'],['8', '.', '.', '.', '6', '.', '.', '.', '3'],['4', '.', '.', '8', '.', '3', '.', '.', '1'],['7', '.', '.', '.', '2', '.', '.', '.', '6'],['.', '6', '.', '.', '.', '.', '2', '8', '.'],['.', '.', '.', '4', '1', '9', '.', '.', '5'],['.', '.', '.', '.', '8', '.', '.', '7', '9']]`, - output: "false", - }, - ], - solution: { - cpp: ` + + isValidSudoku: { + title: "8. Valid Sudoku", + description: + "Determine if a 9 x 9 Sudoku board is valid by checking that each row, column, and 3 x 3 sub-box contains the digits 1-9 without repetition. The board may be partially filled, with empty cells represented by the character '.'. A board is considered valid if it adheres to these rules.", + examples: [ + { + input: `[['5','3','.','.','7','.','.','.','.'],['6','.','.','1','9','5','.','.','.'],['.','9','8','.','.','.','.','6','.'],['8','.','.','.','6','.','.','.','3'],['4','.','.','8','.','3','.','.','1'],['7','.','.','.','2','.','.','.','6'],['.','6','.','.','.','.','2','8','.'],['.','.' ,'.' ,'4' ,'1' ,'9' ,'.' ,'.' ,'5'],['.' ,'.' ,'.' ,'.' ,'8' ,'.' ,'.' ,'7' ,'9']]`, + output: "true", + }, + { + input: `[['8','3','.','.','7','.','.','.','.'],['6','.','.','1','9','5','.','.','.'],['.','9','8','.','.','.','.','6', '.'],['8', '.', '.', '.', '6', '.', '.', '.', '3'],['4', '.', '.', '8', '.', '3', '.', '.', '1'],['7', '.', '.', '.', '2', '.', '.', '.', '6'],['.', '6', '.', '.', '.', '.', '2', '8', '.'],['.', '.', '.', '4', '1', '9', '.', '.', '5'],['.', '.', '.', '.', '8', '.', '.', '7', '9']]`, + output: "false", + }, + ], + solution: { + cpp: ` class Solution { public: bool isValidSudoku(vector>& board) { @@ -470,7 +470,7 @@ class Solution: return true; } };`, - java: ` + java: ` class Solution { public boolean isValidSudoku(char[][] board) { HashSet seen = new HashSet<>(); @@ -486,7 +486,7 @@ class Solution: return true; } };`, - python: ` + python: ` class Solution: def isValidSudoku(self, board: List[List[str]]) -> bool: rows, cols, blocks = [set() for _ in range(9)], [set() for _ in range(9)], [set() for _ in range(9)] @@ -500,20 +500,20 @@ class Solution: cols[j].add(curr) blocks[(i//3)*3+j//3].add(curr) return True`, + }, }, - }, - - firstMissingPositive: { - title: "9. First Missing Positive", - description: - "Given an unsorted integer array nums, return the smallest missing positive integer.", - examples: [ - { input: "[1,2,0]", output: "3" }, - { input: "[3,4,-1,1]", output: "2" }, - { input: "[7,8,9,11,12]", output: "1" }, - ], - solution: { - cpp: ` + + firstMissingPositive: { + title: "9. First Missing Positive", + description: + "Given an unsorted integer array nums, return the smallest missing positive integer.", + examples: [ + { input: "[1,2,0]", output: "3" }, + { input: "[3,4,-1,1]", output: "2" }, + { input: "[7,8,9,11,12]", output: "1" }, + ], + solution: { + cpp: ` class Solution { public: int firstMissingPositive(vector& nums) { @@ -528,7 +528,7 @@ class Solution: return n + 1; } };`, - java: ` + java: ` class Solution { public int firstMissingPositive(int[] nums) { int n = nums.length; @@ -542,7 +542,7 @@ class Solution: return n + 1; } };`, - python: ` + python: ` class Solution: def firstMissingPositive(self, nums: List[int]) -> int: n = len(nums) @@ -553,20 +553,20 @@ class Solution: for i in range(1, n + 1): if not present[i]: return i return n + 1`, + }, }, - }, - - maxSubArray: { - title: "10. Maximum Subarray", - description: - "Given an integer array nums, find the contiguous subarray which has the largest sum and return its sum.", - examples: [ - { input: "[-2,1,-3,4,-1,2,1,-5,4]", output: "6" }, - { input: "[1]", output: "1" }, - { input: "[5,4,-1,7,8]", output: "23" }, - ], - solution: { - cpp: ` + + maxSubArray: { + title: "10. Maximum Subarray", + description: + "Given an integer array nums, find the contiguous subarray which has the largest sum and return its sum.", + examples: [ + { input: "[-2,1,-3,4,-1,2,1,-5,4]", output: "6" }, + { input: "[1]", output: "1" }, + { input: "[5,4,-1,7,8]", output: "23" }, + ], + solution: { + cpp: ` class Solution { public: int maxSubArray(vector& nums) { @@ -578,7 +578,7 @@ class Solution: return max_sum; } };`, - java: ` + java: ` class Solution { public int maxSubArray(int[] nums) { int maxSum = nums[0], currSum = nums[0]; @@ -589,7 +589,7 @@ class Solution: return maxSum; } };`, - python: ` + python: ` class Solution: def maxSubArray(self, nums: List[int]) -> int: max_sum = nums[0] @@ -598,19 +598,19 @@ class Solution: curr_sum = max(num, curr_sum + num) max_sum = max(max_sum, curr_sum) return max_sum`, + }, }, - }, - mySqrt: { - title: "11. Sqrt(x)", - description: - "Given a non-negative integer x, compute and return the square root of x.", - examples: [ - { input: "4", output: "2" }, - { input: "8", output: "2" }, - { input: "16", output: "4" }, - ], - solution: { - cpp: ` + mySqrt: { + title: "11. Sqrt(x)", + description: + "Given a non-negative integer x, compute and return the square root of x.", + examples: [ + { input: "4", output: "2" }, + { input: "8", output: "2" }, + { input: "16", output: "4" }, + ], + solution: { + cpp: ` class Solution { public: int mySqrt(int x) { @@ -627,7 +627,7 @@ public: return res; } };`, - java: ` + java: ` class Solution { public int mySqrt(int x) { if (x < 2) return x; @@ -643,7 +643,7 @@ class Solution { return (int) res; } };`, - python: ` + python: ` class Solution: def mySqrt(self, x: int) -> int: if x < 2: return x @@ -656,22 +656,22 @@ class Solution: else: end = mid - 1 return res `, + }, }, - }, - - searchMatrix: { - title: "12. Search a 2D Matrix", - description: - "Write an efficient algorithm that searches for a value in an m x n matrix.", - examples: [ - { - input: "[[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3", - output: "true", - }, - { input: "[[1,3,5],[10],[23]], target = 13", output: "false" }, - ], - solution: { - cpp: ` + + searchMatrix: { + title: "12. Search a 2D Matrix", + description: + "Write an efficient algorithm that searches for a value in an m x n matrix.", + examples: [ + { + input: "[[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3", + output: "true", + }, + { input: "[[1,3,5],[10],[23]], target = 13", output: "false" }, + ], + solution: { + cpp: ` class Solution { public: bool searchMatrix(vector>& matrix, int target) { @@ -686,7 +686,7 @@ public: return false; } };`, - java: ` + java: ` class Solution { public boolean searchMatrix(int[][] matrix, int target) { if (matrix.length == 0) return false; @@ -700,7 +700,7 @@ class Solution { return false; } };`, - python: ` + python: ` class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: if not matrix: return False @@ -713,19 +713,19 @@ class Solution: else: high = mid - 1 return False `, + }, }, - }, - - deleteDuplicates: { - title: "13. Remove Duplicates from Sorted List", - description: - "Given the head of a sorted linked list, delete all duplicates such that each element appears only once.", - examples: [ - { input: "[1,1,2]", output: "[1,2]" }, - { input: "[1,1,2,3,3]", output: "[1,2,3]" }, - ], - solution: { - cpp: ` + + deleteDuplicates: { + title: "13. Remove Duplicates from Sorted List", + description: + "Given the head of a sorted linked list, delete all duplicates such that each element appears only once.", + examples: [ + { input: "[1,1,2]", output: "[1,2]" }, + { input: "[1,1,2,3,3]", output: "[1,2,3]" }, + ], + solution: { + cpp: ` class Solution { public: ListNode* deleteDuplicates(ListNode* head) { @@ -739,7 +739,7 @@ public: return head; } };`, - java: ` + java: ` class Solution { public ListNode deleteDuplicates(ListNode head) { if (head == null) return null; @@ -752,7 +752,7 @@ class Solution { return head; } };`, - python: ` + python: ` class Solution: def deleteDuplicates(self, head: ListNode) -> ListNode: if head is None: return None @@ -763,22 +763,22 @@ class Solution: else: curr = curr.next return head `, + }, }, - }, - - mergeTwoSortedLists: { - title: "14. Merge Two Sorted Lists", - description: - "You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted in non-decreasing order.", - examples: [ - { - input: "[1,2,4,0,0,0], m = 3, nums2 = [2,5,6], n = 3", - output: "[1,2,2,3,5,6]", - }, - { input: "[1], m = 1, nums2 = [], n = 0", output: "[1]" }, - ], - solution: { - cpp: ` + + mergeTwoSortedLists: { + title: "14. Merge Two Sorted Lists", + description: + "You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted in non-decreasing order.", + examples: [ + { + input: "[1,2,4,0,0,0], m = 3, nums2 = [2,5,6], n = 3", + output: "[1,2,2,3,5,6]", + }, + { input: "[1], m = 1, nums2 = [], n = 0", output: "[1]" }, + ], + solution: { + cpp: ` class Solution { public: void merge(vector& nums1, int m, vector& nums2, int n) { @@ -790,7 +790,7 @@ public: sort(nums1.begin(), nums1.end()); } };`, - java: ` + java: ` class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int i = 0; @@ -801,7 +801,7 @@ class Solution { Arrays.sort(nums1); } };`, - python: ` + python: ` class Solution: def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: i = 0 @@ -811,19 +811,19 @@ class Solution: i += 1 nums1.sort() `, + }, }, - }, - inorderTraversal: { - title: "15. Binary Tree Inorder Traversal", - description: - "Given the root of a binary tree, return the inorder traversal of its nodes' values.", - examples: [ - { input: "[1,null,2,3]", output: "[1,3,2]" }, - { input: "[]", output: "[]" }, - { input: "[1]", output: "[1]" }, - ], - solution: { - cpp: ` + inorderTraversal: { + title: "15. Binary Tree Inorder Traversal", + description: + "Given the root of a binary tree, return the inorder traversal of its nodes' values.", + examples: [ + { input: "[1,null,2,3]", output: "[1,3,2]" }, + { input: "[]", output: "[]" }, + { input: "[1]", output: "[1]" }, + ], + solution: { + cpp: ` class Solution { public: vector ans; @@ -835,7 +835,7 @@ public: return ans; } };`, - java: ` + java: ` class Solution { public List inorderTraversal(TreeNode root) { List ans = new ArrayList<>(); @@ -849,7 +849,7 @@ class Solution { inorderTraversalHelper(root.right, ans); } };`, - python: ` + python: ` class Solution: def inorderTraversal(self, root: TreeNode) -> List[int]: ans = [] @@ -861,19 +861,19 @@ class Solution: inorder(root) return ans `, + }, }, - }, - - isSymmetric: { - title: "16. Symmetric Tree", - description: - "Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).", - examples: [ - { input: "[1,2,2,3,4,4,3]", output: "true" }, - { input: "[1,2,2,null,3,null,3]", output: "false" }, - ], - solution: { - cpp: ` + + isSymmetric: { + title: "16. Symmetric Tree", + description: + "Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).", + examples: [ + { input: "[1,2,2,3,4,4,3]", output: "true" }, + { input: "[1,2,2,null,3,null,3]", output: "false" }, + ], + solution: { + cpp: ` class Solution { public: bool isSymmetric(TreeNode* root) { @@ -887,7 +887,7 @@ public: return isSymmetricTest(p->left, q->right) && isSymmetricTest(p->right, q->left); } };`, - java: ` + java: ` class Solution { public boolean isSymmetric(TreeNode root) { if (root == null) return true; @@ -900,7 +900,7 @@ class Solution { return isSymmetricTest(p.left, q.right) && isSymmetricTest(p.right, q.left); } };`, - python: ` + python: ` class Solution: def isSymmetric(self, root: TreeNode) -> bool: if not root: return True @@ -911,20 +911,20 @@ class Solution: if p.val != q.val: return False return self.isSymmetricTest(p.left, q.right) and self.isSymmetricTest(p.right, q.left) `, + }, }, - }, - - levelOrderTraversal: { - title: "17. Binary Tree Level Order Traversal", - description: - "Given the root of a binary tree, return the level order traversal of its nodes' values.", - examples: [ - { input: "[3,9,20,null,null,15,7]", output: "[[3],[9,20],[15,7]]" }, - { input: "[1]", output: "[[1]]" }, - { input: "[]", output: "[]" }, - ], - solution: { - cpp: ` + + levelOrderTraversal: { + title: "17. Binary Tree Level Order Traversal", + description: + "Given the root of a binary tree, return the level order traversal of its nodes' values.", + examples: [ + { input: "[3,9,20,null,null,15,7]", output: "[[3],[9,20],[15,7]]" }, + { input: "[1]", output: "[[1]]" }, + { input: "[]", output: "[]" }, + ], + solution: { + cpp: ` class Solution { public: vector> levelOrder(TreeNode* root) { @@ -948,7 +948,7 @@ public: return ans; } };`, - java: ` + java: ` class Solution { public List> levelOrder(TreeNode root) { List> ans = new ArrayList<>(); @@ -970,7 +970,7 @@ class Solution { return ans; } };`, - python: ` + python: ` class Solution: def levelOrder(self, root: TreeNode) -> List[List[int]]: ans = [] @@ -987,19 +987,19 @@ class Solution: ans.append(level) return ans `, + }, }, - }, - - maxDepthBinaryTree: { - title: "18. Maximum Depth of Binary Tree", - description: "Given the root of a binary tree, return its maximum depth.", - examples: [ - { input: "[3,9,20,null,null,15,7]", output: "3" }, - { input: "[1,null,2]", output: "2" }, - { input: "[]", output: "0" }, - ], - solution: { - cpp: ` + + maxDepthBinaryTree: { + title: "18. Maximum Depth of Binary Tree", + description: "Given the root of a binary tree, return its maximum depth.", + examples: [ + { input: "[3,9,20,null,null,15,7]", output: "3" }, + { input: "[1,null,2]", output: "2" }, + { input: "[]", output: "0" }, + ], + solution: { + cpp: ` class Solution { public: int maxDepth(TreeNode* root) { @@ -1009,7 +1009,7 @@ public: return max(leftDepth, rightDepth) + 1; } };`, - java: ` + java: ` class Solution { public int maxDepth(TreeNode root) { if (root == null) return 0; @@ -1018,7 +1018,7 @@ class Solution { return Math.max(leftDepth, rightDepth) + 1; } };`, - python: ` + python: ` class Solution: def maxDepth(self, root: TreeNode) -> int: if not root: return 0 @@ -1026,18 +1026,18 @@ class Solution: rightDepth = self.maxDepth(root.right) return max(leftDepth, rightDepth) + 1 `, + }, }, - }, - hasPathSum: { - title: "19. Path Sum", - description: - "Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum. A leaf is a node with no children.", - examples: [ - { input: "[5,4,8,11,null,13,4], targetSum = 22", output: "true" }, - { input: "[1], targetSum = 5", output: "false" }, - ], - solution: { - cpp: ` + hasPathSum: { + title: "19. Path Sum", + description: + "Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum. A leaf is a node with no children.", + examples: [ + { input: "[5,4,8,11,null,13,4], targetSum = 22", output: "true" }, + { input: "[1], targetSum = 5", output: "false" }, + ], + solution: { + cpp: ` class Solution { public: bool hasPathSum(TreeNode* root, int sum) { @@ -1050,7 +1050,7 @@ public: return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); } };`, - java: ` + java: ` class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) @@ -1062,7 +1062,7 @@ class Solution { return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val); } };`, - python: ` + python: ` class Solution: def hasPathSum(self, root: TreeNode, sum: int) -> bool: if not root: @@ -1073,19 +1073,19 @@ class Solution: return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val) `, + }, }, - }, - - generatePascalTriangle: { - title: "20. Pascal's Triangle", - description: - "Given an integer numRows, return a string that concatenates the elements of the first numRows of Pascal's triangle in a single line.", - examples: [ - { input: "5", output: [[1], [1, 1], [1, 2, 1], [1, 3, 3], [1, 4, 6]] }, - { input: "1", output: [[1]] }, - ], - solution: { - cpp: ` + + generatePascalTriangle: { + title: "20. Pascal's Triangle", + description: + "Given an integer numRows, return a string that concatenates the elements of the first numRows of Pascal's triangle in a single line.", + examples: [ + { input: "5", output: [[1], [1, 1], [1, 2, 1], [1, 3, 3], [1, 4, 6]] }, + { input: "1", output: [[1]] }, + ], + solution: { + cpp: ` class Solution { public: vector> generate(int numRows) { @@ -1102,7 +1102,7 @@ public: return res; } };`, - java: ` + java: ` class Solution { public List> generate(int numRows) { List> res = new ArrayList<>(); @@ -1120,7 +1120,7 @@ class Solution { return res; } };`, - python: ` + python: ` class Solution: def generate(self, numRows: int) -> List[List[int]]: res = [] @@ -1133,19 +1133,19 @@ class Solution: return res `, + }, }, - }, - - maxProfit: { - title: "21. Best Time to Buy and Sell Stock", - description: - "You are given an array prices where prices[i] represents the price of a given stock on the i-th day.You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.Return the maximum profit you can achieve from this transaction. If no profit can be made, return 0.", - examples: [ - { input: "[7,1,5,3,6,4]", output: "5" }, - { input: "[7,6,4,3,1]", output: "0" }, - ], - solution: { - cpp: ` + + maxProfit: { + title: "21. Best Time to Buy and Sell Stock", + description: + "You are given an array prices where prices[i] represents the price of a given stock on the i-th day.You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.Return the maximum profit you can achieve from this transaction. If no profit can be made, return 0.", + examples: [ + { input: "[7,1,5,3,6,4]", output: "5" }, + { input: "[7,6,4,3,1]", output: "0" }, + ], + solution: { + cpp: ` class Solution { public: int maxProfit(vector& prices) { @@ -1159,7 +1159,7 @@ public: return result; } };`, - java: ` + java: ` class Solution { public int maxProfit(int[] prices) { int minm = Integer.MAX_VALUE; @@ -1172,7 +1172,7 @@ class Solution { return result; } };`, - python: ` + python: ` class Solution: def maxProfit(self, prices: List[int]) -> int: minm = float('inf') @@ -1184,20 +1184,20 @@ class Solution: return result `, + }, }, - }, - - hasCycle: { - title: "22. Linked List Cycle", - description: - "Given the head of a linked list, determine if the linked list has a cycle in it.", - examples: [ - { input: "[3,2,0,-4], pos=1", output: "true" }, - { input: "[1,2], pos=0", output: "true" }, - { input: "[1], pos=-1", output: "false" }, - ], - solution: { - cpp: ` + + hasCycle: { + title: "22. Linked List Cycle", + description: + "Given the head of a linked list, determine if the linked list has a cycle in it.", + examples: [ + { input: "[3,2,0,-4], pos=1", output: "true" }, + { input: "[1,2], pos=0", output: "true" }, + { input: "[1], pos=-1", output: "false" }, + ], + solution: { + cpp: ` class Solution { public: bool hasCycle(ListNode *head) { @@ -1218,7 +1218,7 @@ public: return false; } };`, - java: ` + java: ` class Solution { public boolean hasCycle(ListNode head) { if (head == null || head.next == null) @@ -1238,7 +1238,7 @@ class Solution { return false; } };`, - python: ` + python: ` class Solution: def hasCycle(self, head: ListNode) -> bool: if not head or not head.next: @@ -1256,21 +1256,21 @@ class Solution: return False `, + }, }, - }, - preorderTraversal: { - title: "23. Binary Tree Preorder Traversal", - description: - "Given the root of a binary tree, return the preorder traversal of its nodes' values.", - examples: [ - { input: "[1,null,2,3]", output: "[1,2,3]" }, - { input: "[]", output: "[]" }, - { input: "[1]", output: "[1]" }, - { input: "[1,2]", output: "[1,2]" }, - { input: "[1,null,2]", output: "[1,2]" }, - ], - solution: { - cpp: ` + preorderTraversal: { + title: "23. Binary Tree Preorder Traversal", + description: + "Given the root of a binary tree, return the preorder traversal of its nodes' values.", + examples: [ + { input: "[1,null,2,3]", output: "[1,2,3]" }, + { input: "[]", output: "[]" }, + { input: "[1]", output: "[1]" }, + { input: "[1,2]", output: "[1,2]" }, + { input: "[1,null,2]", output: "[1,2]" }, + ], + solution: { + cpp: ` class Solution { public: vector ans; @@ -1286,7 +1286,7 @@ class Solution: } };`, - java: ` + java: ` import java.util.*; class Solution { @@ -1304,7 +1304,7 @@ class Solution: } };`, - python: ` + python: ` class Solution: def preorderTraversal(self, root: TreeNode) -> List[int]: result = [] @@ -1318,22 +1318,22 @@ class Solution: self.preorder(node.left, result) self.preorder(node.right, result) `, + }, }, - }, - - postorderTraversal: { - title: "24. Binary Tree Postorder Traversal", - description: - "Given the root of a binary tree, return the postorder traversal of its nodes' values.", - examples: [ - { input: "[1,null,2,3]", output: "[3,2,1]" }, - { input: "[]", output: "[]" }, - { input: "[1]", output: "[1]" }, - { input: "[1,2]", output: "[2,1]" }, - { input: "[1,null,2]", output: "[2,1]" }, - ], - solution: { - cpp: ` + + postorderTraversal: { + title: "24. Binary Tree Postorder Traversal", + description: + "Given the root of a binary tree, return the postorder traversal of its nodes' values.", + examples: [ + { input: "[1,null,2,3]", output: "[3,2,1]" }, + { input: "[]", output: "[]" }, + { input: "[1]", output: "[1]" }, + { input: "[1,2]", output: "[2,1]" }, + { input: "[1,null,2]", output: "[2,1]" }, + ], + solution: { + cpp: ` class Solution { public: vector ans; @@ -1349,7 +1349,7 @@ class Solution: } };`, - java: ` + java: ` import java.util.*; class Solution { @@ -1367,7 +1367,7 @@ class Solution: } };`, - python: ` + python: ` class Solution: def postorderTraversal(self, root: TreeNode) -> List[int]: result = [] @@ -1381,20 +1381,20 @@ class Solution: self.postorder(node.right, result) result.append(node.val) `, + }, }, - }, - - removeElements: { - title: "25. Remove Linked List Elements", - description: - "Given the head of a linked list and an integer val, remove all the nodes of the linked list that have Node.val equal to val, and return the new head.", - examples: [ - { input: "[1,2,6,3,4,5,6], val=6", output: "[1,2,3,4,5]" }, - { input: "[], val=1", output: "[]" }, - { input: "[7,7,7,7], val=7", output: "[]" }, - ], - solution: { - cpp: ` + + removeElements: { + title: "25. Remove Linked List Elements", + description: + "Given the head of a linked list and an integer val, remove all the nodes of the linked list that have Node.val equal to val, and return the new head.", + examples: [ + { input: "[1,2,6,3,4,5,6], val=6", output: "[1,2,3,4,5]" }, + { input: "[], val=1", output: "[]" }, + { input: "[7,7,7,7], val=7", output: "[]" }, + ], + solution: { + cpp: ` class Solution { public: ListNode* removeElements(ListNode* head, int val) { @@ -1407,7 +1407,7 @@ class Solution: } };`, - java: ` + java: ` class Solution { public ListNode removeElements(ListNode head, int val) { if (head == null) return null; @@ -1417,7 +1417,7 @@ class Solution: } };`, - python: ` + python: ` class Solution: def removeElements(self, head: ListNode, val: int) -> ListNode: if not head: @@ -1427,20 +1427,20 @@ class Solution: head.next = self.removeElements(head.next, val) return head `, + }, }, - }, - - reverseList: { - title: "26. Reverse Linked List", - description: - "Given the head of a singly linked list, reverse the list, and return the reversed list.", - examples: [ - { input: "[1,2,3,4,5]", output: "[5,4,3,2,1]" }, - { input: "[1,2]", output: "[2,1]" }, - { input: "[]", output: "[]" }, - ], - solution: { - cpp: ` + + reverseList: { + title: "26. Reverse Linked List", + description: + "Given the head of a singly linked list, reverse the list, and return the reversed list.", + examples: [ + { input: "[1,2,3,4,5]", output: "[5,4,3,2,1]" }, + { input: "[1,2]", output: "[2,1]" }, + { input: "[]", output: "[]" }, + ], + solution: { + cpp: ` class Solution { public: ListNode* reverseList(ListNode* head) { @@ -1460,7 +1460,7 @@ class Solution: } };`, - java: ` + java: ` class Solution { public ListNode reverseList(ListNode head) { List values = new ArrayList<>(); @@ -1480,7 +1480,7 @@ class Solution: } };`, - python: ` + python: ` class Solution: def reverseList(self, head: ListNode) -> ListNode: values = [] @@ -1496,19 +1496,19 @@ class Solution: return head `, + }, }, - }, - - findKthLargest: { - title: "27. Kth Largest Element in an Array", - description: - "Given an integer array nums and an integer k, return the kth largest element in the array.", - examples: [ - { input: "[3,2,1,5,6,4], k = 2", output: "5" }, - { input: "[3,2,3,1,2,4,5,5,6], k = 4", output: "4" }, - ], - solution: { - cpp: ` + + findKthLargest: { + title: "27. Kth Largest Element in an Array", + description: + "Given an integer array nums and an integer k, return the kth largest element in the array.", + examples: [ + { input: "[3,2,1,5,6,4], k = 2", output: "5" }, + { input: "[3,2,3,1,2,4,5,5,6], k = 4", output: "4" }, + ], + solution: { + cpp: ` class Solution { public: int findKthLargest(vector& nums, int k) { @@ -1517,7 +1517,7 @@ class Solution: } };`, - java: ` + java: ` import java.util.*; class Solution { @@ -1527,26 +1527,26 @@ class Solution: } };`, - python: ` + python: ` class Solution: def findKthLargest(self, nums: List[int], k: int) -> int: nums.sort() return nums[-k] `, + }, }, - }, - - containsDuplicate: { - title: "28. Contains Duplicate", - description: - "Given an integer array nums, return true if any value appears at least twice in the array.", - examples: [ - { input: "[1,2,3,1]", output: "true" }, - { input: "[1,2,3,4]", output: "false" }, - { input: "[1,1,1,3,3,4,3,2,4,2]", output: "true" }, - ], - solution: { - cpp: ` + + containsDuplicate: { + title: "28. Contains Duplicate", + description: + "Given an integer array nums, return true if any value appears at least twice in the array.", + examples: [ + { input: "[1,2,3,1]", output: "true" }, + { input: "[1,2,3,4]", output: "false" }, + { input: "[1,1,1,3,3,4,3,2,4,2]", output: "true" }, + ], + solution: { + cpp: ` class Solution { public: bool containsDuplicate(vector& nums) { @@ -1554,7 +1554,7 @@ class Solution: } };`, - java: ` + java: ` import java.util.*; class Solution { @@ -1567,25 +1567,25 @@ class Solution: } };`, - python: ` + python: ` class Solution: def containsDuplicate(self, nums: List[int]) -> bool: return len(nums) > len(set(nums)) `, + }, }, - }, - - invertBinaryTree: { - title: "29. Invert Binary Tree", - description: - "Given the root of a binary tree, invert the tree and return its root.", - examples: [ - { input: "[4,2,7,1,3,6,9]", output: "[4,7,2,9,6,3,1]" }, - { input: "[2,1,3]", output: "[2,3,1]" }, - { input: "[]", output: "[]" }, - ], - solution: { - cpp: ` + + invertBinaryTree: { + title: "29. Invert Binary Tree", + description: + "Given the root of a binary tree, invert the tree and return its root.", + examples: [ + { input: "[4,2,7,1,3,6,9]", output: "[4,7,2,9,6,3,1]" }, + { input: "[2,1,3]", output: "[2,3,1]" }, + { input: "[]", output: "[]" }, + ], + solution: { + cpp: ` class Solution { public: TreeNode* invertTree(TreeNode* root) { @@ -1598,7 +1598,7 @@ class Solution: } };`, - java: ` + java: ` class Solution { public TreeNode invertTree(TreeNode root) { if (root == null) return null; @@ -1610,7 +1610,7 @@ class Solution: } };`, - python: ` + python: ` class Solution: def invertTree(self, root: TreeNode) -> TreeNode: if root is None: @@ -1618,21 +1618,21 @@ class Solution: root.left, root.right = self.invertTree(root.right), self.invertTree(root.left) return root `, + }, }, - }, - - MyQueue: { - title: "30. Implement Queue using Stacks", - description: - "Implement a first-in-first-out (FIFO) queue using only two stacks.", - examples: [ - { - input: `["MyQueue", "push", "push", "peek", "pop", "empty"]`, - output: `[null,null,null,1,1,false]`, - }, - ], - solution: { - cpp: ` + + MyQueue: { + title: "30. Implement Queue using Stacks", + description: + "Implement a first-in-first-out (FIFO) queue using only two stacks.", + examples: [ + { + input: `["MyQueue", "push", "push", "peek", "pop", "empty"]`, + output: `[null,null,null,1,1,false]`, + }, + ], + solution: { + cpp: ` class MyQueue { public: stack s1, s2; @@ -1666,7 +1666,7 @@ class Solution: } };`, - java: ` + java: ` import java.util.Stack; class MyQueue { @@ -1701,7 +1701,7 @@ class Solution: } };`, - python: ` + python: ` class MyQueue: def __init__(self): @@ -1724,19 +1724,19 @@ class Solution: def empty(self) -> bool: return not self.s1 `, + }, }, - }, - - isAnagram: { - title: "31. Valid Anagram", - description: - "Given two strings s and t, return true if t is an anagram of s, and false otherwise.", - examples: [ - { input: "anagram, nagaram", output: "true" }, - { input: "rat, car", output: "false" }, - ], - solution: { - cpp: ` + + isAnagram: { + title: "31. Valid Anagram", + description: + "Given two strings s and t, return true if t is an anagram of s, and false otherwise.", + examples: [ + { input: "anagram, nagaram", output: "true" }, + { input: "rat, car", output: "false" }, + ], + solution: { + cpp: ` class Solution { public: bool isAnagram(string s, string t) { @@ -1759,7 +1759,7 @@ class Solution: } };`, - java: ` + java: ` class Solution { public boolean isAnagram(String s, String t) { if (s.length() != t.length()) return false; @@ -1772,7 +1772,7 @@ class Solution: } };`, - python: ` + python: ` class Solution: def isAnagram(self, s: str, t: str) -> bool: if len(s) != len(t): @@ -1784,19 +1784,19 @@ class Solution: count[ord(ch) - ord('a')] -= 1 return all(c == 0 for c in count) `, + }, }, - }, - - missingNumber: { - title: "32. Missing Number", - description: - "Given an array nums containing n distinct numbers in the range [0,n], return the only number in the range that is missing from the array.", - examples: [ - { input: "[3, 0, 1]", output: "2" }, - { input: "[0, 1]", output: "2" }, - ], - solution: { - cpp: ` + + missingNumber: { + title: "32. Missing Number", + description: + "Given an array nums containing n distinct numbers in the range [0,n], return the only number in the range that is missing from the array.", + examples: [ + { input: "[3, 0, 1]", output: "2" }, + { input: "[0, 1]", output: "2" }, + ], + solution: { + cpp: ` class Solution { public: int missingNumber(vector& nums) { @@ -1811,7 +1811,7 @@ class Solution: } };`, - java: ` + java: ` class Solution { public int missingNumber(int[] nums) { int sum = 0, n = nums.length; @@ -1822,26 +1822,26 @@ class Solution: } };`, - python: ` + python: ` class Solution: def missingNumber(self, nums: List[int]) -> int: n = len(nums) return n * (n + 1) // 2 - sum(nums) `, + }, }, - }, - - guessNumber: { - title: "33. Guess Number Higher or Lower", - description: - "You are playing a Guess Game where you have to guess a number between 1 and n. Each time you guess wrong, the system will tell you whether the actual number is higher or lower.", - examples: [ - { input: "n=10, pick=6", output: "6" }, - { input: "n=1, pick=1", output: "1" }, - { input: "n=2, pick=2", output: "2" }, - ], - solution: { - cpp: ` + + guessNumber: { + title: "33. Guess Number Higher or Lower", + description: + "You are playing a Guess Game where you have to guess a number between 1 and n. Each time you guess wrong, the system will tell you whether the actual number is higher or lower.", + examples: [ + { input: "n=10, pick=6", output: "6" }, + { input: "n=1, pick=1", output: "1" }, + { input: "n=2, pick=2", output: "2" }, + ], + solution: { + cpp: ` class Solution { public: int guessNumber(int n) { @@ -1863,7 +1863,7 @@ class Solution: } };`, - java: ` + java: ` class Solution extends GuessGame { public int guessNumber(int n) { int start = 1, end = n; @@ -1880,7 +1880,7 @@ class Solution: } };`, - python: ` + python: ` class Solution: def guessNumber(self, n: int) -> int: start, end = 1, n @@ -1894,19 +1894,19 @@ class Solution: else: start = mid + 1 `, + }, }, - }, - - intersect: { - title: "34. Intersection of Two Arrays II", - description: - "Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays, and you may return the result in any order.", - examples: [ - { input: "[1, 2, 2, 1], [2, 2]", output: "[2, 2]" }, - { input: "[4, 9, 5], [9, 4, 9, 8, 4]", output: "[4, 9]" }, - ], - solution: { - cpp: ` + + intersect: { + title: "34. Intersection of Two Arrays II", + description: + "Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays, and you may return the result in any order.", + examples: [ + { input: "[1, 2, 2, 1], [2, 2]", output: "[2, 2]" }, + { input: "[4, 9, 5], [9, 4, 9, 8, 4]", output: "[4, 9]" }, + ], + solution: { + cpp: ` class Solution { public: vector intersect(vector& nums1, vector& nums2) { @@ -1930,7 +1930,7 @@ class Solution: } };`, - java: ` + java: ` import java.util.Arrays; import java.util.ArrayList; @@ -1957,7 +1957,7 @@ class Solution: } };`, - python: ` + python: ` class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: nums1.sort() @@ -1975,18 +1975,18 @@ class Solution: j += 1 return result `, + }, }, - }, - - runningSum: { - title: "35. Running Sum of 1d Array", - description: "Given an array nums, return the running sum of nums.", - examples: [ - { input: "[1, 2, 3, 4]", output: "[1, 3, 6, 10]" }, - { input: "[5]", output: "[5]" }, - ], - solution: { - cpp: ` + + runningSum: { + title: "35. Running Sum of 1d Array", + description: "Given an array nums, return the running sum of nums.", + examples: [ + { input: "[1, 2, 3, 4]", output: "[1, 3, 6, 10]" }, + { input: "[5]", output: "[5]" }, + ], + solution: { + cpp: ` class Solution { public: vector runningSum(vector& nums) { @@ -1997,7 +1997,7 @@ class Solution: } };`, - java: ` + java: ` class Solution { public int[] runningSum(int[] nums) { for (int i = 1; i < nums.length; i++) { @@ -2007,28 +2007,28 @@ class Solution: } };`, - python: ` + python: ` class Solution: def runningSum(self, nums: List[int]) -> List[int]: for i in range(1, len(nums)): nums[i] += nums[i - 1] return nums `, + }, }, - }, - - shuffleString: { - title: "36. Shuffle String", - description: - "Given a string s and an integer array indices of the same length as s, shuffle the string according to the indices array.", - examples: [ - { - input: `"codeleet", indices = [4, 5, 6, 7, 0, 2, 1, 3]`, - output: `"leetcode"`, - }, - ], - solution: { - cpp: ` + + shuffleString: { + title: "36. Shuffle String", + description: + "Given a string s and an integer array indices of the same length as s, shuffle the string according to the indices array.", + examples: [ + { + input: `"codeleet", indices = [4, 5, 6, 7, 0, 2, 1, 3]`, + output: `"leetcode"`, + }, + ], + solution: { + cpp: ` class Solution { public: string restoreString(string s, vector& indices) { @@ -2040,7 +2040,7 @@ class Solution: } };`, - java: ` + java: ` class Solution { public String restoreString(String s, int[] indices) { char[] res = new char[s.length()]; @@ -2051,7 +2051,7 @@ class Solution: } };`, - python: ` + python: ` class Solution: def restoreString(self, s: str, indices: List[int]) -> str: res = [''] * len(s) @@ -2059,16 +2059,16 @@ class Solution: res[idx] = s[i] return ''.join(res) `, + }, }, - }, - - maxLevelSum: { - title: "37. Maximum Level Sum of a Binary Tree", - description: - "Given the root of a binary tree, return the level with the maximum sum.", - examples: [{ input: "[1, 7, 0, 7, -8, null, null]", output: "2" }], - solution: { - cpp: ` + + maxLevelSum: { + title: "37. Maximum Level Sum of a Binary Tree", + description: + "Given the root of a binary tree, return the level with the maximum sum.", + examples: [{ input: "[1, 7, 0, 7, -8, null, null]", output: "2" }], + solution: { + cpp: ` class Solution { public: int maxLevelSum(TreeNode* root) { @@ -2102,7 +2102,7 @@ class Solution: } };`, - java: ` + java: ` class Solution { public int maxLevelSum(TreeNode root) { if (root == null) return 0; @@ -2136,7 +2136,7 @@ class Solution: } };`, - python: ` + python: ` class Solution: def maxLevelSum(self, root: Optional[TreeNode]) -> int: if not root: @@ -2165,19 +2165,19 @@ class Solution: return result_level `, + }, }, - }, - - firstAlphabet: { - title: "38. First Alphabet of Each Word", - description: - "Given a string S, return a string containing the first letter of each word in the string.", - examples: [ - { input: `"geeks for geeks"`, output: `"gfg"` }, - { input: `"bad is good"`, output: `"big"` }, - ], - solution: { - cpp: ` + + firstAlphabet: { + title: "38. First Alphabet of Each Word", + description: + "Given a string S, return a string containing the first letter of each word in the string.", + examples: [ + { input: `"geeks for geeks"`, output: `"gfg"` }, + { input: `"bad is good"`, output: `"big"` }, + ], + solution: { + cpp: ` class Solution { public: string firstAlphabet(string S) { @@ -2193,7 +2193,7 @@ class Solution: } };`, - java: ` + java: ` class Solution { public String firstAlphabet(String S) { StringBuilder ans = new StringBuilder(); @@ -2209,7 +2209,7 @@ class Solution: } };`, - python: ` + python: ` class Solution: def firstAlphabet(self, S: str) -> str: result = S[0] # Start with the first character @@ -2218,17 +2218,17 @@ class Solution: result += S[i] # Add character after space return result `, + }, }, - }, - - countLeaves: { - title: "39. Count Leaves in a Binary Tree", - description: "Given a Binary Tree, count the number of leaf nodes.", - examples: [ - { input: "[4, 8, 10, 7, 3, null, 5, null, null, null]", output: "3" }, - ], - solution: { - cpp: ` + + countLeaves: { + title: "39. Count Leaves in a Binary Tree", + description: "Given a Binary Tree, count the number of leaf nodes.", + examples: [ + { input: "[4, 8, 10, 7, 3, null, 5, null, null, null]", output: "3" }, + ], + solution: { + cpp: ` int countLeaves(Node* root) { if (!root) return 0; @@ -2237,7 +2237,7 @@ class Solution: return countLeaves(root->left) + countLeaves(root->right); };`, - java: ` + java: ` class Solution { public int countLeaves(Node root) { if (root == null) return 0; @@ -2248,7 +2248,7 @@ class Solution: } };`, - python: ` + python: ` class Solution: def countLeaves(self, root: Optional[Node]) -> int: if not root: @@ -2259,19 +2259,19 @@ class Solution: return self.countLeaves(root.left) + self.countLeaves(root.right) `, + }, }, - }, - - generateBinaryNumbers: { - title: "40. Generate Binary Numbers from 1 to N", - description: - "Given a number N, generate and print all binary numbers with decimal values from 1 to N.", - examples: [ - { input: "N = 2", output: "1 10" }, - { input: "N = 5", output: "1 10 11 100 101" }, - ], - solution: { - cpp: ` + + generateBinaryNumbers: { + title: "40. Generate Binary Numbers from 1 to N", + description: + "Given a number N, generate and print all binary numbers with decimal values from 1 to N.", + examples: [ + { input: "N = 2", output: "1 10" }, + { input: "N = 5", output: "1 10 11 100 101" }, + ], + solution: { + cpp: ` vector generate(int N) { queue q; vector v; @@ -2287,7 +2287,7 @@ class Solution: return v; };`, - java: ` + java: ` import java.util.*; class Solution { public List generate(int N) { @@ -2305,7 +2305,7 @@ class Solution: } };`, - python: ` + python: ` from collections import deque class Solution: def generate(self, N: int) -> List[str]: @@ -2319,19 +2319,19 @@ class Solution: N -= 1 return result `, + }, }, - }, - - minimumDifference: { - title: "41. Minimum Difference Between Any Pair", - description: - "Given an unsorted array, find the minimum difference between any pair in the given array.", - examples: [ - { input: "[2, 4, 5, 9, 7]", output: "1" }, - { input: "[3, 10, 8, 6]", output: "2" }, - ], - solution: { - cpp: ` + + minimumDifference: { + title: "41. Minimum Difference Between Any Pair", + description: + "Given an unsorted array, find the minimum difference between any pair in the given array.", + examples: [ + { input: "[2, 4, 5, 9, 7]", output: "1" }, + { input: "[3, 10, 8, 6]", output: "2" }, + ], + solution: { + cpp: ` class Solution { public: int minimum_difference(vector nums) { @@ -2345,7 +2345,7 @@ class Solution: } };`, - java: ` + java: ` import java.util.*; class Solution { public int minimum_difference(int[] nums) { @@ -2359,7 +2359,7 @@ class Solution: } };`, - python: ` + python: ` class Solution: def minimum_difference(self, nums: List[int]) -> int: nums.sort() @@ -2370,19 +2370,19 @@ class Solution: return minm `, + }, }, - }, - - mthHalf: { - title: "42. Halve N, M-1 Times", - description: - "Given two values N and M, return the value when N is halved M-1 times.", - examples: [ - { input: "N = 100, M = 4", output: "12" }, - { input: "N = 10, M = 5", output: "0" }, - ], - solution: { - cpp: ` + + mthHalf: { + title: "42. Halve N, M-1 Times", + description: + "Given two values N and M, return the value when N is halved M-1 times.", + examples: [ + { input: "N = 100, M = 4", output: "12" }, + { input: "N = 10, M = 5", output: "0" }, + ], + solution: { + cpp: ` class Solution { public: int mthHalf(int N, int M) { @@ -2390,31 +2390,31 @@ class Solution: } };`, - java: ` + java: ` class Solution { public int mthHalf(int N, int M) { return (int) (N / Math.pow(2, M - 1)); } };`, - python: ` + python: ` class Solution: def mthHalf(self, N: int, M: int) -> int: return N // (2 ** (M - 1)) `, + }, }, - }, - - removeChars: { - title: "43. Remove Characters from First String", - description: - "Given two strings string1 and string2, remove those characters from string1 which are present in string2.", - examples: [ - { input: 'string1 = "computer", string2 = "cat"', output: '"ompuer"' }, - { input: 'string1 = "occurrence", string2 = "car"', output: '"ouene"' }, - ], - solution: { - cpp: ` + + removeChars: { + title: "43. Remove Characters from First String", + description: + "Given two strings string1 and string2, remove those characters from string1 which are present in string2.", + examples: [ + { input: 'string1 = "computer", string2 = "cat"', output: '"ompuer"' }, + { input: 'string1 = "occurrence", string2 = "car"', output: '"ouene"' }, + ], + solution: { + cpp: ` class Solution { public: string removeChars(string string1, string string2) { @@ -2431,7 +2431,7 @@ class Solution: } };`, - java: ` + java: ` class Solution { public String removeChars(String string1, String string2) { int[] arr = new int[26]; @@ -2447,7 +2447,7 @@ class Solution: } };`, - python: ` + python: ` class Solution: def removeChars(self, string1: str, string2: str) -> str: arr = [0] * 26 @@ -2461,25 +2461,25 @@ class Solution: return ''.join(ans) `, + }, }, - }, - - rotateArray: { - title: "44. Rotate Array by D Elements", - description: - "Given an unsorted array arr[] of size N, rotate it by D elements (clockwise).", - examples: [ - { - input: "N = 5, D = 2, arr = [1, 2, 3, 4, 5]", - output: "[3, 4, 5, 1, 2]", - }, - { - input: "N = 10, D = 3, arr = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]", - output: "[8, 10, 12, 14, 16, 18, 20, 2, 4, 6]", - }, - ], - solution: { - cpp: ` + + rotateArray: { + title: "44. Rotate Array by D Elements", + description: + "Given an unsorted array arr[] of size N, rotate it by D elements (clockwise).", + examples: [ + { + input: "N = 5, D = 2, arr = [1, 2, 3, 4, 5]", + output: "[3, 4, 5, 1, 2]", + }, + { + input: "N = 10, D = 3, arr = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]", + output: "[8, 10, 12, 14, 16, 18, 20, 2, 4, 6]", + }, + ], + solution: { + cpp: ` #include using namespace std; @@ -2500,7 +2500,7 @@ class Solution: } };`, - java: ` + java: ` import java.util.*; class Solution { @@ -2519,14 +2519,142 @@ class Solution: } };`, - python: ` + python: ` class Solution: def rotateArray(self, n: int, d: int, arr: List[int]) -> List[int]: d = d % n # Handle cases where d >= n return arr[d:] + arr[:d] `, + }, + }, + flattenMultilevelDoublyLinkedList: { + title: "52. Flatten a Multilevel Doubly Linked List", + description: + "You are given a doubly linked list, where each node contains a next pointer, a prev pointer, and an additional child pointer that may or may not point to a separate doubly linked list. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure. Flatten the list so that all the nodes appear in a single-level, doubly linked list. Preserve the order of nodes in the list, and return the head of the flattened list.", + examples: [ + { + input: "head = [1, 2, 3, 4, 5, 6] (with 3->7->8->9->10 and 8->11->12)", + output: "[1, 2, 3, 7, 8, 11, 12, 9, 10, 4, 5, 6]", + }, + { + input: "head = [1, 2, null, 3] (with 1->2->3, and 1->child->4->5->6)", + output: "[1, 4, 5, 6, 2, 3]", + }, + ], + solution: { + cpp: ` + #include + using namespace std; + + // Definition for a Node. + class Node { + public: + int val; + Node* next; + Node* prev; + Node* child; + Node(int _val) : val(_val), next(NULL), prev(NULL), child(NULL) {} + }; + + class Solution { + public: + Node* flatten(Node* head) { + if (!head) return head; + Node* curr = head; + while (curr) { + if (curr->child) { + Node* nextNode = curr->next; + Node* child = flatten(curr->child); + + curr->next = child; + child->prev = curr; + curr->child = NULL; + + Node* temp = child; + while (temp->next) temp = temp->next; + + temp->next = nextNode; + if (nextNode) nextNode->prev = temp; + } + curr = curr->next; + } + return head; + } + };`, + + java: ` + import java.util.*; + + class Node { + public int val; + public Node next; + public Node prev; + public Node child; + + public Node(int val) { + this.val = val; + } + } + + class Solution { + public Node flatten(Node head) { + if (head == null) return head; + Node curr = head; + while (curr != null) { + if (curr.child != null) { + Node nextNode = curr.next; + Node child = flatten(curr.child); + + curr.next = child; + child.prev = curr; + curr.child = null; + + Node temp = child; + while (temp.next != null) temp = temp.next; + + temp.next = nextNode; + if (nextNode != null) nextNode.prev = temp; + } + curr = curr.next; + } + return head; + } + }`, + + python: ` + class Node: + def __init__(self, val=0, next=None, prev=None, child=None): + self.val = val + self.next = next + self.prev = prev + self.child = child + + class Solution: + def flatten(self, head: 'Node') -> 'Node': + if not head: + return head + curr = head + while curr: + if curr.child: + next_node = curr.next + child = self.flatten(curr.child) + + curr.next = child + child.prev = curr + curr.child = None + + temp = child + while temp.next: + temp = temp.next + + temp.next = next_node + if next_node: + next_node.prev = temp + curr = curr.next + return head + `, + }, }, - }, }; export default problemsData;