Stone Game 2 Problem

Problem Alice and Bob continue their games with piles of stones. There are a number of piles arranged in a row, and each pile has a positive integer number of stones piles[i]. The objective of the game is to end with the most stones. Alice and Bob take turns, with Alice starting first. Initially, M = 1. On each player’s turn, that player can take all the stones in the first X remaining piles, where 1 <= X <= 2M. Then, we set M = max(M, X). ...

Generate nth ugly number

Problem Given an integer n, return the nth ugly number. Ugly Number Definition Examples Example 1: Input: n = 10 Output: 12 Explanation: [1, 2, 3, 4, 5, 6, 8, 9, 10, 12] is the sequence of the first 10 ugly numbers. Example 2: Input: n = 1 Output: 1 Explanation: 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5. ...

Palindrome Partitioning Problem

Problem Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. Definition Palindrome Definition Examples Example 1: Input: s = "aab" Output: [ ["a","a","b"],["aa","b"] ] Example 2: Input: s = "a" Output: [ ["a"] ] ...

Boats to Save People Problem

Problem You are given an array people where people[i] is the weight of the ith person, and an infinite number of boats where each boat can carry a maximum weight of limit. Each boat carries at most two people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person. Examples Example 1: ...

Find Duplicate Number in array containing n+1 numbers between 1 and n

Problem Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. There is only one repeated number in nums, return this repeated number. You must solve the problem without modifying the array nums and uses only constant extra space. OR You are given an array of length n + 1 whose elements belong to the set {1, 2, ..., n}. By the pigeonhole principle, there must be a duplicate. Find it in linear time and space. ...

Candy distribution Problem

Problem 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 candies than their neighbors. Return the minimum number of candies you need to have to distribute the candies to the children. ...

Binary Tree Level Order Traversal - Level by Level

Problem Given the root of a binary tree, return the level order traversal of its nodes’ values. (i.e., from left to right, level by level). NOTE : This problem is very similar to - Create linked lists of all the nodes at each depth for a Binary Tree Examples Example 1: 3 / \ 9 20 / \ 15 7 ...

Binary Tree Traversal - Level Order

Problem Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). Examples Example 1: Given binary tree {3, 9, 20, #, #, 15, 7}, 3 / \ 9 20 / \ 15 7 ...

Merge Two Sorted Linked Lists

Problem Given two sorted Linked Lists, how to merge them into the third list in sorted order? Example Example 1: Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4] Merged List: 1 -> 1 -> 2 -> 3 -> 4 -> 4 ...

Reverse Words in a String

Problem Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. Return a string of the words in reverse order concatenated by a single space. Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces. ...

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