Skip to content

Commit

Permalink
Prepare for publication
Browse files Browse the repository at this point in the history
  • Loading branch information
spannm committed Jul 4, 2024
1 parent d9714b6 commit ffe16bf
Show file tree
Hide file tree
Showing 354 changed files with 522 additions and 2,175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ static final class IdxAndNum {
num = _num;
}

public int getIdx() {
int getIdx() {
return idx;
}

public int getNum() {
int getNum() {
return num;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,19 @@
class Problem0011 extends LeetcodeProblem {

int maxArea(final int[] _height) {
return maxAreaOptimized(_height);
}

public int maxAreaNaive(final int[] _height) {
int minHeight = 0;
int area = 0;
int maxArea = 0;

for (int x1 = _height.length - 1; x1 >= 0; x1--) {
for (int x2 = 0; x2 < x1; x2++) {
minHeight = Math.min(_height[x1], _height[x2]);
area = minHeight * (x1 - x2);
if (area > maxArea) {
maxArea = area;
}
}
}
return maxArea;
}

public int maxAreaOptimized(final int[] _height) {
int idxL = 0;
int idxR = _height.length - 1;
int hL = 0;
int hR = 0;
int area = 0;
int maxArea = 0;
int maxArea1 = 0;

while (idxL < idxR) {
hL = _height[idxL];
hR = _height[idxR];
area = Math.min(hL, hR) * (idxR - idxL);
if (area > maxArea) {
maxArea = area;
if (area > maxArea1) {
maxArea1 = area;
}

if (hL < hR) {
Expand All @@ -54,7 +33,7 @@ public int maxAreaOptimized(final int[] _height) {
}
}
}
return maxArea;
return maxArea1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,14 @@

import io.github.spannm.leetcode.LeetcodeProblem;

import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
* <a href="https://leetcode.com/problems/integer-to-roman/">12. Integer to Roman</a>.
*/
class Problem0012 extends LeetcodeProblem {

static final Map<Integer, String> MAP = IntStream.range(1, 4000).boxed().collect(Collectors.toMap(k -> k, Problem0012::intToRomanSwitch));

public String intToRoman(final int _num) {
return intToRomanStaticMap(_num);
}

static String intToRomanStaticMap(final int _num) {
return MAP.get(_num);
}

static String intToRomanSwitch(final int _num) {
final int[] digits = {_num / 1000, _num / 100 % 10, _num / 10 % 10, _num % 10};
final StringBuilder sb = new StringBuilder();
String intToRoman(final int _num) {
int[] digits = {_num / 1000, _num / 100 % 10, _num / 10 % 10, _num % 10};
StringBuilder sb = new StringBuilder();
for (int i = 0; i < digits.length; i++) {
if (digits[i] < 1) {
continue;
Expand Down Expand Up @@ -80,57 +66,4 @@ static String intToRomanSwitch(final int _num) {
return sb.toString();
}

static String intToRomanSubtraction(int _num) {
final StringBuilder sb = new StringBuilder();
while (_num >= 1000) {
sb.append('M');
_num -= 1000;
}
if (_num >= 900) {
sb.append("CM");
_num -= 900;
} else if (_num >= 500) {
sb.append('D');
_num -= 500;
}
if (_num >= 400) {
sb.append("CD");
_num -= 400;
} else {
while (_num >= 100) {
sb.append('C');
_num -= 100;
}
}
if (_num >= 90) {
sb.append("XC");
_num -= 90;
} else if (_num >= 50) {
sb.append('L');
_num -= 50;
} else if (_num >= 40) {
sb.append("XL");
_num -= 40;
}
while (_num >= 10) {
sb.append('X');
_num -= 10;
}
if (_num == 9) {
sb.append("IX");
return sb.toString();
} else if (_num == 4) {
sb.append("IV");
return sb.toString();
} else if (_num >= 5) {
sb.append('V');
_num -= 5;
}
while (_num >= 1) {
sb.append('I');
_num -= 1;
}
return sb.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*/
class Problem0015 extends LeetcodeProblem {

List<List<Integer>> threeSum2(int[] _nums) {
final int len = _nums.length;
List<List<Integer>> threeSum(int[] _nums) {
int len = _nums.length;
if (len == 0) {
return List.of();
}
Expand Down Expand Up @@ -38,34 +38,4 @@ List<List<Integer>> threeSum2(int[] _nums) {
return new ArrayList<>(set);
}

public List<List<Integer>> threeSum(int[] _nums) {
List<Integer> input = Arrays.stream(_nums).sorted().boxed().toList();
final int len = input.size();
if (len < 3) {
return List.of();
}

Set<List<Integer>> results = new HashSet<>();
for (int first = 0; first < len - 2; first++) {
int second = first + 1;
int third = len - 1;
int sum;
while (second < third) {
sum = input.get(first) + input.get(second) + input.get(third);
if (sum == 0) {
results.add(List.of(input.get(first), input.get(second), input.get(third)));
second++;
third--;
} else if (sum < 0) {
second++;
} else {
third--;
}
}

}

return new ArrayList<>(results);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,25 @@

import io.github.spannm.leetcode.LeetcodeProblem;

import java.util.*;
import java.util.stream.IntStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

/**
* <a href="https://leetcode.com/problems/letter-combinations-of-a-phone-number/">17. Letter Combinations of a Phone
* Number</a>.
*/
class Problem0017 extends LeetcodeProblem {

@SuppressWarnings("unchecked")
private static final List<String>[] DIGITS_TO_LETTERS = new List[] {null, // 0
null, // 1
List.of("a", "b", "c"), // 2
List.of("d", "e", "f"), // 3
List.of("g", "h", "i"), List.of("j", "k", "l"), List.of("m", "n", "o"), List.of("p", "q", "r", "s"), List.of("t", "u", "v"), List.of("w", "x", "y", "z")
};

private static final char[][] DIGITS_TO_CHARS = new char[][] {null, // 0
null, // 1
{'a', 'b', 'c'}, // 2
{'d', 'e', 'f'}, // 3
{'g', 'h', 'i'}, {'j', 'k', 'l'}, {'m', 'n', 'o'}, {'p', 'q', 'r', 's'}, {'t', 'u', 'v'}, {'w', 'x', 'y', 'z'}
};

public List<String> letterCombinations(String _digits) {
List<String> letterCombinations(String _digits) {
if (_digits == null || _digits.isEmpty()) {
return List.of();
}
Expand All @@ -48,40 +42,4 @@ static Collection<String> cartesianProduct(String _input, StringBuilder _sb, int
return _product;
}

public List<String> letterCombinations2(String _digits) {
final int len = _digits.length();

if (len == 0) {
return List.of();
}

List<List<String>> lol = IntStream.range(0, len)
// .map(i -> digits.charAt(i) - '0')
.map(i -> Character.getNumericValue(_digits.charAt(i)))
.mapToObj(i -> DIGITS_TO_LETTERS[i])
.toList();

if (len == 1) {
return lol.get(0);
}

Set<String> combinations = new LinkedHashSet<>();

generatePermutations(lol, combinations, 0, "");

return new ArrayList<>(combinations);
}

// cartesian product
void generatePermutations(List<List<String>> _lists, Collection<String> _result, int _depth, String _current) {
if (_depth == _lists.size()) {
_result.add(_current);
return;
}

for (String element : _lists.get(_depth)) {
generatePermutations(_lists, _result, _depth + 1, _current + element);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,17 @@

import io.github.spannm.leetcode.LeetcodeProblem;

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.LongStream;

/**
* <a href="https://leetcode.com/problems/4sum/">18. 4Sum</a>.
*/
class Problem0018 extends LeetcodeProblem {

List<List<Integer>> fourSum1(int[] _nums, int _target) {
final int len = _nums.length;
Arrays.sort(_nums);

Set<List<Integer>> sets = new HashSet<>();
for (int i = 0; i < len - 3; i++) {
for (int j = i + 1; j < len - 2; j++) {
for (int k = j + 1; k < len - 1; k++) {
for (int p = k + 1; p < len; p++) {
int sum = _nums[i] + _nums[j] + _nums[k] + _nums[p];
if (sum == _target) {
sets.add(List.of(_nums[i], _nums[j], _nums[k], _nums[p]));
}
}
}
}
}

List<List<Integer>> results = new ArrayList<>(sets);

_nums = null;
sets = null;

return results;
}

public List<List<Integer>> fourSum(int[] _nums, final int _target) {
List<List<Integer>> fourSum(int[] _nums, final int _target) {
final int len = _nums.length;
if (len <= 4) {
if (len == 4 && Arrays.stream(_nums).mapToLong(i -> i).sum() == _target) {
Expand Down Expand Up @@ -88,7 +64,6 @@ public List<List<Integer>> fourSum(int[] _nums, final int _target) {
}
}
}
nums = null;
return results;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,4 @@ boolean isValid(String _s) {
return stack.isEmpty();
}

public boolean isValid2(String _s) {
if (_s.length() % 2 != 0) {
return false;
}

final char[] charArray = _s.toCharArray();
final char[] stack = new char[charArray.length];
int head = 0;
for (char c : charArray) {
switch (c) {
case '{':
case '[':
case '(':
stack[head++] = c;
continue;
case '}':
if (head == 0 || stack[--head] != '{') {
return false;
}
break;
case ')':
if (head == 0 || stack[--head] != '(') {
return false;
}
break;
case ']':
if (head == 0 || stack[--head] != '[') {
return false;
}
break;
default:
break;
}
}
return head == 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
class Problem0024 extends LeetcodeProblem {

static ListNode swapPairs(ListNode _head) {
ListNode swapPairs(ListNode _head) {
if (_head == null || _head.next == null) {
return _head;
}
Expand Down
Loading

0 comments on commit ffe16bf

Please sign in to comment.