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 […]
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 […]