Skip to content

Commit f049486

Browse files
committed
Day 3 - MoveZeroes & TwoSumII complete
1 parent e0abc6d commit f049486

File tree

4 files changed

+162
-15
lines changed

4 files changed

+162
-15
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package io.coffeelessprogrammer.leetcode.twopointers;
2+
3+
/*
4+
* Problem: 283. Move Zeroes
5+
* Difficulty: Easy
6+
* Acceptance Rate: 60.8%
7+
* URL: https://leetcode.com/problems/move-zeroes/
8+
*
9+
* Runtime: 2 ms, faster than 76.96% of Java online submissions for Move Zeroes.
10+
* Memory Usage: 43.8 MB, less than 85.98% of Java online submissions for Move Zeroes.
11+
*/
12+
public class MoveZeroes {
13+
14+
public void bubble(int[] nums) {
15+
if(nums.length < 2) return;
16+
17+
int leftmostZero=nextZeroIndex(nums, 0);
18+
if(leftmostZero == -1) return; // Array has no zeroes
19+
20+
int leftmostNumber = nextNonZeroIndex(nums, leftmostZero+1);
21+
22+
while(leftmostNumber > -1) {
23+
nums[leftmostZero] = nums[leftmostNumber];
24+
nums[leftmostNumber] = 0;
25+
26+
leftmostZero = nextZeroIndex(nums, leftmostZero+1);
27+
leftmostNumber = nextNonZeroIndex(nums, leftmostNumber+1);
28+
}
29+
}
30+
31+
private static int nextZeroIndex(int[] nums, final int startFrom) {
32+
for(int i=startFrom; i<nums.length; ++i) {
33+
if(nums[i]==0) return i;
34+
}
35+
36+
return -1;
37+
}
38+
39+
private static int nextNonZeroIndex(int[] nums, final int startFrom) {
40+
for(int i=startFrom; i<nums.length; ++i) {
41+
if(nums[i]!=0) return i;
42+
}
43+
44+
return -1;
45+
}
46+
47+
//#region LeetcodeResearch
48+
49+
public void moveZeroes(int[] nums) {
50+
int firstInsignificantIndex=0;
51+
52+
// Move all significant numbers left
53+
for(int i=0; i<nums.length; ++i){
54+
if(nums[i] != 0){
55+
nums[firstInsignificantIndex] = nums[i];
56+
++firstInsignificantIndex;
57+
}
58+
}
59+
60+
// Fill remaining indices with 0
61+
for(int j=firstInsignificantIndex; j<nums.length; ++j)
62+
nums[j]=0;
63+
}
64+
65+
//#endRegion
66+
}

src/main/java/io/coffeelessprogrammer/leetcode/twopointers/RotateArray1d.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,37 @@ public void rotateInplace(int[] nums, int k) {
4444
else
4545
k = k%nums.length;
4646

47-
// System.out.println("Before: " + Arrays.toString(nums));
47+
// Setup
48+
final int lcm = BasicMath.leastCommonMultiple(nums.length, k);
49+
final int cycleAfter = lcm/k;
50+
51+
// Rotate
52+
int currentCycle = 0;
53+
54+
for(int position=0, j=0; j < nums.length; ++j) {
55+
if(j/cycleAfter > currentCycle) {
56+
++currentCycle;
57+
++position;
58+
}
59+
position = (position+k) % nums.length;
60+
Array.swapIndices(nums, currentCycle, position);
61+
}
62+
}
63+
64+
public void rotateInplaceVerbose(int[] nums, int k) {
65+
// Sanitize input
66+
if(k%nums.length==0)
67+
return;
68+
else
69+
k = k%nums.length;
4870

4971
// Setup
5072
final int lcm = BasicMath.leastCommonMultiple(nums.length, k);
5173
final int cycleAfter = lcm/k;
5274

75+
System.out.printf("k=%d, iterationsPerCycle=%d\n", k, cycleAfter);
76+
System.out.printf("Before: %s\n\n", Arrays.toString(nums));
77+
5378
// Rotate
5479
int currentCycle = 0;
5580

@@ -58,13 +83,14 @@ public void rotateInplace(int[] nums, int k) {
5883
++currentCycle;
5984
++position;
6085
}
86+
System.out.printf("iteration=%d, currentCycle=%d\n", j, currentCycle);
6187
position = (position+k) % nums.length;
62-
// System.out.printf("\tSwapping: nums[%d]=%d\tnums[%d]=%d\n", currentCycle, nums[currentCycle], position, nums[position]);
88+
System.out.printf("\tSwapping: nums[%d]=%d\tnums[%d]=%d\n", currentCycle, nums[currentCycle], position, nums[position]);
6389
Array.swapIndices(nums, currentCycle, position);
64-
// System.out.printf("\tPost-swap: %s\n\n", Arrays.toString(nums));
90+
System.out.printf("\tPost-swap: %s\n\n", Arrays.toString(nums));
6591
}
6692

67-
// System.out.println("After: " + Arrays.toString(nums));
93+
System.out.println("After: " + Arrays.toString(nums));
6894
}
6995

7096
//#region LeetcodeResearch
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.coffeelessprogrammer.leetcode.twopointers;
2+
3+
/*
4+
* Problem: 167. Two Sum II - Input Array Is Sorted
5+
* Difficulty: Medium
6+
* Acceptance Rate: 38.7%
7+
* URL: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
8+
*
9+
* Runtime: 1 ms, faster than 99.24% of Java online submissions for Two Sum II - Input Array Is Sorted.
10+
* Memory Usage: 49.9 MB, less than 55.19% of Java online submissions for Two Sum II - Input Array Is Sorted.
11+
*/
12+
public class TwoSumII {
13+
14+
public int[] twoSum(int[] nums, int target) {
15+
int sum;
16+
17+
for(int i=0, j=nums.length-1; i < j; ) {
18+
sum = nums[i] + nums[j];
19+
20+
if(sum == target)
21+
return new int[]{i+1, j+1};
22+
else if(sum > target)
23+
--j;
24+
else ++i;
25+
}
26+
27+
return null;
28+
}
29+
30+
}

src/test/java/io/coffeelessprogrammer/leetcode/TwoPointersTests.java

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.coffeelessprogrammer.leetcode;
22

3+
import io.coffeelessprogrammer.leetcode.twopointers.MoveZeroes;
34
import io.coffeelessprogrammer.leetcode.twopointers.RotateArray1d;
45
import io.coffeelessprogrammer.leetcode.twopointers.SquaresOfSortedArray;
56
import org.junit.jupiter.api.Test;
@@ -31,7 +32,7 @@ final void SortedSquares_Basic2() {
3132
private RotateArray1d rotateArray1d = new RotateArray1d();
3233

3334
@Test
34-
final void RotateArray1d() {
35+
final void RotateArray1d_LinearSpace() {
3536
final int[] arr = {1,2,3,4,5,6,7};
3637

3738
rotateArray1d.rotate(arr, 3);
@@ -40,30 +41,54 @@ final void RotateArray1d() {
4041
}
4142

4243
@Test
43-
final void RotateArray1d_2() {
44+
final void RotateArray1d_Inplace() {
45+
final int[] arr = {1,2,3,4,5,6,7};
46+
47+
rotateArray1d.rotateInplace(arr, 3);
48+
49+
assertArrayEquals(new int[]{5,6,7,1,2,3,4}, arr);
50+
}
51+
52+
@Test
53+
final void RotateArray1d_Inplace_1CycleOscillation() {
4454
final int[] arr = {-1,-100,3,99};
4555

46-
rotateArray1d.rotate(arr, 2);
56+
rotateArray1d.rotateInplace(arr, 2);
4757

4858
assertArrayEquals(new int[]{3,99,-1,-100}, arr);
4959
}
5060

5161
@Test
52-
final void RotateArray1d_Inplace1() {
53-
final int[] arr = {1,2,3,4,5,6,7};
62+
final void RotateArray1d_Inplace_2CycleOscillation() {
63+
final int[] arr = {2,4,8,16,32,64};
5464

55-
rotateArray1d.rotateInplace(arr, 3);
65+
rotateArray1d.rotateInplace(arr, 4);
5666

57-
assertArrayEquals(new int[]{5,6,7,1,2,3,4}, arr);
67+
assertArrayEquals(new int[]{8,16,32,64,2,4}, arr);
5868
}
5969

6070
@Test
61-
final void RotateArray1d_Inplace2() {
62-
final int[] arr = {-1,-100,3,99};
71+
final void RotateArray1d_Inplace_2CycleOscillation_Verbose() {
72+
final int[] arr = {2,4,8,16,32,64};
6373

64-
rotateArray1d.rotateInplace(arr, 2);
74+
rotateArray1d.rotateInplaceVerbose(arr, 4);
6575

66-
assertArrayEquals(new int[]{3,99,-1,-100}, arr);
76+
assertArrayEquals(new int[]{8,16,32,64,2,4}, arr);
77+
}
78+
79+
//#endRegion
80+
81+
//#region MoveZeroes
82+
83+
private MoveZeroes moveZeroes = new MoveZeroes();
84+
85+
@Test
86+
final void MoveZeroes_Inplace() {
87+
final int[] arr = {0,1,0,3,12};
88+
89+
moveZeroes.bubble(arr);
90+
91+
assertArrayEquals(new int[]{1,3,12,0,0}, arr);
6792
}
6893

6994
//#endRegion

0 commit comments

Comments
 (0)