PROBLEM STATEMENT This question is from Leetcode – String to Integer (atoi). The problem statement in short is given below: Implement a function that converts a string to a 32-bit signed integer. SOLUTION You can check out the code from Github here: String to Integer (atoi) – First Approach PERFORMANCE ANALYSIS RUNTIME 4 ms | […]
Student Attendance Record – Leetcode #551
PROBLEM STATEMENT This problem is from Leetcode Student Attendance Record. Problem statement in short is given below: Given a record string with ‘A’ (absent), ‘L’ (late), and ‘P’ (present), return true if there are fewer than 2 absences and no 3+ consecutive lates; else false. SOLUTION We can solve this problem in one pass. In […]
Detect Capital – Leetcode #520
This problem is from Leetcode – Detect Capital. Problem statement in short is as below: Return true if a word is all uppercase, all lowercase, or only the first letter is uppercase; otherwise, false. Solution To determine if the usage of capitals in a given word is correct according to the specified rules, we can […]
Fibonacci Number – Leetcode #509
Problem Statement This problem is from Leetcode – Fibonacci numbers. The problem statement in short is given below: Compute the nth Fibonacci number where F(0) = 0, F(1) = 1, and F(n) = F(n-1) + F(n-2) for n > 1. Solution: This problem is simple enough. Lets take the iterative approach first. See the code […]
Relative Ranks – Leetcode #506
Problem Statement This problem is from Leetcode – Relative Ranks. The problem statement in short is given below: Given an array of unique athlete scores, assign ranks where the top three receive “Gold Medal”, “Silver Medal”, “Bronze Medal”, and others receive their numeric rank. Return ranks in original order. Solution To solve the problem of […]
Max Consecutive Ones – Leetcode #485
Problem Statement This problem is from Leetcode – Max Consecutive Ones. The problem statement is as below: Given a binary array nums, return the length of the longest run of consecutive 1’s in the array. Solution Initialize variables Iterate Though the Array You can checkout the code from Github here: Max Consecutive Ones. See the […]
Number Complement – Leetcode #476
Problem Statement This problem is from Leetcode – Number Complement. The problem statement in short is as below: Given an integer num, return its binary complement by flipping all 0s to 1s and all 1s to 0s in its binary representation. Solution This problem is simple enough. We first compute the binary of the given […]
Assign Cookies – Leetcode #455
Problem Statement This problem is from Leetcode – Assign Cookies. The problem statement in short is as below: Given two arrays g (children’s greed factors) and s (cookie sizes), assign each child at most one cookie such that s[j] >= g[i] to satisfy the child. Return the maximum number of content children. Solution You can […]
Find All Numbers Disappeared in an Array – Leetcode #448
Problem Statement This problem is from Leetcode – Find All Numbers Disappeared in an Array. Given an array nums of n integers, where each nums[i] is in the range [1, n], find and return all integers in the range [1, n] that are missing from nums. Solution The solution to this problem is simple enough. […]
Arranging Coins – Leetcode #441
Problem Statement This problem is from Leetcode – Arranging Coins. The problem statement in short is as below: Given n coins, return the number of complete rows in a staircase where the ith row has i coins. Solution This problem is easy enough. In awhile loop simply keep subtracting the row count from coins left. […]
Number of Segments in a String – Leetcode #434
Problem Statement This problem is from Leetcode – Number of Segments in a String. The problem statement is given below: Given a string, return the number of segments separated by spaces. Solution Approach – 1 We can simply use a StringTokenizer to solve this problem. See the code sample below: You can checkout the code […]
Third Maximum Number – Leetcode #414
Problem Statement This problem is from Leetcode – Third Maximum Number. The problem statement is as below: Given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number. Solution Let find the max value first by looping through the array. Now […]
Reverse Integer – Leetcode #7
Problem Statement This problem is from Leetcode – Reverse Integer. The problem statement in short is given below: Given a signed 32-bit integer x, return its digits reversed. If the reversed integer overflows the 32-bit signed integer range, return 0. Solution Approach – 1 We need to extract individual numbers out of the given input […]
Median of Two Sorted Arrays – Leetcode #4
Problem Statement This problem is from Leetcode – Median of Two Sorted Arrays. The problem statement is given below: Given two sorted arrays of sizes m and n, return the median of the combined sorted array. Solution Approach – 1 Lets try a simple solution first. We can simply add elements from first and second […]
Longest Substring Without Repeating Characters – Leetcode #3
Problem Statement This problem is from Leetcode – Longest Substring Without Repeating Characters. The problem statement is as below: Given a string s, find the length of the longest substring without repeating characters. Solution Approach – 1 As the window is moving right, keep computing max sub-string length in a max variable. When the for […]