Skip to content

Commit 4bc49a6

Browse files
committed
Add new solutions
1 parent e587481 commit 4bc49a6

File tree

7 files changed

+236
-0
lines changed

7 files changed

+236
-0
lines changed

src/BinarySearch.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
public class BinarySearch {
2+
3+
/*
4+
Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.
5+
You must write an algorithm with O(log n) runtime complexity.
6+
7+
https://leetcode.com/problems/binary-search/
8+
*/
9+
10+
public static int search(int[] nums, int target) {
11+
int min = 0;
12+
int max = nums.length - 1;
13+
14+
while (min <= max) {
15+
int mid = (min + max) / 2;
16+
17+
if (nums[mid] == target) {
18+
return mid;
19+
} else if (nums[mid] < target) {
20+
min = mid + 1;
21+
} else {
22+
max = mid - 1;
23+
}
24+
}
25+
26+
return -1;
27+
}
28+
29+
public static void main(String[] args) {
30+
int[] arr = {-1,0,3,5,9,12};
31+
32+
System.out.println(search(arr, 9)); //-> 4
33+
System.out.println(search(arr, 2)); //-> -1
34+
}
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.andrewbayd;
2+
3+
public class BestTimeToBuyAndSellStock {
4+
5+
/*
6+
You are given an array prices where prices[i] is the price of a given stock on the ith day.
7+
You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.
8+
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
9+
10+
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
11+
*/
12+
13+
public static int maxProfit(int[] prices) {
14+
15+
int minPrice = prices[0];
16+
int maxProfit = 0;
17+
18+
for (int i = 1; i < prices.length; i++) {
19+
if (prices[i] <= minPrice) {
20+
minPrice = prices[i];
21+
} else {
22+
maxProfit = Math.max(maxProfit, prices[i] - minPrice);
23+
}
24+
}
25+
26+
return maxProfit;
27+
}
28+
29+
public static void main(String[] args) {
30+
System.out.println(maxProfit(new int[]{7,1,5,3,6,4})); //-> 5
31+
System.out.println(maxProfit(new int[]{7,6,4,3,1})); //-> 0
32+
}
33+
}

src/com/andrewbayd/ClimbStairs.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.andrewbayd;
2+
3+
public class ClimbStairs {
4+
5+
/*
6+
You are climbing a staircase. It takes n steps to reach the top.
7+
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
8+
9+
https://leetcode.com/problems/climbing-stairs/
10+
*/
11+
12+
public static int climbStairs(int n) {
13+
int x = 1;
14+
int y = 1;
15+
16+
for (int i = 0; i < n - 1; i++) {
17+
int temp = x;
18+
x = x + y;
19+
y = temp;
20+
}
21+
22+
return x;
23+
}
24+
25+
public static void main(String[] args) {
26+
assert climbStairs(2) == 2;
27+
assert climbStairs(3) == 3;
28+
assert climbStairs(5) == 8;
29+
}
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.andrewbayd;
2+
3+
import java.util.HashSet;
4+
5+
public class ContainsDuplicate {
6+
7+
/*
8+
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
9+
10+
https://leetcode.com/problems/contains-duplicate/
11+
*/
12+
13+
public static boolean containsDuplicate(int[] nums) {
14+
HashSet<Integer> passedElements = new HashSet<>();
15+
for (int num : nums) {
16+
if (passedElements.contains(num)) {
17+
return true;
18+
}
19+
passedElements.add(num);
20+
}
21+
return false;
22+
}
23+
24+
public static void main(String[] args) {
25+
assert containsDuplicate(new int[]{1, 2, 3, 1});
26+
assert !containsDuplicate(new int[]{1, 2, 3, 4});
27+
assert containsDuplicate(new int[]{1,1,1,3,3,4,3,2,4,2});
28+
}
29+
}

src/com/andrewbayd/MissingNumber.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.andrewbayd;
2+
3+
public class MissingNumber {
4+
5+
/*
6+
Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
7+
8+
https://leetcode.com/problems/missing-number/
9+
*/
10+
11+
public static int missingNumber(int[] nums) {
12+
13+
int n = nums.length;
14+
int sumArr = 0;
15+
int sumFull = n;
16+
17+
for (int i = 0; i < n; i++) {
18+
sumArr += nums[i];
19+
sumFull += i;
20+
}
21+
22+
return sumFull - sumArr;
23+
}
24+
25+
public static void main(String[] args) {
26+
assert missingNumber(new int[]{3,0,1}) == 2;
27+
assert missingNumber(new int[]{0,1}) == 3;
28+
assert missingNumber(new int[]{9,6,4,2,3,5,7,0,1}) == 8;
29+
}
30+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.andrewbayd;
2+
3+
public class ValidPalindrome {
4+
5+
/*
6+
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters,
7+
it reads the same forward and backward. Alphanumeric characters include letters and numbers.
8+
Given a string s, return true if it is a palindrome, or false otherwise.
9+
10+
https://leetcode.com/problems/valid-palindrome/
11+
*/
12+
13+
public static boolean isPalindrome(String s) {
14+
15+
int left = 0;
16+
int right = s.length() - 1;
17+
18+
while (left < right) {
19+
if (!Character.isLetterOrDigit(s.charAt(left))) {
20+
left++;
21+
} else if (!Character.isLetterOrDigit(s.charAt(right))) {
22+
right--;
23+
} else if (Character.toLowerCase(s.charAt(left)) != Character.toLowerCase(s.charAt(right))) {
24+
return false;
25+
} else {
26+
left++;
27+
right--;
28+
}
29+
}
30+
31+
return true;
32+
}
33+
34+
public static void main(String[] args) {
35+
System.out.println(isPalindrome("A man, a plan, a canal: Panama"));//-> true
36+
System.out.println(isPalindrome("race a car"));//-> false
37+
System.out.println(isPalindrome(" "));//-> true
38+
}
39+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.andrewbayd;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Stack;
6+
7+
public class ValidParentheses {
8+
9+
/*
10+
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
11+
12+
https://leetcode.com/problems/valid-parentheses/
13+
*/
14+
15+
public static boolean isValid(String s) {
16+
17+
Map<Character, Character> map = Map.of(
18+
'(', ')',
19+
'{', '}',
20+
'[', ']'
21+
);
22+
Stack<Character> stack = new Stack<>();
23+
24+
for (int i = 0; i < s.length(); i++) {
25+
char current = s.charAt(i);
26+
if (map.containsKey(current)) {
27+
stack.push(current);
28+
} else if (stack.isEmpty() || map.get(stack.pop()) != current) {
29+
return false;
30+
}
31+
}
32+
33+
return stack.isEmpty();
34+
}
35+
36+
public static void main(String[] args) {
37+
System.out.println(isValid("()[]{}")); //-> true
38+
System.out.println(isValid("([)]")); //-> false
39+
}
40+
}

0 commit comments

Comments
 (0)