This file contains hidden or 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
package NumberSolitaire; | |
class Solution { | |
public int solution(int[] A) { | |
// main idea: | |
// using "dynamic programming" to build up the solution | |
// (bottom up) | |
int[] dp = new int[A.length]; |
This file contains hidden or 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
package TieRopes; | |
class Solution { | |
public int solution(int K, int[] A) { | |
// notice that only "adjacent ropes" can be tied | |
// so, the problem is simple; we can use "greedy" method | |
int total =0; | |
int currentLength=0; |
This file contains hidden or 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
package MaxNonoverlappingSegments; | |
class Solution { | |
public int solution(int[] A, int[] B) { | |
// main idea: | |
// Using "greedy" method to find non-overlapping segments | |
// because the segments are sorted by their rightEnds | |
// we use "for loop" from rightEnd to left |
This file contains hidden or 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
package CountTriangles; | |
import java.util.*; | |
class Solution { | |
public int solution(int[] A) { | |
int numTriangle = 0; | |
// important: sort the edges |
This file contains hidden or 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
// This solution is simple and correct, but with low performance (100% correct, 40% performance) | |
package CountDistinctSlices; | |
import java.util.*; | |
class Solution { | |
public int solution(int M, int[] A) { | |
// key point: using "set" for "each leftEnd" |
This file contains hidden or 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
package CountDistinctSlices; | |
class Solution { | |
public int solution(int M, int[] A) { | |
// This solution is more clever, and much faster O(n) | |
// main idea: | |
// use "boolean[]" to record if an integer is already seen | |
// also use "leftEnd" and "rightEnd" |
This file contains hidden or 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
package AbsDistinct; | |
import java.util.*; | |
class Solution { | |
public int solution(int[] A) { | |
// using "Set" | |
Set<Integer> set = new HashSet<>(); | |
This file contains hidden or 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
package MinMaxDivision; | |
class Solution { | |
public int solution(int K, int M, int[] A) { | |
// main idea: | |
// The goal is to find the "minimal large sum" | |
// We use "binary search" to find it (so, it can be fast) | |
// We assume that the "min max Sum" will be |
This file contains hidden or 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
package FibFrog; | |
import java.util.*; | |
// for using "point" (java.awt.*) | |
import java.awt.*; | |
class Solution { | |
public int solution(int[] A) { | |
// note: cannot use "List" (both java.util.* and java.awt.* have "List") |
This file contains hidden or 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
package Ladder; | |
class Solution { | |
public int[] solution(int[] A, int[] B) { | |
// The task is to find out the number of ways | |
// someone can climb up a ladder of N rungs | |
// by ascending one or two rungs at a time. | |
// It is not very hard to see that | |
// this number is just the "Fibonacci number of order N" |
NewerOlder