Compare Version Numbers Problem

Problem Compare two version numbers version1 and version2. If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0. You may assume that the version strings are non-empty and contain only digits and the . character. The . character does not represent a decimal point and is used to separate number sequences. Here is an example of version numbers ordering: 0.1 < 1.1 < 1.2 < 13.37 ...

ZigZag Conversion Problem

Problem The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R ...

Next Greater Element 3 - Find smallest number larger than given number using same digits

Problem Given a positive integer n, find the smallest integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive integer exists, return -1. Note that the returned integer should fit in 32-bit integer, if there is a valid answer but it does not fit in 32-bit integer, return -1. Examples Example 1: Input: n = 12 Output: 21 ...

Largest Number From Given Numbers

Problem Given a list of non-negative integers nums, arrange them such that they form the largest number and return it. Since the result may be very large, so you need to return a string instead of an integer. Example Example 1: Input: nums = [10,2] Output: "210" Example 2: Input: nums = [3,30,34,5,9] Output: "9534330" ...

Maximum Points You Can Obtain from Cards Problem

Problem There are several cards arranged in a row, and each card has an associated number of points. The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain. ...

Super Ugly Number Problem

Problem A super ugly number is a positive integer whose prime factors are in the array primes. Given an integer n and an array of integers primes, return the nth super ugly number. The nth super ugly number is guaranteed to fit in a 32-bit signed integer. Examples Example 1: Input: n = 12, primes = [2,7,13,19] Output: 32 Explanation: [1,2,4,7,8,13,14,16,19,26,28,32] is the sequence of the first 12 super ugly numbers given primes = [2,7,13,19]. ...

Random Flip Matrix Problem

Problem There is an m x n binary grid matrix with all the values set 0 initially. Design an algorithm to randomly pick an index (i, j) where matrix[i][j] == 0 and flips it to 1. All the indices (i, j) where matrix[i][j] == 0 should be equally likely to be returned. Optimize your algorithm to minimize the number of calls made to the built-in random function of your language and optimize the time and space complexity. ...

Random Pick Index Problem

Problem Given an integer array nums with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array. Implement the Solution class: Solution(int[] nums) Initializes the object with the array nums. int pick(int target) Picks a random index i from nums where nums[i] == target. If there are multiple valid i’s, then each index should have an equal probability of returning. Examples Example 1: ...

Blum Floyd Pratt Rivest Tarjan Algorithm

Algorithm to find kth largest element from an unsorted array in linear time. Algorithm If the number of elements in an array is large .Divide the array into arrays each with 5 elements ⇨ n/5 arrays. Compute Median of all arrays of 5 elements. This can be done by sorting each group and taking middle element. This results in time complexity of n/5 * 5log5. (Sorting each group takes 5log5, and we have n/5 arrays) All the computed medians from each sub-array are then collected into a new, smaller array. Now, compute median of this array Use this Median as Pivot in QuickSelect Median > half of these n/5 group medians. So median > n/10 medians. ...

Reverse Linked List 2 - between m and n

Problem Reverse a linked list from position m to n. Do it in-place and in one-pass. OR Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list. Examples Example 1: --- title: Input --- graph LR A1[1] --> B2[2]:::reversed --> C3[3]:::reversed --> D4[4]:::reversed --> E5[5] classDef reversed fill:#ffcc00,stroke:#000,stroke-width:2px; ...

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