Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
while (left < right) { if (leftMaxHeight < rightMaxHeight) { left++;
if (height[left] > leftMaxHeight) { leftMaxHeight = height[left]; res = Math.max(res, area(height, left, right)); } } else { right--;
if (height[right] > rightMaxHeight) { rightMaxHeight = height[right]; res = Math.max(res, area(height, left, right)); } } }
return res; }
privateintarea(int[] height, int left, int right) { return Math.min(height[left], height[right]) * (right - left); } }
2 Question-15[★★]
3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
/** * Definition for an interval. * public class Interval { * int start; * int end; * Interval() { start = 0; end = 0; } * Interval(int s, int e) { start = s; end = e; } * } */ publicclassSolution { public List<Interval> merge(List<Interval> intervals) { List<Interval> res = newArrayList<>();
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { publicvoidsortColors(int[] nums) { int[] cnt = newint[3]; for (int num : nums) { cnt[num]++; }
intiter=0;
for (inti=0; i <= 2; i++) { while (--cnt[i] >= 0) { nums[iter++] = i; } } } }
7 Question-76[★★★★★]
Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
if (i == 0 || j == 0 || i == m - 1 || j == n - 1) { nodes[i][j].getRoot().isRegion = true; } } } }
for (Node root : set) { if (!root.isRegion) { for (Node child : root.children) { board[child.row][child.col] = 'X'; } board[root.row][root.col] = 'X'; } } } }
9 Question-134[★★★★★]
Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station’s index if you can travel around the circuit once, otherwise return -1.
for (inti=0; i < gas.length; i++) { remain -= cost[i]; remain += gas[i]; if (remain < 0) { lack -= remain; remain = 0; start = i + 1; } }
return remain >= lack ? start : -1; } }
10 Question-200[★★★★★]
Number of Islands
Given a 2d grid map of '1’s (land) and '0’s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn’t one, return 0 instead.
Given two sorted arrays, A and B, create a new array C which contains the sum of each possible pair of elements, one from A and one from B. Your task is to find the top K largest sums in the array C.
Arrays A and B are sorted in ascending order.
The size of arrays A and B may differ.
Array C should consist of sums of pairs formed by taking one element from A and another from B.
You need to find and return the K largest sums in C.