Skip to content

Commit 504af1b

Browse files
Kevin Naughton JrKevin Naughton Jr
authored andcommitted
adding new problems
1 parent 6269878 commit 504af1b

15 files changed

+466
-1
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
2+
3+
//Example:
4+
//Input: "babad"
5+
//Output: "bab"
6+
7+
//Note: "aba" is also a valid answer.
8+
9+
//Example:
10+
//Input: "cbbd"
11+
//Output: "bb"
12+
13+
class LongestPalindromicSubstring {
14+
public String longestPalindrome(String s) {
15+
if(s == null || s.length() == 0) {
16+
return "";
17+
}
18+
19+
String longestPalindromicSubstring = "";
20+
for(int i = 0; i < s.length(); i++) {
21+
for(int j = i + 1; j <= s.length(); j++) {
22+
if(j - i > longestPalindromicSubstring.length() && isPalindrome(s.substring(i, j))) {
23+
longestPalindromicSubstring = s.substring(i, j);
24+
}
25+
}
26+
}
27+
28+
return longestPalindromicSubstring;
29+
}
30+
31+
public boolean isPalindrome(String s) {
32+
int i = 0;
33+
int j = s.length() - 1;
34+
while(i <= j) {
35+
if(s.charAt(i++) != s.charAt(j--)) {
36+
return false;
37+
}
38+
}
39+
40+
return true;
41+
}
42+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
2+
3+
//Example:
4+
//Input: "babad"
5+
//Output: "bab"
6+
7+
//Note: "aba" is also a valid answer.
8+
9+
//Example:
10+
//Input: "cbbd"
11+
//Output: "bb"
12+
13+
class LongestPalindromicSubstring {
14+
public String longestPalindrome(String s) {
15+
if(s == null || s.length() == 0) {
16+
return "";
17+
}
18+
19+
String longestPalindromicSubstring = "";
20+
for(int i = 0; i < s.length(); i++) {
21+
for(int j = i + 1; j <= s.length(); j++) {
22+
if(j - i > longestPalindromicSubstring.length() && isPalindrome(s.substring(i, j))) {
23+
longestPalindromicSubstring = s.substring(i, j);
24+
}
25+
}
26+
}
27+
28+
return longestPalindromicSubstring;
29+
}
30+
31+
public boolean isPalindrome(String s) {
32+
int i = 0;
33+
int j = s.length() - 1;
34+
while(i <= j) {
35+
if(s.charAt(i++) != s.charAt(j--)) {
36+
return false;
37+
}
38+
}
39+
40+
return true;
41+
}
42+
}
43+

company/facebook/MinStack.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Your MinStack object will be instantiated and called as such:
3+
* MinStack obj = new MinStack();
4+
* obj.push(x);
5+
* obj.pop();
6+
* int param_3 = obj.top();
7+
* int param_4 = obj.getMin();
8+
*/
9+
class MinStack {
10+
class Node {
11+
int data;
12+
int min;
13+
Node next;
14+
15+
public Node(int data, int min) {
16+
this.data = data;
17+
this.min = min;
18+
this.next = null;
19+
}
20+
}
21+
Node head;
22+
23+
/** initialize your data structure here. */
24+
public MinStack() {
25+
26+
}
27+
28+
public void push(int x) {
29+
if(head == null) {
30+
head = new Node(x, x);
31+
} else {
32+
Node newNode = new Node(x, Math.min(x, head.min));
33+
newNode.next = head;
34+
head = newNode;
35+
}
36+
}
37+
38+
public void pop() {
39+
head = head.next;
40+
}
41+
42+
public int top() {
43+
return head.data;
44+
}
45+
46+
public int getMin() {
47+
return head.min;
48+
}
49+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
2+
3+
//Example:
4+
//Input: "babad"
5+
//Output: "bab"
6+
7+
//Note: "aba" is also a valid answer.
8+
9+
//Example:
10+
//Input: "cbbd"
11+
//Output: "bb"
12+
13+
class LongestPalindromicSubstring {
14+
public String longestPalindrome(String s) {
15+
if(s == null || s.length() == 0) {
16+
return "";
17+
}
18+
19+
String longestPalindromicSubstring = "";
20+
for(int i = 0; i < s.length(); i++) {
21+
for(int j = i + 1; j <= s.length(); j++) {
22+
if(j - i > longestPalindromicSubstring.length() && isPalindrome(s.substring(i, j))) {
23+
longestPalindromicSubstring = s.substring(i, j);
24+
}
25+
}
26+
}
27+
28+
return longestPalindromicSubstring;
29+
}
30+
31+
public boolean isPalindrome(String s) {
32+
int i = 0;
33+
int j = s.length() - 1;
34+
while(i <= j) {
35+
if(s.charAt(i++) != s.charAt(j--)) {
36+
return false;
37+
}
38+
}
39+
40+
return true;
41+
}
42+
}

leetcode/array/BestTimeToBuyAndSellStock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public int maxProfit(int[] prices) {
2020
return 0;
2121
}
2222

23-
int max = 0;
2423
int min = prices[0];
24+
int max = 0;
2525

2626
for(int i = 1; i < prices.length; i++) {
2727
if(prices[i] > min) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//Say you have an array for which the ith element is the price of a given stock on day i.
2+
//Design an algorithm to find the maximum profit. You may complete as many transactions as you
3+
//like (ie, buy one and sell one share of the stock multiple times). However, you may not engage
4+
//in multiple transactions at the same time (ie, you must sell the stock before you buy again).
5+
6+
class BestTimeToBuyAndSellStockII {
7+
public int maxProfit(int[] prices) {
8+
if(prices == null || prices.length == 0) {
9+
return 0;
10+
}
11+
12+
int profit = 0;
13+
for(int i = 0; i < prices.length - 1; i++) {
14+
if(prices[i] < prices[i + 1]) {
15+
profit += prices[i + 1] - prices[i];
16+
}
17+
}
18+
19+
return profit;
20+
}
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//Given two lists Aand B, and B is an anagram of A. B is an anagram of A means B is made by randomizing the order of the elements in A.
2+
//We want to find an index mapping P, from A to B. A mapping P[i] = j means the ith element in A appears in B at index j.
3+
//These lists A and B may contain duplicates. If there are multiple answers, output any of them.
4+
5+
//For example, given
6+
//A = [12, 28, 46, 32, 50]
7+
//B = [50, 12, 32, 46, 28]
8+
9+
//We should return
10+
//[1, 4, 3, 2, 0]
11+
//as P[0] = 1 because the 0th element of A appears at B[1], and P[1] = 4 because the 1st element of A appears at B[4], and so on.
12+
13+
class FindAnagramMappings {
14+
public int[] anagramMappings(int[] A, int[] B) {
15+
int[] mapping = new int[A.length];
16+
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
17+
18+
for(int i = 0; i < B.length; i++) {
19+
map.put(B[i], i);
20+
}
21+
22+
for(int i = 0; i < A.length; i++) {
23+
mapping[i] = map.get(A[i]);
24+
}
25+
26+
return mapping;
27+
}
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//You're given strings J representing the types of stones that are jewels, and S representing the stones you have.
2+
//Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels.
3+
4+
//The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive,
5+
//so "a" is considered a different type of stone from "A".
6+
7+
class JewelsAndStones {
8+
public int numJewelsInStones(String J, String S) {
9+
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
10+
for(char c: J.toCharArray()) {
11+
map.put(c, 1);
12+
}
13+
14+
int numberOfJewels = 0;
15+
for(char c: S.toCharArray()) {
16+
if(map.containsKey(c)) {
17+
numberOfJewels++;
18+
}
19+
}
20+
21+
return numberOfJewels;
22+
}
23+
}

leetcode/hash-table/ValidAnagram.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class ValidAnagram {
2+
public boolean isAnagram(String s, String t) {
3+
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
4+
for(char c: s.toCharArray()) {
5+
if(map.containsKey(c)) {
6+
map.put(c, map.get(c) + 1);
7+
}
8+
else {
9+
map.put(c, 1);
10+
}
11+
}
12+
13+
for(char c: t.toCharArray()) {
14+
if(map.containsKey(c)) {
15+
map.put(c, map.get(c) - 1);
16+
}
17+
else {
18+
return false;
19+
}
20+
}
21+
22+
for(char c: map.keySet()) {
23+
if(map.get(c) != 0) {
24+
return false;
25+
}
26+
}
27+
28+
return true;
29+
}
30+
}

leetcode/math/PalindromeNumber.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//Determine whether an integer is a palindrome. Do this without extra space.
2+
3+
class PalindromeNumber {
4+
public boolean isPalindrome(int x) {
5+
if(x < 0) {
6+
return false;
7+
}
8+
9+
int num = x;
10+
int reversed = 0;
11+
12+
while(num != 0) {
13+
reversed = reversed * 10 + num % 10;
14+
num /= 10;
15+
}
16+
17+
return x == reversed;
18+
}
19+
}

leetcode/math/PoorPigs.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//There are 1000 buckets, one and only one of them contains poison, the rest are filled with water.
2+
//They all look the same. If a pig drinks that poison it will die within 15 minutes. What is the
3+
//minimum amount of pigs you need to figure out which bucket contains the poison within one hour.
4+
5+
//Answer this question, and write an algorithm for the follow-up general case.
6+
7+
//Follow-up:
8+
//If there are n buckets and a pig drinking poison will die within m minutes, how many pigs (x)
9+
//you need to figure out the "poison" bucket within p minutes? There is exact one bucket with poison.
10+
11+
class PoorPigs {
12+
public int poorPigs(int buckets, int minutesToDie, int minutesToTest) {
13+
int numPigs = 0;
14+
while (Math.pow(minutesToTest / minutesToDie + 1, numPigs) < buckets) {
15+
numPigs++;
16+
}
17+
18+
return numPigs;
19+
}
20+
}

leetcode/stack/MinStack.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
2+
//push(x) -- Push element x onto stack.
3+
//pop() -- Removes the element on top of the stack.
4+
//top() -- Get the top element.
5+
//getMin() -- Retrieve the minimum element in the stack.
6+
7+
/**
8+
* Your MinStack object will be instantiated and called as such:
9+
* MinStack obj = new MinStack();
10+
* obj.push(x);
11+
* obj.pop();
12+
* int param_3 = obj.top();
13+
* int param_4 = obj.getMin();
14+
*/
15+
class MinStack {
16+
class Node {
17+
int data;
18+
int min;
19+
Node next;
20+
21+
public Node(int data, int min) {
22+
this.data = data;
23+
this.min = min;
24+
this.next = null;
25+
}
26+
}
27+
Node head;
28+
29+
/** initialize your data structure here. */
30+
public MinStack() {
31+
32+
}
33+
34+
public void push(int x) {
35+
if(head == null) {
36+
head = new Node(x, x);
37+
} else {
38+
Node newNode = new Node(x, Math.min(x, head.min));
39+
newNode.next = head;
40+
head = newNode;
41+
}
42+
}
43+
44+
public void pop() {
45+
head = head.next;
46+
}
47+
48+
public int top() {
49+
return head.data;
50+
}
51+
52+
public int getMin() {
53+
return head.min;
54+
}
55+
}
56+

0 commit comments

Comments
 (0)