formatted_conversations
stringlengths
1.13k
138k
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a positive integer n, find and return the longest distance between any two adjacent 1's in the binary representation of n. If there are no two adjacent 1's, return 0. Tw...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a word, you need to judge whether the usage of capitals in it is right or not. We define the usage of capitals in a word to be right when one of the following cases h...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactly 1. Now, given an integer array, you need to find the ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = "hello", return "holle". Example 2: Given s = "leetcode", retu...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a string s consisting only of letters 'a' and 'b'. In a single step you can remove one palindromic subsequence from s. Return the minimum number of steps to make the giv...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an array of unique integers salary where salary[i] is the salary of the employee i. Return the average salary of employees excluding the minimum and maximum salary.   Ex...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be construct...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.We have a collection of stones, each stone has a positive integer weight. Each turn, we choose the two heaviest stones and smash them together.  Suppose the stones have weight...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given two integer arrays startTime and endTime and given an integer queryTime. The ith student started doing their homework at the time startTime[i] and finished it at time en...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character. Note that after backspacing an empty text, the ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an array of integers nums, write a method that returns the "pivot" index of this array. We define the pivot index as the index where the sum of the numbers to the left ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Balanced strings are those who have equal quantity of 'L' and 'R' characters. Given a balanced string s split it in the maximum amount of balanced strings. Return the maximum ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used. Note: All letters in hexadecimal (a-f) must be in ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Example: Input: [-2,1,-3,4,-1,2,1,-...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an integer, write a function to determine if it is a power of two. Example 1: Input: 1 Output: true Explanation: 20 = 1 Example 2: Input: 16 Output: true Explana...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an array A of integers, return true if and only if it is a valid mountain array. Recall that A is a mountain array if and only if: A.length >= 3 There exists some i wit...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Alice and Bob take turns playing a game, with Alice starting first. Initially, there is a number N on the chalkboard.  On each player's turn, that player makes a move consisti...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a square matrix mat, return the sum of the matrix diagonals. Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diago...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.You are given an array nums of non-negative integers. nums is considered special if there exists a number x such that there are exactly x numbers in nums that are greater than...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Tic-tac-toe is played by two players A and B on a 3 x 3 grid. Here are the rules of Tic-Tac-Toe: Players take turns placing characters into empty squares (" "). The first pla...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an array arr of integers, check if there exists two integers N and M such that N is the double of M ( i.e. N = 2 * M). More formally check if there exists two indices i ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a list of dominoes, dominoes[i] = [a, b] is equivalent to dominoes[j] = [c, d] if and only if either (a==c and b==d), or (a==d and b==c) - that is, one domino can be rot...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of th...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.You are given a string representing an attendance record for a student. The record only contains the following three characters: 'A' : Absent. 'L' : Late. 'P' : Present. ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a string s containing only lower case English letters and the '?' character, convert all the '?' characters into lower case letters such that the final string does not c...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a non-negative integer. Since the return type is an integer, the decimal digi...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indices, and for each string, we delete all the character...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. Example: Input: 38 Output: 2 Explanation: The process is like: 3 + 8 =...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.You are given an array A of strings. A move onto S consists of swapping any two even indexed characters of S, or any two odd indexed characters of S. Two strings S and T are s...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given two strings s and t which consist of only lowercase letters. String t is generated by random shuffling string s and then add one more letter at a random position. Find...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place. The mov...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.We have n chips, where the position of the ith chip is position[i]. We need to move all the chips to the same position. In one step, we can change the position of the ith chip...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i fro...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an array of integers arr, write a function that returns true if and only if the number of occurrences of each value in the array is unique.   Example 1: Input: arr = [1,...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.For two strings s and t, we say "t divides s" if and only if s = t + ... + t  (t concatenated with itself 1 or more times) Given two strings str1 and str2, return the largest ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given two non-negative integers low and high. Return the count of odd numbers between low and high (inclusive).   Example 1: Input: low = 3, high = 7 Output: 3 Explanation: Th...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a non-negative integer num, return the number of steps to reduce it to zero. If the current number is even, you have to divide it by 2, otherwise, you have to subtract 1...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string c...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an array nums of integers, return how many of them contain an even number of digits.   Example 1: Input: nums = [12,345,2,6,7896] Output: 2 Explanation: 12 contains 2 d...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (i.e., buy one and sell...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an integer n and an integer start. Define an array nums where nums[i] = start + 2*i (0-indexed) and n == nums.length. Return the bitwise XOR of all elements of nums.   E...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given two strings A and B of lowercase letters, return true if you can swap two letters in A so the result is equal to B, otherwise, return false. Swapping letters is defined ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.The Leetcode file system keeps a log each time some user performs a change folder operation. The operations are described below: "../" : Move to the parent folder of the curr...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4] Outpu...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a valid (IPv4) IP address, return a defanged version of that IP address. A defanged IP address replaces every period "." with "[.]".   Example 1: Input: address = "1.1.1...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given two integer arrays of equal length target and arr. In one step, you can select any non-empty sub-array of arr and reverse it. You are allowed to make any number of steps...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a date, return the corresponding day of the week for that date. The input is given as three integers representing the day, month and year respectively. Return the answer...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an integer array arr, return true if there are three consecutive odd numbers in the array. Otherwise, return false.   Example 1: Input: arr = [2,6,4,1] Output: false Exp...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase lette...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.In a deck of cards, each card has an integer written on it. Return true if and only if you can choose X >= 2 such that it is possible to split the entire deck into 1 or more g...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a string s, the power of the string is the maximum length of a non-empty substring that contains only one unique character. Return the power of the string.   Example 1: ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subseq...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an array of positive integers arr, calculate the sum of all possible odd-length subarrays. A subarray is a contiguous subsequence of the array. Return the sum of all odd...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.An axis-aligned rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) is the coordinate of its bottom-left corner, and (x2, y2) is the coordinate of its top-righ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.You are given an array of strings words and a string chars. A string is good if it can be formed by characters from chars (each character can only be used once). Return the su...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Students are asked to stand in non-decreasing order of heights for an annual photo. Return the minimum number of students that must move in order for all students to be standi...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1. Examp...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes t...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times. Return the element repeated N times.   Example 1: Input: [1,2...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Every email consists of a local name and a domain name, separated by the @ sign. For example, in alice@leetcode.com, alice is the local name, and leetcode.com is the domain na...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Find the nth digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... Note: n is positive and will fit within the range of a 32-bit signed integer (n < ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given two integer arrays arr1 and arr2, and the integer d, return the distance value between the two arrays. The distance value is defined as the number of elements arr1[i] su...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only. We would like to convert the sentence to "Goat Latin"...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference betwe...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.The Tribonacci sequence Tn is defined as follows:  T0 = 0, T1 = 1, T2 = 1, and Tn+3 = Tn + Tn+1 + Tn+2 for n >= 0. Given n, return the value of Tn.   Example 1: Input: n = 4 O...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element. We define an array is non-decreasing if array[i] ho...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a sentence that consists of some words separated by a single space, and a searchWord. You have to check if searchWord is a prefix of any word in sentence. Return the ind...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 2. 11 3. 21 4. 1211 5. 111221 1 is read off as "one...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements. Your task is to find the smalle...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray). Example 1: Input: [1,3,5,4,7] Output: 3 Explanation: The longe...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, calculate the Hamming distance...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explanation: 3! = 6, no trailing zero. Example 2: Input: 5 Output: 1 Explan...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example: Given a = 1 and b = 2, return 3. Credits:Special thanks to @fujiaoz...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Input: ["flower"...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a rows x cols matrix mat, where mat[i][j] is either 0 or 1, return the number of special positions in mat. A position (i,j) is called special if mat[i][j] == 1 and all o...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.In a list of songs, the i-th song has a duration of time[i] seconds.  Return the number of pairs of songs for which their total duration in seconds is divisible by 60.  Formal...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an integer n, add a dot (".") as the thousands separator and return it in string format.   Example 1: Input: n = 987 Output: "987" Example 2: Input: n = 1234 Output: "1...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a string s of zeros and ones, return the maximum score after splitting the string into two non-empty substrings (i.e. left substring and right substring). The score afte...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome. Example 1: Input: "aba" Output: True Example 2: Input: "a...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an array of integers arr, a lucky integer is an integer which has a frequency in the array equal to its value. Return a lucky integer in the array. If there are multiple...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.A bus has n stops numbered from 0 to n - 1 that form a circle. We know the distance between all pairs of neighboring stops where distance[i] is the distance between the stops ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an integer n. Each number from 1 to n is grouped according to the sum of its digits.  Return how many groups have the largest size.   Example 1: Input: n = 13 Output: 4 ...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. Example 1: Input: [3,0,1] Output: 2 Example 2: Inp...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, formed from 3 of these lengths. If it is impossible to form any triangle o...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. Follow up: Could...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is monotone increasing if for all i <= j, A[i] <= A[j].  An array A is monotone de...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.Given a string S, return the "reversed" string where all characters that are not a letter stay in the same place, and all letters reverse their positions.   Example 1...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.On a N * N grid, we place some 1 * 1 * 1 cubes that are axis-aligned with the x, y, and z axes. Each value v = grid[i][j] represents a tower of v cubes placed on top of grid c...
user: Generate an executable Python function generated from the given prompt. Return the function body without invoking it at the final solution.On an 8 x 8 chessboard, there is one white rook.  There also may be empty squares, white bishops, and black pawns.  These are given as characters 'R', '.', 'B', and 'p' respec...