-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
72 additions
and
10 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
interview_prep/algorithm/java/src/main/java/hoa/can/code/ez/AddBinary.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package hoa.can.code.ez; | ||
|
||
/** | ||
* https://leetcode.com/problems/add-binary/ | ||
*/ | ||
public class AddBinary { | ||
public String addBinary(String a, String b) { | ||
StringBuilder result = new StringBuilder(); | ||
int carry = 0; | ||
int i = a.length() - 1; | ||
int j = b.length() - 1; | ||
|
||
while (i >= 0 || j >= 0 || carry > 0) { | ||
int sum = carry; | ||
if (i >= 0) { | ||
sum += a.charAt(i) - '0'; | ||
i--; | ||
} | ||
if (j >= 0) { | ||
sum += b.charAt(j) - '0'; | ||
j--; | ||
} | ||
|
||
carry = sum / 2; | ||
result.insert(0, sum % 2); | ||
} | ||
|
||
return result.toString(); | ||
} | ||
|
||
} |
20 changes: 11 additions & 9 deletions
20
interview_prep/algorithm/java/src/main/java/hoa/can/code/gg/JumpingDP.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,31 @@ | ||
package hoa.can.code.gg; | ||
|
||
import static java.lang.Integer.MAX_VALUE; | ||
import static java.lang.Math.min; | ||
|
||
public class JumpingDP implements Jumping { | ||
public boolean canJump(int[] nums) { | ||
return minJumps(nums) != Integer.MAX_VALUE; | ||
return minJumps(nums) != MAX_VALUE; | ||
} | ||
|
||
public int minJumps(int[] arr) { | ||
int n = arr.length; | ||
int[] minCostToIdx = new int[n]; | ||
int[] cost = new int[n]; | ||
int i, j; | ||
|
||
if (n == 0 || arr[0] == 0) | ||
return Integer.MAX_VALUE; | ||
|
||
minCostToIdx[0] = 0; | ||
return MAX_VALUE; | ||
|
||
cost[0] = 0; | ||
for (i = 1; i < n; i++) { | ||
minCostToIdx[i] = Integer.MAX_VALUE; | ||
cost[i] = MAX_VALUE; | ||
for (j = 0; j < i; j++) { | ||
if (i <= j + arr[j] && minCostToIdx[j] != Integer.MAX_VALUE) { | ||
minCostToIdx[i] = Math.min(minCostToIdx[i], minCostToIdx[j] + 1); | ||
if (i <= j + arr[j] && cost[j] != MAX_VALUE) { | ||
cost[i] = min(cost[i], cost[j] + 1); | ||
break; | ||
} | ||
} | ||
} | ||
return minCostToIdx[n - 1]; | ||
return cost[n - 1]; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
interview_prep/algorithm/java/src/test/java/hoa/can/code/AddBinaryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package hoa.can.code; | ||
|
||
import hoa.can.code.ez.AddBinary; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class AddBinaryTest { | ||
AddBinary sol = new AddBinary(); | ||
|
||
@Test | ||
@DisplayName("11 + 1 = 100") | ||
public void test1() { | ||
assertEquals("100", sol.addBinary("11","1")); | ||
} | ||
|
||
@Test | ||
@DisplayName("1 + 11 = 100") | ||
public void test2() { | ||
assertEquals("100", sol.addBinary("1","11")); | ||
} | ||
|
||
@Test | ||
@DisplayName("11 + 1 = 100") | ||
public void test3() { | ||
assertEquals("10101", sol.addBinary("1010","1011")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters