Skip to content

[박종훈] 1주차 답안 제출 #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions best-time-to-buy-and-sell-stock/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
- https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
- time complexity : O(n)
- space complexity : O(1)
- https://algorithm.jonghoonpark.com/2024/02/20/leetcode-121

```java
class Solution {
public int maxProfit(int[] prices) {
int buyAt = 0;
int profit = 0;

for(int i = 1; i < prices.length; i++) {
if (prices[i] < prices[buyAt]) {
buyAt = i;
} else {
profit = Math.max(profit, prices[i] - prices[buyAt]);
}
}

return profit;
}
}
```
22 changes: 22 additions & 0 deletions contains-duplicate/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
- https://leetcode.com/problems/contains-duplicate/
- time complexity : O(n)
- space complexity : O(n)
- https://algorithm.jonghoonpark.com/2024/04/24/leetcode-217

```java
import java.util.HashSet;
import java.util.Set;

class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> numSet = new HashSet<>();
for (int num : nums) {
if (numSet.contains(num)) {
return true;
}
numSet.add(num);
}
return false;
}
}
```
27 changes: 27 additions & 0 deletions two-sum/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
- https://leetcode.com/problems/two-sum/
- time complexity : O(n)
- space complexity : O(n)
- https://algorithm.jonghoonpark.com/2024/01/03/leetcode-1

```java
import java.util.HashMap;
import java.util.Map;

class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> numMap = new HashMap<>();

for (int i = 0; i < nums.length; i++) {
int num = nums[i];
int _target = target - num;

if (numMap.containsKey(_target)) {
return new int[]{numMap.get(_target), i};
}
numMap.put(num, i);
}

return null;
}
}
```
18 changes: 18 additions & 0 deletions valid-anagram/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
- https://leetcode.com/problems/valid-anagram/
- time complexity : O(nlogn)
- space complexity : O(n)
- https://algorithm.jonghoonpark.com/2024/04/24/leetcode-242

```java
import java.util.Arrays;

class Solution {
public boolean isAnagram(String s, String t) {
char[] temp1 = s.toCharArray();
char[] temp2 = t.toCharArray();
Arrays.sort(temp1);
Arrays.sort(temp2);
return Arrays.equals(temp1, temp2);
}
}
```
28 changes: 28 additions & 0 deletions valid-palindrome/dev-jonghoonpark.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
- https://leetcode.com/problems/valid-palindrome/
- time complexity : O(n)
- space complexity : O(n)
- https://algorithm.jonghoonpark.com/2024/04/24/leetcode-125

```java
class Solution {
public boolean isPalindrome(String s) {
StringBuilder filtered = new StringBuilder();
for(char c : s.toCharArray()) {
// '0' == 48, '9' == 57, 'A' == 65, 'z' == 122
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')) {
filtered.append(c);
} else if ((c >= 'A' && c <= 'Z')) {
filtered.append((char) (c + 32));
}
}

for(int i = 0; i < filtered.length() / 2; i++) {
if (filtered.charAt(i) != filtered.charAt(filtered.length() - i - 1)) {
return false;
}
}

return true;
}
}
```