Skip to content

Commit d236288

Browse files
committed
Solutions added for LeetCode #462, #1423
1 parent c8e494a commit d236288

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.sean.array;
2+
3+
/***
4+
* 1423. Maximum Points You Can Obtain from Cards
5+
*/
6+
public class MaxCardPoints {
7+
8+
public int maxScore(int[] cardPoints, int k) {
9+
int len = cardPoints.length;
10+
if (k == 1) {
11+
return Math.max(cardPoints[0], cardPoints[len - 1]);
12+
}
13+
int total = 0;
14+
for (int e : cardPoints) {
15+
total += e;
16+
}
17+
if (k >= len)
18+
return total;
19+
20+
// reverse thinking : find the subarray with the min sum
21+
// sliding window
22+
int winLen = len - k;
23+
int sum = 0;
24+
for (int j = 0; j < winLen; j++) {
25+
sum += cardPoints[j];
26+
}
27+
int min = sum;
28+
for (int i = 1; i + winLen <= len; i++) {
29+
sum -= cardPoints[i - 1];
30+
sum += cardPoints[i + winLen - 1];
31+
32+
if (min > sum) {
33+
min = sum;
34+
}
35+
}
36+
return total - min;
37+
}
38+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.sean.sorting;
2+
3+
import java.util.Random;
4+
5+
/***
6+
* 462. Minimum Moves to Equal Array Elements II
7+
*/
8+
public class MinMoveCalculator {
9+
public int minMoves2(int[] nums) {
10+
if (nums.length <= 1)
11+
return 0;
12+
13+
// Find the median of the array
14+
// * Sorting the array could be one of the solutions, but its time complexity goes to O(n*lgn)
15+
// * By partition : O(n)
16+
int median = partition(nums, 0, nums.length - 1, (nums.length + 1) / 2);
17+
18+
int mv = 0;
19+
for (int e : nums) {
20+
mv += Math.abs(e - median);
21+
}
22+
return mv;
23+
}
24+
25+
private void swap(int[] nums, int i, int j) {
26+
int tmp = nums[i];
27+
nums[i] = nums[j];
28+
nums[j] = tmp;
29+
}
30+
31+
private int partition(int[] nums, int l, int r, int targetPos) {
32+
// randomizing the pivot is important
33+
final int randIndex = new Random().nextInt(r - l + 1) + l;
34+
swap(nums, randIndex, r);
35+
final int pivot = nums[r];
36+
37+
38+
// int pivot = nums[r];
39+
int swapIndex = l;
40+
for (int i = l; i < r; i++) {
41+
if (nums[i] <= pivot) {
42+
swap(nums, i, swapIndex++);
43+
}
44+
}
45+
swap(nums, swapIndex, r);
46+
47+
int cnt = swapIndex - l + 1;
48+
if (cnt == targetPos)
49+
return nums[swapIndex];
50+
else if (cnt > targetPos) {
51+
return partition(nums, l, swapIndex - 1, targetPos);
52+
} else {
53+
return partition(nums, swapIndex + 1, r, targetPos - cnt);
54+
}
55+
}
56+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.sean.array;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class MaxCardPointsTest {
9+
10+
private MaxCardPoints cardPoints;
11+
12+
@Before
13+
public void setUp() throws Exception {
14+
cardPoints = new MaxCardPoints();
15+
}
16+
17+
@Test
18+
public void maxScore() {
19+
assertEquals(232, cardPoints.maxScore(new int[]{11, 49, 100, 20, 86, 29, 72}, 4));
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.sean.sorting;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class MinMoveCalculatorTest {
9+
10+
private MinMoveCalculator calculator;
11+
12+
@Before
13+
public void setUp() throws Exception {
14+
calculator = new MinMoveCalculator();
15+
}
16+
17+
@Test
18+
public void minMoves2() {
19+
assertEquals(14, calculator.minMoves2(new int[]{1, 0, 0, 8, 6}));
20+
}
21+
}

0 commit comments

Comments
 (0)