Skip to content

Instantly share code, notes, and snippets.

@rahulsai4
rahulsai4 / lc343.cpp
Created May 13, 2025 11:34
integer break
class Solution {
public:
int integerBreak(int n) {
vector<int> dp(n + 1, 0);
dp[1] = 1;
dp[2] = 1;
for(int i = 3; i <= n; i++){
for(int j = 2; j < i; j++){
// divide into parts
int left = max(j, dp[j]);
@rahulsai4
rahulsai4 / lc95.cpp
Created May 13, 2025 11:15
unique binary search trees 2
class Solution {
public:
vector<TreeNode*> generateTrees(int n) {
vector<vector<TreeNode*>> dp(n + 1, vector<TreeNode*>());
dp[0].push_back(nullptr);
dp[1].push_back(new TreeNode(1));
for(int i = 2; i <= n; i++){
// root selection
for(int j = 1; j <= i; j++){
@rahulsai4
rahulsai4 / lc96.cpp
Created May 13, 2025 08:33
unique binary search trees
class Solution {
public:
int numTrees(int n) {
vector<int> dp(n + 1, 0);
dp[0] = 1;
dp[1] = 1;
for(int i = 2; i <= n; i++){
int avail = i - 1;
for(int j = 0; j < i; j++){
class Solution {
public:
using lli = long long int;
int M = (int) 1e9 + 7;
int lengthAfterTransformations(string s, int t) {
array<int, 26> freq{};
for(char c : s) freq[c - 'a']++;
while(t >= 26){
array<lli, 26> temp{};
@rahulsai4
rahulsai4 / lc377.cpp
Created May 12, 2025 16:34
combination sum 4 vs coin change 2
class Solution {
public:
int combinationSum4(vector<int>& coins, int amount) {
int n = coins.size();
vector<unsigned int> curr(amount + 1, 0);
curr[0] = 1;
for(int j = 1; j <= amount; j++){
for(int coin : coins){
@rahulsai4
rahulsai4 / lc518.cpp
Created May 12, 2025 15:50
coin change 2
class Solution {
public:
using lli = long long;
int change(int amount, vector<int>& coins) {
int n = coins.size();
vector<unsigned int> curr(amount + 1, 0);
curr[0] = 1;
for(int coin : coins){
@rahulsai4
rahulsai4 / lc322.cpp
Created May 12, 2025 15:38
coin change 1
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
int n = coins.size();
vector<int> curr(amount + 1, amount + 1);
curr[0] = 0;
for(int coin : coins){
// (0, coin - 1) => already have coorect values
import { zodResolver } from "@hookform/resolvers/zod";
import React from "react";
import { useForm } from "react-hook-form";
import { z } from "zod";
const schema = z.object({
description: z.string().min(10, "min length of description is 10"),
});
const TaskForm = () => {
@rahulsai4
rahulsai4 / lc494.cpp
Created May 11, 2025 04:51
target sum
class Solution {
public:
int findTargetSumWays(vector<int>& nums, int target) {
int n = nums.size();
int sum = accumulate(nums.begin(), nums.end(), 0);
if(sum < abs(target)) return 0;
// (-sum, sum) => (0, 2 * sum)
int dp[21][10001] = {};
class Solution {
public:
bool canPartition(vector<int>& nums) {
int sum = accumulate(nums.begin(), nums.end(), 0);
if(sum % 2 == 1) return false;
int target = sum / 2;
bitset<200 * 100 + 1> dp;
dp[0] = 1;
for(int num : nums){