Skip to content

Instantly share code, notes, and snippets.

13 Sep 2024

2265. Count Nodes Equal to Average of Subtree
809. Expressive Words
106. Construct Binary Tree from Inorder and Postorder Traversal
386. Lexicographical Numbers

6 Sep 2024

/// https://leetcode.com/problems/intervals-between-identical-elements
///
class Solution {
public long[] getDistances(int[] arr) {
int n = arr.length;
HashMap<Integer, ArrayList<Integer>> map = new HashMap<>();
for (int i=0; i<n; i++) {
ArrayList<Integer> a = map.get(arr[i]);
if (a == null) {
a = new ArrayList<>();
@igavrysh
igavrysh / Solution.java
Created October 6, 2024 00:46
Leetcode2062 solution
// https://leetcode.com/problems/count-vowel-substrings-of-a-string/
// https://youtu.be/hGH_Z4_jMBI
///
class Solution {
public int countVowelSubstrings(String word) {
int n = word.length();
int[] fq = new int[26];
int k = 0; int l = 0;
int res = 0;
for (int r = 0; r < n; r++) {
@igavrysh
igavrysh / leetcode_2337.java
Created December 5, 2024 05:48
2337. Move Pieces to Obtain a String
/*
https://leetcode.com/problems/move-pieces-to-obtain-a-string
2337. Move Pieces to Obtain a String
Solved
Medium
You are given two strings start and target, both of length n. Each string consists only of the characters 'L', 'R', and '_' where:
The characters 'L' and 'R' represent pieces, where a piece 'L' can move to the left only if there is a blank space directly to its left, and a piece 'R' can move to the right only if there is a blank space directly to its right.
The character '_' represents a blank space that can be occupied by any of the 'L' or 'R' pieces.
@igavrysh
igavrysh / leetcode_2554.java
Created December 6, 2024 05:52
2554. Maximum Number of Integers to Choose From a Range I
/**
https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-i/description/?envType=daily-question&envId=2024-12-06
2554. Maximum Number of Integers to Choose From a Range I
Solved
Medium
Topics
Companies
Hint
You are given an integer array banned and two integers n and maxSum. You are choosing some number of integers following the below rules:
@igavrysh
igavrysh / leetcode_1760.java
Created December 7, 2024 17:40
1760. Minimum Limit of Balls in a Bag
/**
1760. Minimum Limit of Balls in a Bag
Medium
You are given an integer array nums where the ith bag contains nums[i] balls. You are also given an integer maxOperations.
You can perform the following operation at most maxOperations times:
Take any bag of balls and divide it into two new bags with a positive number of balls.
@igavrysh
igavrysh / leetcode_2054.java
Created December 8, 2024 03:50
2054. Two Best Non-Overlapping Events
/**
https://leetcode.com/problems/two-best-non-overlapping-events/
2054. Two Best Non-Overlapping Events
Solved
Medium
Topics
Companies
Hint
You are given a 0-indexed 2D integer array of events where events[i] = [startTimei, endTimei, valuei]. The ith event starts at startTimei and ends at endTimei, and if you attend this event, you will receive a value of valuei. You can choose at most two non-overlapping events to attend such that the sum of their values is maximized.
@igavrysh
igavrysh / leetcode_3152.java
Created December 9, 2024 23:34
3152. Special Array II
/*
https://leetcode.com/problems/special-array-ii
3152. Special Array II
Medium
An array is considered special if every pair of its adjacent elements contains two numbers with different parity.
You are given an array of integer nums and a 2D integer matrix queries, where for queries[i] = [fromi, toi] your task is to check that
@igavrysh
igavrysh / leetcode_2981.java
Last active December 11, 2024 15:51
2981. Find Longest Special Substring That Occurs Thrice I
/*
https://leetcode.com/problems/find-longest-special-substring-that-occurs-thrice-i
2981. Find Longest Special Substring That Occurs Thrice I
Medium
You are given a string s that consists of lowercase English letters.
A string is called special if it is made up of only a single character. For example, the string "abc" is not special, whereas the strings "ddd", "zz", and "f" are special.