Posts
Max Area Of Island Array
PROBLEM You are given an m x n binary matrix grid. An island is a group of 1’s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
The area of an island is the number of cells with a value 1 in the island.
Return the maximum area of an island in grid. If there is no island, return 0.
read morePosts
Merge Two Binary Trees
PROBLEM You are given two binary trees root1 and root2.
Imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge the two trees into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of the new tree.
read morePosts
Minimum Path Sum
PROBLEM Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Example *Input*: grid = [[1,3,1],[1,5,1],[4,2,1]] *Output*: 7 Explanation: Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum. Constraints m==grid.length n==grid[i].length 1<=m, n<=200 0<=grid[i][j]<=100 SOLUTION We’ll use Dynamic Programming method.
read morePosts
Minimum Penalty for a Shop
PROBLEM You are given the customer visit log of a shop represented by a 0-indexed string customers consisting only of characters 'N' and 'Y':
if the ith character is 'Y' , it means that customers come at the ith hour whereas 'N' indicates that no customers come at the ith hour. If the shop closes at the jth hour (0<=j<=n), the penalty is calculated as follows:
For every hour when the shop is open and no customers come, the penalty increases by 1.
read morePosts
Minimum Time To Type Using Special Typewritter
PROBLEM There is a special typewriter with lowercase English letters 'a' to 'z' arranged in a circle with a pointer. A character can only be typed if the pointer is pointing to that character. The pointer is initially pointing to the character 'a'.
Each second, you may perform one of the following operations:
Move the Pointer one character counterclockwise or clockwise Type the character the pointer is currently only Given a string word, return the minimum number of seconds to type out the characters in word.
read morePosts
Move Zeroes
PROBLEM Given an integer array nums, move all 0’s to the end of it while maintaining the relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.
Example1 *Input*: nums = [0,1,0,3,12] *Output*: [1,3,12,0,0] Example2 *Input*: nums = [0] *Output*: [0] Constraints 1<=nums.length<=104 -231<=nums[i]<=231-1 Follow up: Could you minimize the total number of operations done?
SOLVING We’ll use the Two Pointers method
read morePosts
Palindrome Number
PROBLEM Given an integer x, return true if x is a palindrome, and false otherwise.
Example1 *Input*: x = 121 *Output*: true *Explanation*: 121 reads as 121 from left to right and from right to left. Example2 *Input*: x = -121 *Output*: false *Explanation*: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example3 *Input*: x = 10 *Output*: false *Explanation*: Reads 01 from right to left.
read morePosts
Permutation
PROBLEM Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.
Example1 *Input*: nums = [1,2,3] *Output*: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] Example2 *Input*: nums = [0,1] *Output*: [[0,1],[1,0]] Example3 *Input*: nums = [1] *Output*: [ [1] ] Constraints 1<=nums.length<=6 -10<=nums[i]<=10 All the integers of nums are unique. SOLVING we’ll use Backtracking method
Steps Backtracking method: push in result and return if current index equal size for each index between current index and size: swap in nums the element at current index and index from for loop call in recursive the backtrack method with as parameter current index + 1 re swap in nums the element at current index and index from for loop to reset the change return result Code class Solution { public: vector<vector<int>> permute(vector<int> &nums) { backtrack(nums, 0, nums.
read morePosts
Populating Next Right Pointers In Each Node
PROBLEM You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
struct Node { int val; Node *left; Node *right; Node *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
read morePosts
Problem Solving
Problem solving is the process of identifying and then implementing a solution to a problem.
read more