Skip to content

Commit

Permalink
✨ feat: Add #11 #12 #15
Browse files Browse the repository at this point in the history
  • Loading branch information
Maecenas committed May 16, 2019
1 parent 5e2a578 commit 16991f1
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/main/java/LeetCode/_11_ContainerWithMostWater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package LeetCode;

/*
https://leetcode.com/problems/container-with-most-water/
Medium. Array, Two Pointers.
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.
Note: You may not slant the container and n is at least 2.
Example:
Input: [1,8,6,2,5,4,8,3,7]
Output: 49
*/

class _11_ContainerWithMostWater {

public static int maxArea(int[] height) {
if (height == null || height.length < 2) return 0;

int maxArea = 0, lo = 0, hi = height.length - 1;
while (lo < hi) {
maxArea = Math.max(maxArea, (hi - lo) * Math.min(height[lo], height[hi]));
if (height[lo] > height[hi]) {
hi--;
} else {
lo++;
}
}
return maxArea;
}
}
81 changes: 81 additions & 0 deletions src/main/java/LeetCode/_12_IntegerToRoman.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package LeetCode;

/*
https://leetcode.com/problems/integer-to-roman/
Medium. Math, String.
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, two is written as II in Roman numeral, just two one's added together.
Twelve is written as, XII, which is simply X + II.
The number twenty seven is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right.
However, the numeral for four is not IIII. Instead, the number four is written as IV.
Because the one is before the five we subtract it making four.
The same principle applies to the number nine, which is written as IX.
There are six instances where subtraction is used:
I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
Example 1:
Input: 3
Output: "III"
Example 2:
Input: 4
Output: "IV"
Example 3:
Input: 9
Output: "IX"
Example 4:
Input: 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.
Example 5:
Input: 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
*/

class _12_IntegerToRoman {

// Input is guaranteed to be within the range from 1 to 3999.
private static final String[][] intToRoman = {
{"", "M", "MM", "MMM"},
{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}
};

public static String intToRoman(int num) {
if (num < 0) return "";

return intToRoman[0][num / 1000] +
intToRoman[1][(num % 1000) / 100] +
intToRoman[2][(num % 100) / 10] +
intToRoman[3][num % 10];
}
}
66 changes: 66 additions & 0 deletions src/main/java/LeetCode/_15_3Sum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package LeetCode;

/*
https://leetcode.com/problems/3sum/
Medium. Array, Two Pointers.
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0?
Find all unique triplets in the array which gives the sum of zero.
Note:
The solution set must not contain duplicate triplets.
Example:
Given array nums = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
*/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class _15_3Sum {

public static List<List<Integer>> threeSum(int[] nums) {
if (nums == null || nums.length < 3) return new ArrayList<>();

Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
int lo, hi, target;

for (int i = 0; i < nums.length - 2; i++) {
// early return
if (nums[i] > 0) break;
// only search for the first occurrence in duplicates
if (i == 0 || nums[i] != nums[i - 1]) {
lo = i + 1;
hi = nums.length - 1;
target = 0 - nums[i];

while (lo < hi) {
if (nums[lo] + nums[hi] < target) {
lo++;
} else if (nums[lo] + nums[hi] > target) {
hi--;
} else {
res.add(Arrays.asList(nums[i], nums[lo], nums[hi]));
// skip duplicates
// also ensure index in bound by checking (lo < hi)
while (lo < hi && nums[lo] == nums[lo + 1]) lo++;
while (lo < hi && nums[hi] == nums[hi - 1]) hi--;
lo++;
hi--;
}
}
}
}
return res;
}
}

0 comments on commit 16991f1

Please sign in to comment.