In this article, I explained about LRU Cache implementation with example. Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.put(key, value) - Set or insert the value …

# Implement Stack using Queues

Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Return whether the stack is empty. Example: MyStack stack = new MyStack(); stack.push(1); stack.push(2); stack.top(); // returns 2 stack.pop(); // returns 2 stack.empty(); // …

# Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.empty() -- Return whether the queue is empty. Example: MyQueue queue = new MyQueue(); queue.push(1); queue.push(2); queue.peek(); // returns 1 queue.pop(); // returns …

# First Missing Positive

Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2,0] Output: 3 Example 2: Input: [3,4,-1,1] Output: 2 Example 3: Input: [7,8,9,11,12] Output: 1 Note: Your algorithm should run in O(n) time and uses constant extra space. Solution With comments public class Solution { public int firstMissingPositive(int[] nums) { int n = …

# Find All Numbers Disappeared in an Array

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array. Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count …

# Find All Duplicates in an Array

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements that appear twice in this array. Could you do it without extra space and in O(n) runtime? Example: Input: [4,3,2,7,8,2,3,1] Output: [2,3] Approach When find a number i, flip the number at position i-1 to negative.If …

# How Many Numbers Are Smaller Than the Current Number

Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] < nums[i]. Return the answer in an array. Example 1: Input: nums = [8,1,2,2,3] Output: [4,0,1,1,3] Explanation: For nums[0]=8 there exist four smaller numbers than …

Continue reading How Many Numbers Are Smaller Than the Current Number

# Minimum Remove to Make Valid Parentheses

Introduction In this article, we will see about Minimum Remove to Make Valid Parentheses. We have to remove the minimum number of parentheses to make a string with valid parentheses. BACKGROUND Day 15 of 100 Days of Leetcode Programming Challenge. This problem tests our knowledge in the Stack data structure. LANGUAGE I took C# programming language …

# Alphabet Board Path

INTRODUCTION In this article, we will see about Alphabet Board Path. It is one of the interesting problem, find Alphabet Board Path. BACKGROUND Day 14 of 100 Days of Leetcode Programming Challenge. This is one of the top Leetcode Problem. This problem tests our logical skills. LANGUAGE I took C# programming language to solve this …

# How to Reorganize String?

INTRODUCTION In this article, we will see about How to Reorganize String? It is one of the interesting problem, Reorganizing string. Background Day 13 of 100 Days of Leetcode Programming Challenge. This is one of the top Leetcode Problem. This problem tests our knowledge in string manipulations and hashing. Related Problems Top K Frequent ElementsHow …

# How to Sort Characters By Frequency?

Introduction In this article, we will see about How to sort characters based upon frequency? Background Day 12 of 100 Days of Leetcode Programming Challenge. This is one of the top Leetcode Problem. This problem tests our knowledge in Map data structure and Sorting. Language I took C# programming language to solve this problem. Since …

# Maximum Depth of Binary Tree

In this article, I explain about how to find the Maximum Depth of Binary Tree which is also called as Height of Binary Tree. Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Note: A …

# Look and Say (Count and Say) Sequence

Day 10 Find the nth term in Count and Say Sequence. The Count and Say sequence is the sequence of below integers: "1" "11" "21" "1211" "111221" "312211" "13112221" "1113213211" "31131211131221" "13211311123113112211" "11131221133112132113212221" "3113112221232112111312211312113211" "1321132132111213122112311311222113111221131221" "11131221131211131231121113112221121321132132211331222113112211" "311311222113111231131112132112311321322112111312211312111322212311322113212221" For example: If you have "1", the next line is "11".If you have "11", the next line is …

# Find the Missing Number in an Array

In this article, I explained about how to find the missing number in an array. Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. Example 1: Input: [3,0,1] Output: 2 Example 2: Input: [9,6,4,2,3,5,7,0,1] Output: 8 Note:Your algorithm should run in linear run time …

# Squares of a Sorted Array

Day 8 Welcome to 100 Days of Leetcode Challenge. Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order. Example 1: Input: [-4,-1,0,3,10] Output: [0,1,9,16,100] Example 2: Input: [-7,-3,2,3,11] Output: [4,9,9,49,121] Note: 1 <= A.length <= 10000-10000 <= A[i] <= 10000A is sorted in non-decreasing order. …