Skip to content

Instantly share code, notes, and snippets.

View coderRohan123's full-sized avatar
🎯
Focusing

Rohan Mallick coderRohan123

🎯
Focusing
View GitHub Profile
@coderRohan123
coderRohan123 / Leaf Similarity Check.md
Created January 9, 2024 03:49
The code snippet checks if the leaf values of two binary trees are similar. It collects the leaf values of each tree and compares them using the equals() method.

Leaf Similarity Check

Preview:
public class Solution {
    public boolean leafSimilar(TreeNode root1, TreeNode root2) {
        List<Integer> leafValues1 = new ArrayList<>();
        List<Integer> leafValues2 = new ArrayList<>();
        
        collectLeafValues(root1, leafValues1);
        collectLeafValues(root2, leafValues2);
@coderRohan123
coderRohan123 / Range Sum of Binary Search Tree.md
Created January 8, 2024 13:51
This code snippet defines a class Solution with a method rangeSumBST that calculates the sum of all node values in a binary search tree (BST) that fall within a given range (low to high). It uses a depth-first search (DFS) algorithm

Range Sum of Binary Search Tree

Preview:
# Python3 Solution
class Solution:
    def rangeSumBST(self, root: TreeNode, low: int, high: int) -> int:
        def dfs(node):
            if not node:
                return
            if low <= node.val <= high:
@coderRohan123
coderRohan123 / Job Scheduling with Maximum Profit.md
Created January 6, 2024 15:48
The code snippet implements a job scheduling algorithm that maximizes profit. It takes in arrays of start times, end times, and profits for each job, and returns the maximum profit that can be obtained by scheduling a subset of jobs without overlapping time intervals.

Job Scheduling and Profit Calculation

Preview:
class Solution {
    public int jobScheduling(int[] startTime, int[] endTime, int[] profit) {
        int numJobs = profit.length; // Number of jobs
        Job[] jobs = new Job[numJobs];
​
        for (int i = 0; i < numJobs; ++i) {
            jobs[i] = new Job(endTime[i], startTime[i], profit[i]);
@coderRohan123
coderRohan123 / Job Scheduling with Maximum Profit.md
Created January 6, 2024 15:46
The code snippet implements a solution to a job scheduling problem. It sorts the jobs based on their start times, then uses dynamic programming to calculate the maximum profit that can be obtained by scheduling the jobs in a non-overlapping manner.

Job Scheduling with Maximum Profit

Preview:
import bisect

class Solution:
    def jobScheduling(self, s: List[int], e: List[int], p: List[int]) -> int:
        n = len(s)
        st = [(s[i], i) for i in range(n)]
        st.sort()
@coderRohan123
coderRohan123 / Length of Longest Increasing Subsequence.md
Created January 5, 2024 15:41
The code snippet implements the "lengthOfLIS" method, which calculates the length of the longest increasing subsequence in an array of integers. It uses dynamic programming and binary search to efficiently find the length.

Length of Longest Increasing Subsequence

Preview:
import java.util.Arrays;
​
class Solution {
    public int lengthOfLIS(int[] nums) {
        int[] temp = new int[nums.length];
        int len = 1;
@coderRohan123
coderRohan123 / Longest Increasing Subsequence Length using Dynamic Programming.md
Created January 5, 2024 15:39
This code snippet finds the length of the longest increasing subsequence in a given list of integers using dynamic programming. It initializes an array `dp` with all elements set to 1, then iterates through the list to update `dp` based on

Longest Increasing Subsequence Length using Dynamic Programming

Preview:
class Solution:
    def lengthOfLIS(self, nums: List[int]) -> int:
        if not nums:
            return 0
        
        n = len(nums)
        dp = [1] * n
@coderRohan123
coderRohan123 / Minimum Operations to Make Array Elements Equal.md
Created January 4, 2024 13:55
This code snippet calculates the minimum number of operations needed to make all elements in the given list have a frequency that is a multiple of 3. It returns -1 if it is not possible.

Minimum Operations to Make Array Elements Equal

Preview:
class Solution:
    def minOperations(self, nums: List[int]) -> int:
        counter = Counter(nums)
        ans = 0
        for c in counter.values():
            if c == 1: 
                return -1
@coderRohan123
coderRohan123 / Min Operations for Unique Triplets.md
Created January 4, 2024 13:53
This code snippet takes an array of integers as input and calculates the minimum number of operations required to make all elements in the array equal. It uses a HashMap to count the frequency of each number, and then iterates through the counts to calculate the minimum

Min Operations for Unique Triplets

Preview:
class Solution {
    public int minOperations(int[] nums) {
        var counter = new HashMap<Integer, Integer>();
        for (int num: nums) {
            counter.put(num, counter.getOrDefault(num, 0) + 1);
        }
        int ans = 0;
@coderRohan123
coderRohan123 / Counting Beams in a List.md
Created January 3, 2024 12:00
Anti-theft security devices are activated inside a bank. You are given a 0-indexed binary string array bank representing the floor plan of the bank, which is an m x n 2D matrix. bank[i] represents the ith row, consisting of '0's and '1's. '0' means the cell is empty, while'1' means the cell has a security device

Counting Beams in a List

Preview:
class Solution:
    def numberOfBeams(self, bank: List[str]) -> int:
        prev=res=0
        for s in bank[::-1]:
            oneCount=s.count('1')
            res+=prev*oneCount
            prev=oneCount or prev
@coderRohan123
coderRohan123 / Count number of beams in a bank..md
Created January 3, 2024 11:59
Anti-theft security devices are activated inside a bank. You are given a 0-indexed binary string array bank representing the floor plan of the bank, which is an m x n 2D matrix. bank[i] represents the ith row, consisting of '0's and '1's. '0' means the cell is empty, while'1' means the cell has a security device. There is one laser beam between …

Count number of beams in a bank.

Preview:
import java.util.ArrayList;

class Solution {
    public int numberOfBeams(String[] bank) {
        ArrayList<Integer> list = new ArrayList<>();

        for(String floor : bank){