Posts

Leetcode 2594. Minimum Time to Repair Cars explained clearly

When solving problems efficiently, choosing the right strategy makes all the difference. A common comparison in programming is Binary Search vs. Brute Force (or the Naïve approach ). While brute force might seem straightforward, it often becomes impractical for large inputs. Binary search, on the other hand, is a game-changer. Let's break it down in simple terms. 1. Understanding Brute Force (Naïve Approach) Imagine you have a book with 1,000 pages and you are looking for a particular word. If you start from page 1 and check each page one by one, that’s the brute force approach . This method works but is slow because you have to go through every single page before finding what you need. In coding, brute force means trying all possible solutions until you find the correct one. While this approach is easy to understand, it is very slow when the number of possibilities is huge. Example: Car Repair Problem Let’s say you have mechanics who can repair cars, and you want to find ou...

Finding the Minimum Robbery Capability – A Smart Approach

Imagine a street with several houses, each containing a certain amount of money. A robber wants to steal money, but there’s a catch – he cannot rob two adjacent houses. The goal is to find the minimum possible maximum amount he needs to be capable of stealing while ensuring he robs at least k houses. Understanding the Problem We are given: nums: An array where nums[i] represents the money in the i-th house. k: The minimum number of houses the robber must steal from. We need to find the smallest possible "capability", meaning the highest amount the robber steals from any house in his plan. How Do We Solve This Efficiently? Since nums can be large (up to 10^5 houses), checking every combination would be too slow. Instead, we use Binary Search to efficiently find the answer. Steps to Solve Using Binary Search Set the Search Range The lowest capability (l) is the minimum house value. The highest capability (r) is the maximum house value. Binary Search for the Minimum Capability W...

Mastering the Difference Array Technique: A Simple Guide with Examples

Image
Introduction Have you ever encountered a problem where you need to update multiple ranges in an array efficiently? Updating each element one by one can be too slow , especially for large datasets. This is where the Difference Array Technique comes in! It allows us to perform range updates in O(1) time per update and compute the final array efficiently. In this blog, we’ll break it down with step-by-step explanations and visuals to make it super easy to understand. Understanding the Problem Let’s say we have an array: A = [ 0 , 0 , 0 , 0 , 0 ] A = [0, 0, 0, 0, 0] A = [ 0 , 0 , 0 , 0 , 0 ] We want to perform multiple operations like: 1️⃣ Add 10 to indices [1 to 3] 2️⃣ Add 5 to indices [2 to 4] 3️⃣ Add 20 to indices [0 to 2] A naive approach would be to loop through each range and update elements one by one , which takes O(n) per operation . For large arrays and multiple queries, this becomes very inefficient ! 🚨 What is the Difference Array? Instead of updating each ele...

Finding a Unique Binary String in Python

IntroductionImagine you are given a list of binary strings, and your task is to find a new binary string that is not present in the list. This might seem tricky at first, but Python makes it easy to solve. In this guide, we'll break down the problem step by step and understand how to implement a simple and efficient solution. Understanding the ProblemYou are given a list of n binary strings, each of length n. Your goal is to find a new n-bit binary string that is not present in the list. Example:nums = ["00", "01", "10"] Output: "11" (since "11" is not in the list) There can be multiple valid answers. The key is to ensure that the generated binary string is unique. here is my approach to solve  from typing import List class Solution :     def findDifferentBinaryString ( self , nums : List[ str ]) -> str :         n = len (nums)         num_set = set (nums)         for i in range ( 2 **n):     ...

What is simple linear regression

 Linear regression is a machine learning algorithm more specifically in supervised learning algorithm that learns from labelled dataset and maps the data points in most optimized linear function which can be used prediction for data Linear regression  solve only regression porblems with numerical or continuous value based on dependent feature(target) and one or more independent features  For example house price dataset where dependent feature is price and independent features are area,sqft, bedroom  Linear regression equation is y= mx+c Where y= dependent variable  X=independent variables  M = intercept  C= slope  Intercept and slope also known as weight and bias in context of deep learning  What exactly do intercept and slope in LR? Intercept is value of dependent variable when independent variable is zero. It represents the starting point of value y when x=0 Ex:- use House price when intercept is 50,000 This means when a house is 0 sqft th...

K-Nearest Neighbors (KNN)

Image
  An easy-to-understand approach for regression and classification is K-Nearest Neighbors (KNN). A data point is classed according to the classification of its neighbors. KNN looks at the ‘K’ closest points (neighbors) to a data point and classifies it based on the majority class of these neighbors. For regression, it takes the average of the ‘K’ nearest points. Evaluation Metrics Classification : Accuracy, Precision, Recall, F1 Score. Regression : Mean Squared Error (MSE), R-squared. Applying with Sci-kit Learn We’ll use the Wine dataset again but this time with KNN. We’ll train the KNN model to classify the types of wine and evaluate its performance with classification metrics. Here are the steps we’ll follow. 1. Create and Train the KNN Model: A K-Nearest Neighbors (KNN) model is created with n_neighbors=3. This means the model looks at the three nearest neighbors of a data point to make a prediction. The model is trained (fitted) with the training data. During training, it does...

what is bert

Image
  BERT (Bidirectional Encoder Representations from Transformers) is a deep learning model developed by Google in 2018 that revolutionized natural language processing (NLP). It is based on the Transformer architecture and has significantly improved the state of the art in various NLP tasks, such as sentiment analysis, question answering, and named entity recognition. Key Concepts of BERT: 1.   Transformer Architecture :  BERT is built on the Transformer architecture, introduced by Vaswani et al. in 2017. The Transformer model relies on self-attention mechanisms to process input data in a parallelized manner, which is more efficient than traditional RNNs and LSTMs. 2. Bidirectional Training: Unlike previous models like GPT (which is unidirectional and processes text from left to right), BERT is bidirectional. This means BERT looks at both the left and right context simultaneously when processing words. This bidirectional approach allows BERT to capture richer contextual in...