Skip to content

Commit

Permalink
More solutions and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spannm committed Mar 6, 2024
1 parent 0b7f23d commit 8471a0f
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
public class Problem0141 extends LeetcodeProblem {

public boolean hasCycle(ListNode _head) {
boolean hasCycle(ListNode _head) {
if (_head == null || _head.next == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.spannm.leetcode.lc0.lc0600;

import io.github.spannm.leetcode.LeetcodeSqlProblem;

/**
* 626. Exchange Seats.
*/
public class Problem0626 extends LeetcodeSqlProblem {

Problem0626() {
super("""
SELECT
s1.id,
COALESCE(s2.student, s1.student) AS student
FROM
Seat AS s1
LEFT JOIN
Seat AS s2 ON (s1.id + 1) ^ 1 - 1 = s2.id
ORDER BY 1""");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.github.spannm.leetcode.lc0.lc0600;

import io.github.spannm.leetcode.LeetcodeSqlProblem;

/**
* 627. Swap Salary.
*/
public class Problem0627 extends LeetcodeSqlProblem {

Problem0627() {
super("""
UPDATE
salary
SET
sex =
CASE sex
WHEN 'm'
THEN 'f'
ELSE 'm'
END""");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@

public class Problem0628 extends LeetcodeProblem {

public int maximumProduct(int[] _nums) {
int maximumProduct(int[] _nums) {
int len = _nums.length;
Arrays.sort(_nums);
int product = 1;
if (_nums.length >= 3) {
for (int i = _nums.length - 1; i >= _nums.length - 3; i--) {
if (len >= 3) {
for (int i = len - 1; i >= len - 3; i--) {
product *= _nums[i];
}
int anotherProduct = _nums[0] * _nums[1] * _nums[_nums.length - 1];
product = Math.max(product, anotherProduct);
} else {
for (int num : _nums) {
product *= num;
}
int another = _nums[0] * _nums[1] * _nums[len - 1];
return Math.max(product, another);
}
return product;
return Arrays.stream(_nums).reduce(1, (a, b) -> a * b);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

public class Problem0630 extends LeetcodeProblem {

public int scheduleCourse(int[][] _courses) {
int scheduleCourse(int[][] _courses) {
Arrays.sort(_courses, Comparator.comparingInt(a -> a[1]));
int day = 0;
Queue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.spannm.leetcode.lc0.lc0600;

import io.github.spannm.leetcode.LeetcodeBaseTest;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.converter.ConvertWith;
import org.junit.jupiter.params.provider.CsvSource;

class Problem0628Test extends LeetcodeBaseTest {

@ParameterizedTest(name = "[{index}] {0} --> {1}")
@CsvSource(delimiter = ';', value = {
"1,2,3; 6",
"1,2,3,4; 24",
"-1,-2,-3; -6"
})
void test(@ConvertWith(CsvToIntArray.class) int[] _nums, int _expectedResult) {
assertEquals(_expectedResult, new Problem0628().maximumProduct(_nums));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.github.spannm.leetcode.lc1.lc1700;

import io.github.spannm.leetcode.LeetcodeBaseTest;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class Problem1750Test extends LeetcodeBaseTest {

@ParameterizedTest(name = "[{index}] {0} --> {1}")
@CsvSource(delimiter = ';', value = {
"ca; 2",
"cabaabac; 0",
"aabccabba; 3"
})
void test(String _s, int _expectedResult) {
assertEquals(_expectedResult, new Problem1750().minimumLength(_s));
}

}

0 comments on commit 8471a0f

Please sign in to comment.