Skip to content

Commit 9bd4514

Browse files
week1 mission done
1 parent 7db46d5 commit 9bd4514

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
3+
* time complexity : O(n)
4+
* space complexity : O(1)
5+
* https://jonghoonpark.com/2024/02/20/leetcode-121
6+
*/
7+
8+
/*
9+
class Solution {
10+
public int maxProfit(int[] prices) {
11+
int buyAt = 0;
12+
int profit = 0;
13+
14+
for(int i = 1; i < prices.length; i++) {
15+
if (prices[i] < prices[buyAt]) {
16+
buyAt = i;
17+
} else {
18+
profit = Math.max(profit, prices[i] - prices[buyAt]);
19+
}
20+
}
21+
22+
return profit;
23+
}
24+
}
25+
*/

contains-duplicate/jonghoonpark.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* https://leetcode.com/problems/contains-duplicate/
3+
* time complexity : O(n)
4+
* space complexity : O(n)
5+
* https://jonghoonpark.com/2024/04/24/leetcode-217
6+
*/
7+
8+
/*
9+
import java.util.HashSet;
10+
import java.util.Set;
11+
12+
void main() {
13+
Solution solution = new Solution();
14+
System.out.println(solution.containsDuplicate(new int[]{1, 2, 3, 1})); // true
15+
System.out.println(solution.containsDuplicate(new int[]{1, 2, 3, 4})); // false
16+
System.out.println(solution.containsDuplicate(new int[]{1, 1, 1, 3, 3, 4, 3, 2, 4, 2})); // true
17+
}
18+
19+
class Solution {
20+
public boolean containsDuplicate(int[] nums) {
21+
Set<Integer> numSet = new HashSet<>();
22+
for (int num : nums) {
23+
if (numSet.contains(num)) {
24+
return true;
25+
}
26+
numSet.add(num);
27+
}
28+
return false;
29+
}
30+
}
31+
*/

two-sum/jonghoonpark.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* https://leetcode.com/problems/two-sum/
3+
* time complexity : O(n)
4+
* space complexity : O(n)
5+
* https://jonghoonpark.com/2024/01/03/leetcode-1
6+
*/
7+
8+
/*
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
class Solution {
13+
public int[] twoSum(int[] nums, int target) {
14+
Map<Integer, Integer> numMap = new HashMap<>();
15+
16+
for (int i = 0; i < nums.length; i++) {
17+
int num = nums[i];
18+
int _target = target - num;
19+
20+
if (numMap.containsKey(_target)) {
21+
return new int[]{numMap.get(_target), i};
22+
}
23+
numMap.put(num, i);
24+
}
25+
26+
return null;
27+
}
28+
}
29+
*/

valid-anagram/jonghoonpark.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* https://leetcode.com/problems/valid-anagram/
3+
* time complexity : O(nlogn)
4+
* space complexity : O(n)
5+
* https://jonghoonpark.com/2024/04/24/leetcode-242
6+
*/
7+
8+
9+
/*
10+
import java.util.Arrays;
11+
12+
void main() {
13+
Solution solution = new Solution();
14+
System.out.println(solution.isAnagram("anagram", "nagaram")); // true
15+
System.out.println(solution.isAnagram("rat", "car")); // false
16+
}
17+
18+
class Solution {
19+
public boolean isAnagram(String s, String t) {
20+
char[] temp1 = s.toCharArray();
21+
char[] temp2 = t.toCharArray();
22+
Arrays.sort(temp1);
23+
Arrays.sort(temp2);
24+
return Arrays.equals(temp1, temp2);
25+
}
26+
}
27+
*/

valid-palindrome/jonghoonpark.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* https://leetcode.com/problems/valid-palindrome/
3+
* time complexity : O(n)
4+
* space complexity : O(n)
5+
* https://jonghoonpark.com/2024/04/24/leetcode-125
6+
*/
7+
8+
/*
9+
10+
void main() {
11+
Solution solution = new Solution();
12+
System.out.println(solution.isPalindrome("A man, a plan, a canal: Panama")); // true
13+
System.out.println(solution.isPalindrome("race a car")); // false
14+
System.out.println(solution.isPalindrome(" ")); // true
15+
}
16+
17+
class Solution {
18+
public boolean isPalindrome(String s) {
19+
StringBuilder filtered = new StringBuilder();
20+
for(char c : s.toCharArray()) {
21+
// '0' == 48, '9' == 57, 'A' == 65, 'z' == 122
22+
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')) {
23+
filtered.append(c);
24+
} else if ((c >= 'A' && c <= 'Z')) {
25+
filtered.append((char) (c + 32));
26+
}
27+
}
28+
29+
for(int i = 0; i < filtered.length() / 2; i++) {
30+
if (filtered.charAt(i) != filtered.charAt(filtered.length() - i - 1)) {
31+
return false;
32+
}
33+
}
34+
35+
return true;
36+
}
37+
}
38+
39+
*/

0 commit comments

Comments
 (0)