Skip to content

Commit 386bcd0

Browse files
committed
solve week 1 problems
- 0001. Two Sum - 0121. Best Time to Buy and Sell Stock - 0125. Valid Palindrome - 0217. Contains Duplicate - 0242. Valid Anagram
1 parent df37585 commit 386bcd0

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
3+
* TC : O(N)
4+
* SC : O(1)
5+
*/
6+
class Solution_0121 {
7+
public int maxProfit(int[] prices) {
8+
int hold = prices[0];
9+
int profit = 0;
10+
11+
for (int i = 1; i < prices.length; i++) {
12+
if (prices[i] < hold) {
13+
hold = prices[i];
14+
}
15+
16+
if (prices[i] - hold > profit) {
17+
profit = prices[i] - hold;
18+
}
19+
}
20+
return profit;
21+
}
22+
}

contains-duplicate/bky373.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
/**
5+
* https://leetcode.com/problems/contains-duplicate/
6+
* TC : O(N)
7+
* SC : O(N)
8+
*/
9+
class Solution_0217 {
10+
11+
public boolean containsDuplicate(int[] nums) {
12+
Map<Integer, Integer> map = new HashMap();
13+
for (int n : nums) {
14+
if (map.containsKey(n)) {
15+
return true;
16+
} else {
17+
map.put(n, 1);
18+
}
19+
}
20+
return false;
21+
}
22+
}

two-sum/bky373.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
/**
5+
* https://leetcode.com/problems/two-sum/
6+
* TC : O(N)
7+
* SC : O(N)
8+
*/
9+
class Solution_0001 {
10+
11+
public int[] twoSum(int[] nums, int target) {
12+
Map<Integer, Integer> map = new HashMap<>();
13+
14+
for (int i = 0; i < nums.length; i++) {
15+
if (map.containsKey(target - nums[i])) {
16+
return new int[]{map.get(target - nums[i]), i};
17+
}
18+
map.put(nums[i], i);
19+
}
20+
return new int[]{};
21+
}
22+
}

valid-anagram/bky373.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* https://leetcode.com/problems/valid-anagram/
3+
* TC : O(N)
4+
* SC : O(1)
5+
*/
6+
class Solution_242 {
7+
public boolean isAnagram(String s, String t) {
8+
if (s.length() != t.length()) {
9+
return false;
10+
}
11+
12+
int[] alpCnt = new int[26];
13+
14+
for (int i=0; i<s.length(); i++) {
15+
alpCnt[s.charAt(i) - 'a']++;
16+
}
17+
18+
for (int i = 0; i < s.length(); i++) {
19+
if (alpCnt[t.charAt(i) - 'a'] < 1) {
20+
return false;
21+
}
22+
alpCnt[t.charAt(i) - 'a']--;
23+
}
24+
return true;
25+
}
26+
}

valid-palindrome/bky373.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* https://leetcode.com/problems/valid-palindrome/
3+
* TC : O(N)
4+
* SC : O(1)
5+
*/
6+
class Solution_0125 {
7+
8+
public boolean isPalindrome(String s) {
9+
if (s.isBlank() || s.length() == 1) {
10+
return true;
11+
}
12+
s = s.toLowerCase();
13+
14+
int j = s.length() - 1;
15+
for (int i = 0; i < j; i++) {
16+
if (!isAlpNum(s.charAt(i))) {
17+
continue;
18+
}
19+
if (!isAlpNum(s.charAt(j))) {
20+
i--;
21+
j--;
22+
continue;
23+
}
24+
if (s.charAt(i) != s.charAt(j)) {
25+
return false;
26+
}
27+
j--;
28+
}
29+
30+
return true;
31+
}
32+
33+
public boolean isAlpNum(char c) {
34+
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'z');
35+
}
36+
}

0 commit comments

Comments
 (0)