Problem Statement You are given a string s representing an attendance record for a student where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters: The student is eligible for an attendance award if they meet both of the following criteria: Return […]
Detect Capital – Leetcode #520
This problem is from Leetcode – Detect Capital. Problem statement is as below: We define the usage of capitals in a word to be right when one of the following cases holds: Given a string word, return true if the usage of capitals in it is right. Solution To determine if the usage of capitals […]
Fibonacci Number – Leetcode #509
Problem Statement The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, F(0) = 0, F(1) = 1 F(n) = F(n – 1) + F(n – 2), for n > 1. Given n, […]
Relative Ranks – Leetcode #506
Problem Statement This problem is from Leetcode – Relative Ranks. You are given an integer array score of size n, where score[i] is the score of the ith athlete in a competition. All the scores are guaranteed to be unique. The athletes are placed based on their scores, where the 1st place athlete has the […]
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 maximum number 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 performance of this […]
Number Complement – Leetcode #476
Problem Statement This problem is from Leetcode – Number Complement. The problem statement is as below: The complement of an integer is the integer you get when you flip all the 0’s to 1’s and all the 1’s to 0’s in its binary representation. Given an integer num, return its complement. Solution This problem is […]
Assign Cookies – Leetcode #455
Problem Statement This problem is from Leetcode – Assign Cookies. The problem statement is as below: Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor g[i], which is the minimum size of a […]
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 nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums. Example 1: Input: nums = [4,3,2,7,8,2,3,1] Output: […]
Arranging Coins – Leetcode #441
Problem Statement This problem is from Leetcode – Arranging Coins. The problem statement is as below: You have n coins and you want to build a staircase with these coins. The staircase consists of k rows where the ith row has exactly i coins. The last row of the staircase may be incomplete. Given the […]
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 s, return the number of segments in the string. A segment is defined to be a contiguous sequence of non-space characters. Solution Approach – 1 We can simply use a StringTokenizer to […]
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 is given below: Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 – 1], then return 0. Solution Approach – 1 We need […]
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 nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. Solution Approach – 1 Lets try a simple solution first. We can simply add elements […]
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 […]
Missing Number – Leetcode #268
Problem Statement This problem is from Leetcode – Missing Number. The problem statement is as below: Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array. Solution Approach-1 Lets try a simple for loop approach first. See the code […]