# Leetcode Solutions

LeetCode Solutions in C++/Java/Python

## 1000. Minimum Cost to Merge Stones LeetCode Solution

Share:

Share:Minimum Cost to Merge Stones There areĀ nĀ piles ofĀ stonesĀ arranged in a row. TheĀ ithĀ pile hasĀ stones[i]Ā stones. A move consists of merging exactly k consecutive piles into one pile, and the cost of this move is equal to the total number of stones in these k piles. Return the minimum cost to merge all piles of stones into one pile. If it is impossible, …

## 999. Available Captures for Rook LeetCode Solution | Easy Approach

Share:

Share:Available Captures for Rook On anĀ 8 x 8Ā chessboard, there isĀ exactly oneĀ white rookĀ ‘R’Ā and some number of white bishopsĀ ‘B’, black pawnsĀ ‘p’, and empty squaresĀ ‘.’. When the rook moves, it chooses one of four cardinal directions (north, east, south, or west), then moves in that direction until it chooses to stop, reaches the edge of the board, captures …

## 998. Maximum Binary Tree II LeetCode Solution | Easy Approach

Share:

Share:Maximum Binary Tree II AĀ maximum treeĀ is a tree where every node has a value greater than any other value in its subtree. You are given the root of a maximum binary tree and an integer val. Just as in the previous problem, the given tree was constructed from a list a (root = Construct(a)) recursively with the following Construct(a) routine: If a is empty, return null. …

## 997. Find the Town Judge LeetCode Solution | Easy Approach

Share:

Share:Find the Town Judge In a town, there areĀ nĀ people labeled fromĀ 1Ā toĀ n. There is a rumor that one of these people is secretly the town judge. If the town judge exists, then: The town judge trusts nobody. Everybody (except for the town judge) trusts the town judge. There is exactly one person that satisfies properties 1 and 2. You …

## 996. Number of Squareful Arrays LeetCode Solution | Easy Approach

Share:

Share:Number of Squareful Arrays An array isĀ squarefulĀ if the sum of every pair of adjacent elements is aĀ perfect square. Given an integer array nums, return the number of permutations of nums that are squareful. Two permutations perm1 and perm2 are different if there is some index i such that perm1[i] != perm2[i]. Example 1: Input: nums = [1,17,8] Output: 2 Explanation: [1,8,17] and [17,8,1] are the valid …

## 995. Minimum Number of K Consecutive Bit Flips LeetCode Solution

Share:

Share:Minimum Number of K Consecutive Bit Flips You are given a binary arrayĀ numsĀ and an integerĀ k. A k-bit flip is choosing a subarray of length k from nums and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of k-bit flips required so that there is no 0 in the array. If it is not possible, return -1. A subarray is a contiguous part of an array. Example …

## 994. Rotting Oranges LeetCode Solution | Easy Approach

Share:

Share:Rotting Oranges You are given anĀ m x nĀ gridĀ where each cell can have one of three values: 0 representing an empty cell, 1 representing a fresh orange, or 2 representing a rotten orange. Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten. Return the minimum number of minutes that must elapse until no cell has a …

## 993. Cousins in Binary Tree LeetCode Solution | Easy Approach

Share:

Share:Cousins in Binary Tree Given theĀ rootĀ of a binary tree with unique values and the values of two different nodes of the treeĀ xĀ andĀ y, returnĀ trueĀ if the nodes corresponding to the valuesĀ xĀ andĀ yĀ in the tree areĀ cousins, orĀ falseĀ otherwise. Two nodes of a binary tree are cousins if they have the same depth with different parents. Note that in a binary tree, the …

## 992. Subarrays with K Different Integers LeetCode Solution

Share:

Share:Subarrays with K Different Integers Given an integer arrayĀ numsĀ and an integerĀ k, returnĀ the number ofĀ good subarraysĀ ofĀ nums. A good array is an array where the number of different integers in that array is exactly k. For example, [1,2,3,1,2] has 3 different integers: 1, 2, and 3. A subarray is a contiguous part of an array. Example 1: Input: nums = [1,2,1,2,3], k = 2 Output: 7 Explanation: Subarrays formed with exactly …

## 991. Broken Calculator LeetCode Solution | Easy Approach

Share:

Share:Broken Calculator There is a broken calculator that has the integerĀ startValueĀ on its display initially. In one operation, you can: multiply the number on display by 2, or subtract 1 from the number on display. Given two integers startValue and target, return the minimum number of operations needed to display target on the calculator. Example 1: Input: startValue = 2, target = 3 Output: 2 …