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
import java.io.*; | |
import java.util.*; | |
import java.text.*; | |
import java.math.*; | |
import java.util.regex.*; | |
public class Solution { | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); |
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
import java.io.*; | |
import java.util.*; | |
import java.text.*; | |
import java.math.*; | |
import java.util.regex.*; | |
public class Solution { | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
int t = in.nextInt(); |
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
import java.io.*; | |
import java.util.*; | |
import java.text.*; | |
import java.math.*; | |
import java.util.regex.*; | |
public class Solution { | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); |
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
// 3 + 3*1 + 3*2 + ... + 3*n = 3(1+2+3+...+n) = 3*n*(n+1)/2 | |
import java.io.*; | |
import java.util.*; | |
import java.text.*; | |
import java.math.*; | |
import java.util.regex.*; | |
public class Solution { | |
public static void main(String[] args) { |
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
/** | |
- When s1 = s2 = 0, longest = 0 | |
- When s1, s2 >= 1, we have two cases: | |
+ If s1.charAt(largestIndex) == s2.charAt(largestIndex) , currentLongest = 1 + [Longest when s1 and s2 - 1 length] | |
+ If s1.charAt(largestIndex) != s2.charAt(largestIndex) , currentLongest = max[[Longest when s1, s2 - 1 length],[Longest when s1 - 1, s2 length]] | |
- So we calculate lcs with s1, s2 length increase by 1 each time | |
**/ | |
class Solution { | |
public int longestCommonSubsequence(String text1, String text2) { |
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
// TLE | |
class Solution { | |
public int rob(int[] nums) { | |
int length = nums.length; | |
int total = 0; | |
int total1 = 0; | |
if (length == 0) | |
{ | |
return 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
class Solution { | |
public boolean isSubsequence(String s, String t) { | |
int j = 0; | |
int sLength = s.length(); | |
if (sLength == 0) | |
{ | |
return true; | |
} | |
for (int i = 0; i < t.length(); i++){ | |
char t1 = t.charAt(i); |
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
// Approach | |
// [a b] | top optimalCostToTop = min(a,b) | |
// [a b c] | top optimalCostToTop = min(b,a+c) | |
// [a b c d] | top optimalCostToTop = min(optimal cost to c + cost(c), optimal cost to d + cost(d)) | |
// optimal cost to c = min(a,b) | |
// optimal cost to d = min(b, a + c) | |
// [a b c d e] | top optimalCostToTop = min(optimal cost to d + cost(d), optimal cost to e + cost(e)) | |
// ... and so on | |
// class Solution { |
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
class Solution { | |
public int maxProfit(int[] prices) { | |
int maxProfit; | |
int lowestBuyDate; | |
int possibleMaxProfit; | |
if (prices.length <= 1) | |
{ | |
return 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
class Solution { | |
public int[] intersection(int[] nums1, int[] nums2) { | |
LinkedList<Integer> intersection = new LinkedList<Integer>(); | |
int lastIntersection = -1; | |
int[] outputArr; | |
int nums1Length = nums1.length; | |
int nums2Length = nums2.length; | |
Arrays.sort(nums2); | |
Arrays.sort(nums1); | |
if (nums1Length >= nums2Length) |
NewerOlder