LeetCode Maximum Erasure Value

You are given an array of positive integers nums and want to erase a subarray containing unique elements. The score you get by erasing the subarray is equal to the sum of its elements. Return the maximum score you can get by erasing exactly one subarray. An array b is called to be a subarray of a if it forms a contiguous subsequence of a, that is, if it is equal to a[l],a[l+1],…,a[r] for some (l,r). Example…… Continue reading LeetCode Maximum Erasure Value

Published

Jump Search Algorithm In Java

Jump search algorithm is a pretty new algorithm to search for an element in a sorted array. The idea of jump search is to skip the number of comparisons by jumping the indices by length m while performing the searching thus getting a better time complexity than linear search. For jump search m is determined…… Continue reading Jump Search Algorithm In Java

Published

Knuth Morris Pratt Pattern Search Algorithm

What is Knuth Morris Pratt or KMP algorithm ? KMP is an algorithm which is used in the applications of pattern matching like, find a Word ‘W’ in a String ‘S’ in O(M+N) time complexity M being the length of the string and N being the length of the word. Here is the video explanation…… Continue reading Knuth Morris Pratt Pattern Search Algorithm

Published

Binary Search Algorithm In Java

Binary Search is a Logarithmic search which finds the target element in a sorted array in O(logN) times compared to linear search which takes O(N). Binary search uses divide and conquer approach and can only work on a sorted array or list. In this approach, we divide the input collections into two equal halves using…… Continue reading Binary Search Algorithm In Java

Leetcode Unique Paths Java Solution

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below). How many possible unique paths are there?…… Continue reading Leetcode Unique Paths Java Solution

Published

Leetcode Regular Expression Matching Java Solution

Given an input string (s) and a pattern (p), implement regular expression matching with support for ‘.’ and ‘*’. ‘.’ Matches any single character. ‘*’ Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). Note: s could be empty and contains only lowercase letters a-z.p could be empty and contains only lowercase letters a-z, and…… Continue reading Leetcode Regular Expression Matching Java Solution

Published

Leetcode ZigZag Conversion Java Solution

The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: “PAHNAPLSIIGYIR” Write the code that will take…… Continue reading Leetcode ZigZag Conversion Java Solution

Published

Leetcode Sum Root to Leaf Numbers Java Solution

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. Note: A leaf is a node with no children. Example: Input: [1,2,3] 1 / \ 2 3 Output: 25 Explanation: The root-to-leaf path 1->2 represents…… Continue reading Leetcode Sum Root to Leaf Numbers Java Solution

Published

Leetcode 59 Spiral Matrix II Java Solution

Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. Example: Input: 3 Output: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] Solution 1: class Solution { public int[][] generateMatrix(int num) { int[][] spiralMatrix = new int[num][num]; int m =…… Continue reading Leetcode 59 Spiral Matrix II Java Solution

Published

Leetcode 54 Spiral Matrix Java Solution

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example 1: Input: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] Output: [1,2,3,6,9,8,7,4,5] Example 2: Input: [ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12] ] Output: [1,2,3,4,8,12,11,10,9,5,6,7] Solution 1: class…… Continue reading Leetcode 54 Spiral Matrix Java Solution

Published