Design and implement a data structure for a Least Frequently Used (LFU) cache. Implement the LFUCache class: Example 1: Input [“LFUCache”, “put”, “put”, “get”, “put”, “get”, “get”, “put”, “get”, “get”, “get”] [[2], [1, 1], [2, 2], [1], [3, 3], [2], [3], [4, 4], [1], [3], [4]] Output [null, null, null, 1, null, -1, 3, null, -1, 3, 4] This… Continue reading
Category: Algorithm
Program to find power of a number
In computer programming, power is denoted by symbol ^ . To calculate the power y of a number x, we use x^y. For example Let’s say x = 2, y = 5 Output of x^y = 32. Below is the implementation of the same In the above program, the function pow() is used to calculate… Continue reading
Program to find best possible denomination of input money
In this program, we will be finding possible denomination of money input from user. In this case we are assuming denomination as 1,5,10,25. For example if input money is 65 then denomination will be 25*2,10*1,5*1. Since we are trying to get best possible denomination such that we have least coins, so will store the denomination… Continue reading
Infix to Postfix Expression Using Stack
In this post, we will see infix to postfix expression.
Maximum of all Subarrays of size K | LeetCode #239
You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window of size K Example 1: Input:… Continue reading
Program to move all zeroes to the end
Given an integer array nums, move all 0’s to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array. Example 1: Input: nums = [0,1,0,3,12]Output: [1,3,12,0,0]Example 2: Input: nums = [0]Output: [0]
Program to reverse word in a string
Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: s = “Let’s take LeetCode contest”Output: “s’teL ekat edoCteeL tsetnoc”Example 2: Input: s = “God Ding”Output: “doG gniD” Solution: string reverseWords(string s) {int n = s.length();if(n==0){return s;} };