Alice and Bob take turns playing a game, with Alice starting first. Initially, there areContinue reading “[Leetcode]1510. Stone Game IV”`n`

stones in a pile. On each player's turn, that player makes amoveconsisting of removinganynon-zerosquare numberof stones in the pile. Also, if a player cannot make a move, he/she loses the game. Given a positive integer`n`

. Return`True`

if and only if Alice wins the game otherwise return`False`

, assuming both players play optimally.

## [Leetcode]1508. Range Sum of Sorted Subarray Sums

Given the arrayContinue 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.

## [Leetcode]1513. Number of Substrings With Only 1s

Given a binary stringContinue 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.Example 1:Input:s = "0110111"Output:9Explanation:There are 9 substring in total with only 1's characters. "1" -> 5 times. "11" -> 3 times. "111" -> 1 time.

## [Leetcode]1514. Path with Maximum Probability

You are given an undirected weighted graph ofContinue 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`a`

and`b`

with a probability of success of traversing that edge`succProb[i]`

. Given two nodes`start`

and`end`

, find the path with the maximum probability of success to go from`start`

to`end`

and return its success probability. If there is no path from`start`

to`end`

,return 0.Example 1:Input:n = 3, edges = [[0,1],[1,2],[0,2]], succProb = [0.5,0.5,0.2], start = 0, end = 2Output:0.25000

## [Leetcode]264. Ugly Number II

Write a program to find theContinue reading “[Leetcode]264. Ugly Number II”`n`

-th ugly number. Ugly numbers arepositive numberswhose prime factors only include`2, 3, 5`

.Example:Input:n = 10Output:12Explanation:`1, 2, 3, 4, 5, 6, 8, 9, 10, 12`

is the sequence of the first`10`

ugly numbers.Note:1.`1`

is typically treated as an ugly number. 2.`n`

does not exceed 1690.

## [Leetcode]287. Find the Duplicate Number

Given an arrayContinue reading “[Leetcode]287. Find the Duplicate Number”numscontainingn+ 1 integers where each integer is between 1 andn(inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.Example 1:Input:`[1,3,4,2,2]`

Output:2Note:1. Youmust notmodify 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 thanO(n^{2}). 4. There is only one duplicate number in the array, but it could be repeated more than once.

## [Leetcode]190. Reverse Bits

Reverse bits of a given 32 bits unsigned integer.Continue reading “[Leetcode]190. Reverse Bits”Example 1:Input:00000010100101000001111010011100Output:00111001011110000010100101000000

## [Leetcode]18. 4Sum

Given an arrayContinue reading “[Leetcode]18. 4Sum”`nums`

ofnintegers and an integer`target`

, are there elementsa,b,c, anddin`nums`

such thata+b+c+d=`target`

? Find all unique quadruplets in the array which gives the sum of`target`

.Note:The solution set must not contain duplicate quadruplets.Example: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] ]