self picture of CY
CY Wu

This is me who never ever stop learning.

1. Two Sum (Easy)

Description:
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]

Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]

Code
Runtime

2167 ms (39.06%)

Memory

14.22 MB (33.06%)

11. Container with most water (Medium)

Description:
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
Find two lines that together with the x-axis form a container, such that the container contains the most water.
Return the maximum amount of water a container can store.
Noticethat you may not slant the container.

Example 1:
example 1 Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

Example 2:
Input: height = [1,1]
Output: 1

Code
Runtime

531 ms (75.04%)

Memory

29.63 MB (60.31%)

13. Roman to Integer (Easy)

Description:
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

                        Symbol       Value
                        I             1
                        V             5
                        X             10
                        L             50
                        C             100
                        D             500
                        M             1000
                    
For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given a roman numeral, convert it to an integer.

Example 1:
Input: s = "III"
Output: 3
Explanation: III = 3.

Example 2:
Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

Example 3:
Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

Code
Runtime

30 ms (60.81%)

Memory

13.5 MB (14.94%)

14. Longest Common Prefix (Easy)

Description:
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".

Example 1:
Input: strs = ["flower","flow","flight"]
Output: "fl"

Example 2:
Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Code
Runtime

11 ms (94.49%)

Memory

13.5 MB (47.70%)

15. 3Sum (Medium)

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.
Notice that the solution set must not contain duplicate triplets.

Example 1:
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Explanation:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.

Example 2:
Input: s = nums = [0,1,1]
Output: []
Explanation: The only possible triplet does not sum up to 0.

Example 3:
Input: nums = [0,0,0]
Output: [[0,0,0]]
Explanation: The only possible triplet sums up to 0.

Code
Runtime

752 ms (63.67%)

Memory

21.43 MB (44.85%)

19. Remove Nth Node From End of List (Medium)

Description:
Given the head of a linked list, remove the nth node from the end of the list and return its head.

Example 1:
example 1 Input: head = [1,2,3,4,5], n = 2
Output: [1,2,3,5]

Example 2:
Input: head = [1], n = 1
Output: []

Example 3:
Input: head = [1,2], n = 1
Output: [1]

Code
Runtime

34 ms (83.30%)

Memory

16.43 MB (69.46%)

20. Valid Parentheses (Easy)

Description:
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
1. Open brackets must be closed by the same type of brackets.
2. Open brackets must be closed in the correct order.
3. Every close bracket has a corresponding open bracket of the same type.

Example 1:
Input: s = "()"
Output: true

Example 2:
Input: s = "()[]{}"
Output: true

Example 3:
Input: s = "(]"
Output: false

Code
Runtime

7 ms (98.44%)

Memory

13.3 MB (83.30%)

21. Merge Two Sorted Lists (Easy)

Description:
You are given the heads of two sorted linked lists list1 and list2.
Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.
Return the head of the merged linked list.

Example 1:
example 1 Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]

Example 2:
Input: list1 = [], list2 = []
Output: []

Example 3:
Input: list1 = [], list2 = [0]
Output: [0]

Code
Runtime

11 ms (98.09%)

Memory

13.1 MB (83.64%)

24. Swap Nodes in Pairs (Medium)

Description:
Given a linked list, swap every two adjacent nodes and return its head.
You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)

Example 1:
example 1 Input: head = [1,2,3,4]
Output: [2,1,4,3]

Example 2:
Input: head = []
Output: []

Example 3:
Input: head = [1]
Output: [1]

Code
Runtime

35 ms (71.19%)

Memory

17.37 MB (20.16%)

33. Search in Rotated Sorted Array (Medium)

Description:
There is an integer array nums sorted in ascending order (with distinct values).
Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed).
For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].
Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.
You must write an algorithm with O(log n) runtime complexity.

Example 1:
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4

Example 2:
Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1

Example 3:
Input: nums = [1], target = 0
Output: -1

Code
Runtime

41 ms (83.05%)

Memory

16.96 MB (63.42%)

34. Find First and Last Position of Element in Sorted Array (Medium)

Description:
Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.
If target is not found in the array, return [-1, -1].
You must write an algorithm with O(log n) runtime complexity.

Example 1:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]

Example 2:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [-1,-1]

Example 3:
Input: nums = [], target = 0
Output: [-1,-1]

Code
Runtime

69 ms (95.05%)

Memory

17.80 MB (68.10%)

35. Search Insert Position (Easy)

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.

Example 1:
Input: nums = [1,3,5,6], target = 5
Output: 2

Example 2:
Input: nums = [1,3,5,6], target = 2
Output: 1

Example 3:
Input: nums = [1,3,5,6], target = 7
Output: 4

Code
Runtime

28 ms (71.54%)

Memory

13.93 MB (66.71%)

46. Permutations (Medium)

Description:
Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

Example 1:
Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

Example 2:
Input: nums = [0,1]
Output: [[0,1],[1,0]]

Example 3:
Input: nums = [1]
Output: [[1]]

Code
Runtime

39 ms (79.74%)

Memory

17.57 MB (19.66%)

54. Spiral Matrix (Medium)

Description:
Given an m x n matrix, return all elements of the matrix in spiral order.

Example 1:
example 1 Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]

Example 2:
example 2 Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]

Code
Runtime

29 ms (94.05%)

Memory

16.62 MB (57.41%)

62. Unique Paths (Medium)

Description:
There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]).
The robot can only move either down or right at any point in time.
Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.
The test cases are generated so that the answer will be less than or equal to 2 * 109.

Example 1:
example 1 Input: m = 3, n = 7
Output: 28

Example 2:
Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Down -> Down
2. Down -> Down -> Right
3. Down -> Right -> Down

Code
Runtime

31 ms (89.96%)

Memory

17.26 MB (42.75%)

64. Minimum Path Sum (Medium)

Description:
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.

Example 1:
example 1 Input: grid = [[1,3,1],[1,5,1],[4,2,1]]
Output: 7
Explanation:
Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.

Example 2:
Input: grid = [[1,2,3],[4,5,6]]
Output: 12

Code
Runtime

74 ms (97.38%)

Memory

18.37 MB (65.78%)

70. Search a 2D Matrix (Medium)

Description:
You are given an m x n integer matrix matrix with the following two properties:
1. Each row is sorted in non-decreasing order.
2. The first integer of each row is greater than the last integer of the previous row.
Given an integer target, return true if target is in matrix or false otherwise.
You must write a solution in O(log(m * n)) time complexity.

Example 1:
example 1 Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
Output: true

Example 2:
example 2 Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
Output: false

Code
Runtime

48 ms (58.23%)

Memory

17.02 MB (69.37%)

75. Sort Colors (Medium)

Description:
Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue.
We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.
You must solve this problem without using the library's sort function.

Example 1:
Input: nums = [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]

Example 2:
Input: nums = [2,0,1]
Output: [0,1,2]

Code
Runtime

40 ms (52.75%)

Memory

17.45 MB (18.84%)

78. Subsets (Medium)

Description:
Given an integer array nums of unique elements, return all possible subsets (the power set).
The solution set must not contain duplicate subsets. Return the solution in any order.

Example 1:
Input: nums = [1,2,3]
Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

Example 2:
Input: nums = [0]
Output: [[],[0]]

Code
Runtime

38 ms (64.26%)

Memory

17.66 MB (20.58%)

94. Binary Tree Inorder Traversal (Easy)

Description:
Given the root of a binary tree, return the inorder traversal of its nodes' values.

Example 1:
example 1 Input: root = [1,null,2,3]
Output: [1,3,2]

Example 2:
Input: root = []
Output: []

Example 3:
Input: root = [1]
Output: [1]

Code
Runtime

32 ms (86.46%)

Memory

16.50 MB (30.62%)

98. Validate Binary Search Tree (Medium)

Description:
Given the root of a binary tree, determine if it is a valid binary search tree (BST).
A valid BST is defined as follows:
1. The left subtree of a node contains only nodes with keys less than the node's key.
2. The right subtree of a node contains only nodes with keys greater than the node's key.
3. Both the left and right subtrees must also be binary search trees.

Example 1:
example 1 Input: root = [2,1,3]
Output: true

Example 2:
example 2 Input: root = [5,1,4,null,null,3,6]
Output: false
Explanation:
The root node's value is 5 but its right child's value is 4.

Code
Runtime

18 ms (96.43%)

Memory

16.62 MB (86.62%)

101. Symmetric Tree (Easy)

Description:
Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

Example 1:
example 1 Input: root = [1,2,2,3,4,4,3]
Output: true

Example 2:
example 2 Input: root = [1,2,2,null,3,null,3]
Output: false

Code
Runtime

15 ms (76.78%)

Memory

13.50 MB (55.07%)

102. Binary Tree Level Order Traversal (Medium)

Description:
Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).

Example 1:
example 1 Input: root = [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]]

Example 2:
Input: root = [1]
Output: [[1]]

Example 3:
Input: root = []
Output: []

Code
Runtime

29 ms (98.86%)

Memory

17.28 MB (69.74%)

104. Maximum Depth of Binary Tree (Easy)

Description:
Given the root of a binary tree, return its maximum depth.
A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Example 1:
example 1 Input: root = [3,9,20,null,null,15,7]
Output: 3

Example 2:
Input: root = [1,null,2]
Output: 2

Code
Runtime

37 ms (85.40%)

Memory

17.49 MB (99.56%)

118. Pascal's Triangle (Easy)

Description:
Given an integer numRows, return the first numRows of Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
description

Example 1:
Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]

Example 2:
Input: numRows = 1
Output: [[1]]

Code
Runtime

13 ms (81.41%)

Memory

13.22 MB (49.44%)

121. Pascal's Triangle (Easy)

Description:
You are given an array prices where prices[i] is the price of a given stock on the ith 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 you cannot achieve any profit, return 0.

Example 1:
Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation:
Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.

Example 2:
Input: prices = [7,6,4,3,1]
Output: 0
Explanation:
In this case, no transactions are done and the max profit = 0.

Code
Runtime

773 ms (65.04%)

Memory

27.38 MB (83.56%)

136. Single Number (Easy)

Description:
Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.

Example 1:
Input: nums = [2,2,1]
Output: 1

Example 2:
Input: nums = [4,1,2,1,2]
Output: 4

Example 3:
Input: nums = [1]
Output: 1

Code
Runtime

109 ms (40.06%)

Memory

17.04 MB (6.76%)

141. Linked List Cycle (Easy)

Description:
Given head, the head of a linked list, determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer.
Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.
Return true if there is a cycle in the linked list. Otherwise, return false.

Example 1:
example 1 Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation:
There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).

Example 2:
example 2 Input: head = [1,2], pos = 0
Output: true
Explanation:
There is a cycle in the linked list, where the tail connects to the 0th node.

Example 3:
example 3 Input: head = [1], pos = -1
Output: false
Explanation:
There is no cycle in the linked list.

Code
Runtime

48 ms (64.58%)

Memory

19.07 MB (94.59%)

142. Linked List Cycle II (Medium)

Description:
Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer.
Internally, pos is used to denote the index of the node that tail's next pointer is connected to (0-indexed). It is -1 if there is no cycle. Note that pos is not passed as a parameter.
Do not modify the linked list.

Example 1:
example 1 Input: head = [3,2,0,-4], pos = 1
Output: tail connects to node index 1
Explanation:
There is a cycle in the linked list, where tail connects to the second node.

Example 2:
example 2 Input: head = [1,2], pos = 0
Output: tail connects to node index 0
Explanation:
There is a cycle in the linked list, where tail connects to the first node.

Example 3:
example 3 Input: head = [1], pos = -1
Output: no cycle
Explanation:
There is no cycle in the linked list.

Code
Runtime

19 ms (98.73%)

Memory

17.98 MB (90.66%)

153. Find Minimum in Rotated Sorted Array (Medium)

Description:
Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:
1. [4,5,6,7,0,1,2] if it was rotated 4 times.
2. [0,1,2,4,5,6,7] if it was rotated 7 times.
Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].
Given the sorted rotated array nums of unique elements, return the minimum element of this array.
You must write an algorithm that runs in O(log n) time.

Example 1:
Input: nums = [3,4,5,1,2]
Output: 1
Explanation:
The original array was [1,2,3,4,5] rotated 3 times.

Example 2:
Input: nums = [4,5,6,7,0,1,2]
Output: 0
Explanation:
The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.

Example 3:
Input: nums = [11,13,15,17]
Output: 11
Explanation:
The original array was [11,13,15,17] and it was rotated 4 times.

Code
Runtime

45 ms (58.08%)

Memory

17.50 MB (53.50%)

160. Intersection of Two Linked Lists (Easy)

Description:
Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.
For example, the following two linked lists begin to intersect at node c1:
description
The test cases are generated such that there are no cycles anywhere in the entire linked structure.
Note that the linked lists must retain their original structure after the function returns.

Example 1:
example 1 Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
Output: Intersected at '8'
Explanation:
The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).
From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
- Note that the intersected node's value is not 1 because the nodes with value 1 in A and B (2nd node in A and 3rd node in B) are different node references. In other words, they point to two different locations in memory, while the nodes with value 8 in A and B (3rd node in A and 4th node in B) point to the same location in memory.

Example 2:
example 2 Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
Output: Intersected at '2'
Explanation:
The intersected node's value is 2 (note that this must not be 0 if the two lists intersect).
From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.

Example 3:
example 3 Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
Output: No intersection
Explanation:
From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
Explanation: The two lists do not intersect, so return null.

Code
Runtime

112 ms (93.18%)

Memory

43.32 MB (26.96%)

169. Majority Element (Easy)

Description:
Given an array nums of size n, return the majority element.
The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.

Example 1:
Input: nums = [3,2,3]
Output: 3

Example 2:
Input: nums = [2,2,1,1,1,2,2]
Output: 2

Code
Runtime

138 ms (64.21%)

Memory

18.16 MB (52.79%)

189. Rotate Array (Medium)

Description:
Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.

Example 1:
Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]

Example 2:
Input: nums = [-1,-100,3,99], k = 2
Output: [3,99,-1,-100]
Explanation:
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]

Code
Runtime

153 ms (89.60%)

Memory

27.87 MB (81.84%)

198. House Robber (Medium)

Description:
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

Example 1:
Input: nums = [1,2,3,1]
Output: 4
Explanation:
Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.

Example 2:
Input: nums = [2,7,9,3,1]
Output: 12
Explanation:
Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
Total amount you can rob = 2 + 9 + 1 = 12.

Code
Runtime

20 ms (99.78%)

Memory

26.46 MB (81.33%)

199. Binary Tree Right Side View (Medium)

Description:
Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

Example 1:
example 1 Input: root = [1,2,3,null,5,null,4]
Output: [1,3,4]

Example 2:
Input: root = [1,null,3]
Output: [1,3]

Example 3:
Input: root = []
Output: []

Code
Runtime

39 ms (55.10%)

Memory

17.30 MB (40.41%)

206. Reverse Linked List (Easy)

Description:
Given the head of a singly linked list, reverse the list, and return the reversed list.

Example 1:
example 1 Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]

Example 2:
example 1 Input: head = [1,2]
Output: [2,1]

Example 3:
Input: head = []
Output: []

Code
Runtime

14 ms (89.19%)

Memory

15.20 MB (76.09%)

226. Invert Binary Tree (Easy)

Description:
Given the root of a binary tree, invert the tree, and return its root.

Example 1:
example 1 Input: root = [4,2,7,1,3,6,9]
Output: [4,7,2,9,6,3,1]

Example 2:
example 1 Input: root = [2,1,3]
Output: [2,3,1]

Example 3:
Input: root = []
Output: []

Code
Runtime

17 ms (44.86%)

Memory

13.10 MB (79.15%)

230. Kth Smallest Element in a BST (Medium)

Description:
Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.

Example 1:
example 1 Input: root = [3,1,4,null,2], k = 1
Output: 1

Example 2:
example 1 Input: root = [5,3,6,2,4,null,null,1], k = 3
Output: 3

Code
Runtime

44 ms (78.88%)

Memory

20.35 MB (54.07%)

234. Palindrome Linked List (Easy)

Description:
Given the head of a singly linked list, return true if it is a palindrome or false otherwise.

Example 1:
example 1 Input: head = [1,2,2,1]
Output: true

Example 2:
example 1 Input: head = [1,2]
Output: false

Code
Runtime

679 ms (76.58%)

Memory

66.44 MB (88.08%)

240. Search a 2D Matrix II (Medium)

Description:
Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix. This matrix has the following properties:
1. Integers in each row are sorted in ascending from left to right.
2. Integers in each column are sorted in ascending from top to bottom.

Example 1:
example 1 Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
Output: true

Example 2:
example 1 Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
Output: false

Code
Runtime

137 ms (86.57%)

Memory

23.76 MB (37.27%)

283. Move Zero (Easy)

Description:
Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.

Example 1:
Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]

Example 2:
Input: nums = [0]
Output: [0]

Code
Runtime

113 ms (98.83%)

Memory

18.78 MB (49.27%)

543. Diameter of Binary Tree (Easy)

Description:
Given the root of a binary tree, return the length of the diameter of the tree.
The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
The length of a path between two nodes is represented by the number of edges between them.

Example 1:
example 1 Input: root = [1,2,3,4,5]
Output: 3
Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3].

Example 2:
Input: root = [1,2]
Output: 1

Code
Runtime

27 ms (52.27%)

Memory

15.99 MB (69.88%)

706. Binary Search (Easy)

Description:
Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1. You must write an algorithm with O(log n) runtime complexity.

Example 1:
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4

Example 2:
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1

Code
Runtime

170 ms (96.68%)

Memory

14.50 MB (64.13%)

739. Daily Temperatures (Medium)

Description:
Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature.
If there is no future day for which this is possible, keep answer[i] == 0 instead.

Example 1:
Input: temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]

Example 2:
Input: temperatures = [30,40,50,60]
Output: [1,1,1,0]

Example 3:
Input: temperatures = [30,60,90]
Output: [1,1,0]

Code
Runtime

900 ms (74.29%)

Memory

31.22 MB (71.79%)