DP Other DP¶
Table of Contents¶
- 1387. Sort Integers by The Power Value (Medium)
- 823. Binary Trees With Factors (Medium)
- 940. Distinct Subsequences II (Hard)
- 135. Candy (Hard)
- 650. 2 Keys Keyboard (Medium)
- 638. Shopping Offers (Medium)
- 467. Unique Substrings in Wraparound String (Medium)
- 2262. Total Appeal of A String (Hard)
- 828. Count Unique Characters of All Substrings of a Given String (Hard)
- 2746. Decremental String Concatenation (Medium)
- 2930. Number of Strings Which Can Be Rearranged to Contain Substring (Medium)
- 1569. Number of Ways to Reorder Array to Get Same BST (Hard)
- 818. Race Car (Hard)
- 920. Number of Music Playlists (Hard)
- 1388. Pizza With 3n Slices (Hard)
- 1987. Number of Unique Good Subsequences (Hard)
- 903. Valid Permutations for DI Sequence (Hard)
- 1896. Minimum Cost to Change the Final Value of Expression (Hard)
- 1531. String Compression II (Hard)
- 964. Least Operators to Express Number (Hard)
- 1787. Make the XOR of All Segments Equal to Zero (Hard)
- 2060. Check if an Original String Exists Given Two Encoded Strings (Hard)
- 2809. Minimum Time to Make Array Sum At Most x (Hard)
- 3299. Sum of Consecutive Subsequences (Hard) 👑
- 2189. Number of Ways to Build House of Cards (Medium) 👑
- 2597. The Number of Beautiful Subsets (Medium)
- 2638. Count the Number of K-Free Subsets (Medium) 👑
1387. Sort Integers by The Power Value¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: dynamic programming, memoization, sorting
823. Binary Trees With Factors¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, hash table, dynamic programming, sorting
940. Distinct Subsequences II¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: string, dynamic programming
135. Candy¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, greedy
- Return the minimum number of candies you must give.
from typing import List
# Greedy
def candy(ratings: List[int]) -> int:
# TC: O(n)
# SC: O(n)
n = len(ratings)
if n <= 1:
return n
candy = [1 for _ in range(n)]
for i in range(1, n):
if ratings[i] > ratings[i - 1]:
candy[i] = candy[i - 1] + 1
for j in range(n - 2, -1, -1):
if ratings[j] > ratings[j + 1]:
candy[j] = max(candy[j], candy[j + 1] + 1)
return sum(candy)
ratings = [1, 0, 2]
print(candy(ratings)) # 5
650. 2 Keys Keyboard¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: math, dynamic programming
638. Shopping Offers¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, dynamic programming, backtracking, bit manipulation, memoization, bitmask
467. Unique Substrings in Wraparound String¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: string, dynamic programming
2262. Total Appeal of A String¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: hash table, string, dynamic programming
828. Count Unique Characters of All Substrings of a Given String¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: hash table, string, dynamic programming
2746. Decremental String Concatenation¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, string, dynamic programming
2930. Number of Strings Which Can Be Rearranged to Contain Substring¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: math, dynamic programming, combinatorics
1569. Number of Ways to Reorder Array to Get Same BST¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, math, divide and conquer, dynamic programming, tree, union find, binary search tree, memoization, combinatorics, binary tree
818. Race Car¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: dynamic programming
920. Number of Music Playlists¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: math, dynamic programming, combinatorics
1388. Pizza With 3n Slices¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, dynamic programming, greedy, heap priority queue
1987. Number of Unique Good Subsequences¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: string, dynamic programming
903. Valid Permutations for DI Sequence¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: string, dynamic programming, prefix sum
1896. Minimum Cost to Change the Final Value of Expression¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: math, string, dynamic programming, stack
1531. String Compression II¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: string, dynamic programming
964. Least Operators to Express Number¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: math, dynamic programming, memoization
1787. Make the XOR of All Segments Equal to Zero¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, dynamic programming, bit manipulation
2060. Check if an Original String Exists Given Two Encoded Strings¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: string, dynamic programming
2809. Minimum Time to Make Array Sum At Most x¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, dynamic programming, sorting
3299. Sum of Consecutive Subsequences¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, hash table, dynamic programming
2189. Number of Ways to Build House of Cards¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: math, dynamic programming
2597. The Number of Beautiful Subsets¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, hash table, math, dynamic programming, backtracking, sorting, combinatorics
#include <functional>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
class Solution {
public:
int beautifulSubsets(vector<int>& nums, int k) {
int res = 0;
unordered_map<int, int> cnt;
auto dfs = [&](auto&& self, int i) -> void {
if (i == (int)nums.size()) {
res++;
return;
}
self(self, i + 1); // Skip nums[i]
int x = nums[i];
if (cnt[x - k] == 0 && cnt[x + k] == 0) {
cnt[x]++;
self(self, i + 1); // Include nums[i]
cnt[x]--; // Backtrack
}
};
dfs(dfs, 0);
return res - 1;
}
};
int main() {
Solution sol;
vector<int> nums = {1, 2, 3, 4};
int k = 1;
cout << sol.beautifulSubsets(nums, k) << endl;
return 0;
}
2638. Count the Number of K-Free Subsets¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, math, dynamic programming, sorting, combinatorics