Leetcode Question of Today
70 subscribers
472 links
Send Question of Today from Leetcode everyday at 0:00 (UTC)
Download Telegram
2021-12-11
878. Nth Magical Number

Topic: Math, Binary Search
Difficulty: Hard

Problem:
A positive integer is magical if it is divisible by either a or b.

Given the three integers n, a, and b, return the n^th magical number. Since the answer may be very large, return it modulo 10^9 + 7.

Example 1:

Input: n = 1, a = 2, b = 3
Output: 2


Example 2:

Input: n = 4, a = 2, b = 3
Output: 6


Example 3:

Input: n = 5, a = 2, b = 4
Output: 10


Example 4:

Input: n = 3, a = 6, b = 4
Output: 8



Constraints:

1 <= n <= 10^92 <= a, b <= 4 * 10^4
2021-12-12
416. Partition Equal Subset Sum

Topic: Array, Dynamic Programming
Difficulty: Medium

Problem:
Given a non-empty array nums containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.

Example 1:

Input: nums = [1,5,11,5]
Output: true
Explanation: The array can be partitioned as [1, 5, 5] and [11].


Example 2:

Input: nums = [1,2,3,5]
Output: false
Explanation: The array cannot be partitioned into equal sum subsets.


Constraints:

1 <= nums.length <= 200
1 <= nums[i] <= 100
2021-12-13
1446. Consecutive Characters

Topic: String
Difficulty: Easy

Problem:
The power of the string is the maximum length of a non-empty substring that contains only one unique character.

Given a string s, return the power of s.

Example 1:

Input: s = "leetcode"
Output: 2
Explanation: The substring "ee" is of length 2 with the character 'e' only.


Example 2:

Input: s = "abbcccddddeeeeedcba"
Output: 5
Explanation: The substring "eeeee" is of length 5 with the character 'e' only.


Example 3:

Input: s = "triplepillooooow"
Output: 5


Example 4:

Input: s = "hooraaaaaaaaaaay"
Output: 11


Example 5:

Input: s = "tourist"
Output: 1


Constraints:

1 <= s.length <= 500
s consists of only lowercase English letters.
2021-12-14
938. Range Sum of BST

Topic: Tree, Depth-First Search, Binary Search Tree, Binary Tree
Difficulty: Easy

Problem:
Given the root node of a binary search tree and two integers low and high, return the sum of values of all nodes with a value in the inclusive range [low, high].

Example 1:

Image: https://assets.leetcode.com/uploads/2020/11/05/bst1.jpg

Input: root = [10,5,15,3,7,null,18], low = 7, high = 15
Output: 32
Explanation: Nodes 7, 10, and 15 are in the range [7, 15]. 7 + 10 + 15 = 32.


Example 2:

Image: https://assets.leetcode.com/uploads/2020/11/05/bst2.jpg

Input: root = [10,5,15,3,7,13,18,1,null,6], low = 6, high = 10
Output: 23
Explanation: Nodes 6, 7, and 10 are in the range [6, 10]. 6 + 7 + 10 = 23.


Constraints:

• The number of nodes in the tree is in the range [1, 2 * 10^4].
1 <= Node.val <= 10^5
1 <= low <= high <= 10^5
• All Node.val are unique.
2021-12-15
147. Insertion Sort List

Topic: Linked List, Sorting
Difficulty: Medium

Problem:
Given the head of a singly linked list, sort the list using insertion sort, and return the sorted list's head.

The steps of the insertion sort algorithm:

1. Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.
2. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there.
3. It repeats until no input elements remain.

The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.

Image: https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif

Example 1:

Image: https://assets.leetcode.com/uploads/2021/03/04/sort1linked-list.jpg

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


Example 2:

Image: https://assets.leetcode.com/uploads/2021/03/04/sort2linked-list.jpg

Input: head = [-1,5,3,4,0]
Output: [-1,0,3,4,5]


Constraints:

• The number of nodes in the list is in the range [1, 5000].
-5000 <= Node.val <= 5000