Alice and Bob take turns playing a game, with Alice starting first.
Initially, there are
Continue reading “[Leetcode]1510. Stone Game IV”
n stones in a pile. On each player's turn, that player makes a move consisting of removing any non-zero square numberof stones in the pile.
Also, if a player cannot make a move, he/she loses the game.
Given a positive integer
True if and only if Alice wins the game otherwise return
False, assuming both players play optimally.
Given the array
Continue reading “[Leetcode]1508. Range Sum of Sorted Subarray Sums”
nums consisting of
n positive integers. You computed the sum of all non-empty continous subarrays from the array and then sort them in non-decreasing order, creating a new array of
n * (n + 1) / 2 numbers.
Return the sum of the numbers from index
left to index
right (indexed from 1), inclusive, in the new array. Since the answer can be a huge number return it modulo 10^9 + 7.
Given a binary string
Continue reading “[Leetcode]1513. Number of Substrings With Only 1s”
s (a string consisting only of '0' and '1's).
Return the number of substrings with all characters 1's.
Since the answer may be too large, return it modulo 10^9 + 7.
Input: s = "0110111" Output: 9 Explanation: There are 9 substring in total with only 1's characters. "1" -> 5 times. "11" -> 3 times. "111" -> 1 time.
You are given an undirected weighted graph of
Continue reading “[Leetcode]1514. Path with Maximum Probability”
n nodes (0-indexed), represented by an edge list where
edges[i] = [a, b] is an undirected edge connecting the nodes
b with a probability of success of traversing that edge
Given two nodes
end, find the path with the maximum probability of success to go from
end and return its success probability.
If there is no path from
end, return 0.
Input: n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], start = 0, end = 2
Write a program to find the
Continue reading “[Leetcode]264. Ugly Number II”
n-th ugly number.
Ugly numbers are positive numbers whose prime factors only include
2, 3, 5.
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.
1 is typically treated as an ugly number.
n does not exceed 1690.
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
Continue reading “[Leetcode]287. Find the Duplicate Number”
[1,3,4,2,2] Output: 2
1. You must not modify the array (assume the array is read only).
2. You must use only constant, O(1) extra space.
3. Your runtime complexity should be less than O(n2).
4. There is only one duplicate number in the array, but it could be repeated more than once.
Reverse bits of a given 32 bits unsigned integer.
Input: 00000010100101000001111010011100 Output: 00111001011110000010100101000000
Continue reading “[Leetcode]190. Reverse Bits”
Given an array
Continue reading “[Leetcode]18. 4Sum”
nums of n integers and an integer
target, are there elements a, b, c, and d in
numssuch that a + b + c + d =
target? Find all unique quadruplets in the array which gives the sum of
The solution set must not contain duplicate quadruplets.
Given array nums = [1, 0, -1, 0, -2, 2], and target = 0. A solution set is: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ]