Learnitweb

Category: Java programming question

  • LeetCode 997: Find the Town Judge

    Problem Statement In a town of n people labeled 1 to n: You are given an array trust where trust[i] = [a, b] means person a trusts person b. Return the label of the town judge, or -1 if no judge exists. Example: Approach (Single-Array Optimized) Idea: Steps: Why it works: Java Code Solution Dry…

  • LeetCode 367: Valid Perfect Square

    Problem Statement Given a positive integer num, determine if it is a perfect square. Return true if num is a perfect square, otherwise return false. You cannot use built-in square root functions. Example 1: Example 2: Constraints: Approach to Solve the Problem (Simple Language) Idea: Steps: Why binary search works: Important Note: Java Code Solution…

  • LeetCode 1232: Check If It Is a Straight Line

    Problem Statement You are given an array coordinates where each element is a pair [x, y] representing a point on a 2D plane. Return true if these points all lie on a straight line in the XY-plane, otherwise return false. Example 1: Example 2: Constraints: Approach to Solve the Problem (Simple Language) Idea: Steps in…

  • LeetCode 993: Cousins in Binary Tree

    Problem Statement Given a binary tree and two integers x and y, determine if the nodes with values x and y are cousins. Definition of cousins: Example 1: Example 2: Constraints: Approach to Solve the Problem (Simple Language) We need to check two conditions for nodes x and y: Steps: Why DFS works: Java Code…

  • LeetCode 169: Majority Element

    Problem Statement Given an array nums of size n, find the majority element. The majority element is the element that appears more than ⌊n / 2⌋ times. You can assume that the majority element always exists in the array. Example 1: Example 2: Constraints: Approach to Solve the Problem (Simple Language) There are multiple ways…

  • LeetCode 387: First Unique Character in a String

    Problem Statement Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1. Example 1: Example 2: Constraints: Approach (HashMap for General Characters) Advantages: Java Code Solution (HashMap) Dry Run Example Input: Step-by-step Execution: Index Char Count Is Unique? 0 a 2 No 1…

  • LeetCode 476: Number Complement

    Problem Statement Given a positive integer num, find its complement. The complement of an integer is defined as flipping all the bits in its binary representation (0 → 1, 1 → 0). Example: Constraints: Approach to Solve the Problem (Simple Language) Why XOR works: Java Code Solution Dry Run Example Input: Step-by-step Execution: Textual Diagram…

  • LeetCode 383: Ransom Note

    Problem Statement You are given two strings: You need to check if you can construct the ransomNote using the letters from magazine. Each letter in magazine can only be used once. Example: Constraints: Approach to Solve the Problem (Simple Language) Optimized Idea: Steps in short: Java Code Solution Dry Run Example Input: Step-by-step Execution: Array…

  • LeetCode 771: Jewels and Stones

    Problem Statement You are given two strings: You want to know how many of the stones you have are also jewels. Example: Constraints: Approach to Solve the Problem (Simple Language) Optimized idea: Steps in short: This approach ensures fast lookup and is simple to understand. Java Code Solution Dry Run Example Input: Step-by-step execution: Stone…

  • First Bad Version

    Problem Statement You are the product manager, and your team is developing a new product.Unfortunately, one of the versions released is bad, and all the versions after it are also bad. You have a function isBadVersion(version) that returns: Your task is to find the first bad version among n versions, using the minimum number of…