Min Cost Climbing Stairs

Problem You are given an integer array cost where cost[i] is the cost of ith step on a staircase. Once you pay the cost, you can either climb one or two steps. You can either start from the step with index 0, or the step with index 1. Return the minimum cost to reach the top of the floor. Examples Example 1: Input: cost = [10,15,20] Output: 15 Explanation: You will start at index 1. - Pay 15 and climb two steps to reach the top. The total cost is 15. ...

Unique Email Addresses Problem

Problem Every valid email consists of a local name and a domain name, separated by the '@' sign. Besides lowercase letters, the email may contain one or more '.' or '+'. For example, in "[email protected]", "alice" is the local name, and "leetcode.com" is the domain name. If you add periods '.' between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name. Note that this rule does not apply to domain names. ...

Length of Last Word

Length of Last Word Problem Given a string s consisting of some words separated by some number of spaces, return the length of the last word in the string. A word is a maximal substring consisting of non-space characters only. Examples Example 1: Input: s = "Hello World" Output: 5 Explanation: The last word is "World" with length 5. ...

Search Insert Position in sorted array

Problem Given a sorted array 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. You may assume no duplicates in the array. You must write an algorithm with O(log n) runtime complexity. Examples Example 1: Input: nums = [1,3,5,6], target = 5 Output: 2 ...

Remove Linked List Elements Problem

Problem Remove all elements from a linked list of integers that have value val. Example Example 1: Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 Return: 1 --> 2 --> 3 --> 4 --> 5 ...

Min Stack Problem

Problem Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the MinStack class: MinStack() initializes the stack object. void push(int val) pushes the element val onto the stack. void pop() removes the element on the top of the stack. int top() gets the top element of the stack. int getMin() retrieves the minimum element in the stack. You must implement a solution with O(1) aka constant time complexity for each function. ...

Happy Number Problem

Happy Number Problem Write an algorithm to determine if a number is “happy”. Definition A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits. Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy. Return true if n is a happy number, and false if not. ...

Replace Elements with Greatest Element on Right Side

Problem Given an array arr, replace every element in that array with the greatest element among the elements to its right, and replace the last element with -1. After doing so, return the array. Examples Example 1: Input: arr = [17,18,5,4,6,1] Output: [18,6,6,6,1,-1] Explanation: - index 0 → the greatest element to the right of index 0 is index 1 (18). - index 1 → the greatest element to the right of index 1 is index 4 (6). - index 2 → the greatest element to the right of index 2 is index 4 (6). - index 3 → the greatest element to the right of index 3 is index 4 (6). - index 4 → the greatest element to the right of index 4 is index 5 (1). - index 5 → there are no elements to the right of index 5, so we put -1. ...

Invert Binary Tree Problem

Problem Given the root of a binary tree, invert the tree, and return its root. OR Change a tree so that the roles of the left and right pointers are swapped at every node. Example Example 1 Given binary tree 1 / \ 2 3 / \ / \ 4 5 6 7 ...

First Unique Character in a String

Find First Unique Character in a String Problem Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1. Examples Example 1: Input: s = "leetcode" Output: 0 Example 2: Input: s = "loveleetcode" Output: 2 ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy