Candy LeetCode Solution

Problem Statement: Candy LeetCode Solution: There are n children standing in a line. Each child is assigned a rating value given in the integer array ratings. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get more …

Binary Search LeetCode Solution

Problem Statement Binary Search LeetCode Solution says that – Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1. You must write an algorithm with O(log n) runtime complexity. Example 1: Input: nums = [-1,0,3,5,9,12], target …

Pairs of Songs With Total Durations Divisible by 60 LeetCode Solution

Problem Statement Pairs of Songs With Total Durations Divisible by 60 LeetCode Solution – Pairs of Songs With Total Durations Divisible by 60 LeetCode Solution says that – You are given a list of songs where the ith song has a duration of time[i] seconds. Return the number of pairs of songs for which …

Reverse Integer Leetcode Solution

Problem Statement Reverse Integer LeetCode Solution says that – 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. Assume the environment does not allow you to store 64-bit integers (signed or unsigned). Example 1: …

Palindrome Number LeetCode Solution

Problem Statement Palindrome Number LeetCode Solution says that – Given an integer x, return true if x is palindrome integer. An integer is a palindrome when it reads the same backward as forward. For example, 121 is a palindrome while 123 is not.   Example 1: Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right …

Score of Parenthesis LeetCode Solution

Problem Statement The score of Parenthesis LeetCode Solution says – Given a balanced parentheses string s and return the maximum score. The score of a balanced parenthesis string is based on the following rules: “()” has score 1. AB has score A + B, where A and B are balanced parenthesis strings. (A) has score 2 * A, where A is a …

LRU Cache Leetcode Solution

Problem Statement The LRU Cache LeetCode Solution – “LRU Cache” asks you to design a data structure that follows Least Recently Used (LRU) Cache We need to implement LRUCache class that has the following functions: LRUCache(int capacity): Initializes the LRU cache with positive size capacity. int get(int key): Return the value …

Fibonacci Number LeetCode Solution

Problem Statement Fibonacci Number LeetCode Solution – “Fibonacci Number” states that 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 …

Longest Palindromic Substring LeetCode Solution

Problem Statement The Longest Palindromic Substring LeetCode Solution – “Longest Palindromic Substring” states that You are Given a string s, return the longest palindromic substring in s. Note: A palindrome is a word that reads the same backward as forwards, e.g. madam. Example:   s = “babad” “bab” Explanation: All …

Reverse a stack without using extra space in O(n)

Problem Statement The problem “Reverse a stack without using extra space in O(n)” states that you are given a stack data structure. Reverse the given stack without using extra O(n) space. Example 5 4 3 2 1 1 2 3 4 5 80 60 10 20 20 10 60 80 …

Translate »