# 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 …