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 […]
Contains Duplicate – Leetcode #217
Problem statement This problem is from Leetcode – Contains Duplicate. Problem statement is given below: Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. Solution Approach-1 Lets try a simple nested for loops approach. For each integer iterate the […]
Majority Element – Leetcode #169
Problem Statement This problem is from Leetcode – Majority Element. Given an array nums of size n, return the majority element. The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array. Solution This problem can easily be solved by […]
Single Number – Leetcode #136
Problem Statement This problem is from Leetcode – Single Number. The problem statement is as below: Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. You must implement a solution with a linear runtime complexity and use only constant extra space. Solution This problem is asking us to figure out […]
Valid Palindrome – Leetcode #125
Problem Statement This problem is from Leetcode – Valid Palindrome. The problem statement is as below: A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if […]
Merge Sorted Array – Leetcode #88
Problem Statement This problem is from Leetcode – Merge Sorted Array. The problem statement is as below: You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. Merge nums1 and nums2 into a single array sorted […]
Length of Last Word – Leetcode #58
Problem Statement This problem is from Leetcode – Length of Last Word. The problem statement is as below: Given a string s consisting of words and spaces, return the length of the last word in the string Solution Approach-1 Lets try the most obvious approach first, by using split() Lets see the code sample below: […]
Search Insert Position – Leetcode #35
Problem Statement This problem is from Leetcode – Search Insert Position. The problem statement is given below: Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Solution This problem is […]
Best Time to Buy and Sell Stock – Leetcode #121
Problem Statement This problem is from Leetcode – Best Time to Buy and Sell Stock. The problem statement is as below: You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock […]