Skip to content

[bky373] Solutions for week 1 #27

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 1 commit into from
Apr 29, 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
22 changes: 22 additions & 0 deletions best-time-to-buy-and-sell-stock/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
* TC : O(N)
* SC : O(1)
*/
class Solution_0121 {
public int maxProfit(int[] prices) {
int hold = prices[0];
int profit = 0;

for (int i = 1; i < prices.length; i++) {
if (prices[i] < hold) {
hold = prices[i];
}

if (prices[i] - hold > profit) {
profit = prices[i] - hold;
}
}
return profit;
}
}
22 changes: 22 additions & 0 deletions contains-duplicate/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.util.HashMap;
import java.util.Map;

/**
* https://leetcode.com/problems/contains-duplicate/
* TC : O(N)
* SC : O(N)
*/
class Solution_0217 {

public boolean containsDuplicate(int[] nums) {
Map<Integer, Integer> map = new HashMap();
for (int n : nums) {
if (map.containsKey(n)) {
return true;
} else {
map.put(n, 1);
}
}
return false;
}
}
22 changes: 22 additions & 0 deletions two-sum/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.util.HashMap;
import java.util.Map;

/**
* https://leetcode.com/problems/two-sum/
* TC : O(N)
* SC : O(N)
*/
class Solution_0001 {

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

for (int i = 0; i < nums.length; i++) {
if (map.containsKey(target - nums[i])) {
return new int[]{map.get(target - nums[i]), i};
}
map.put(nums[i], i);
}
return new int[]{};
}
}
26 changes: 26 additions & 0 deletions valid-anagram/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* https://leetcode.com/problems/valid-anagram/
* TC : O(N)
* SC : O(1)
*/
class Solution_242 {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}

int[] alpCnt = new int[26];

for (int i=0; i<s.length(); i++) {
alpCnt[s.charAt(i) - 'a']++;
}

for (int i = 0; i < s.length(); i++) {
if (alpCnt[t.charAt(i) - 'a'] < 1) {
return false;
}
alpCnt[t.charAt(i) - 'a']--;
}
return true;
}
}
36 changes: 36 additions & 0 deletions valid-palindrome/bky373.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* https://leetcode.com/problems/valid-palindrome/
* TC : O(N)
* SC : O(1)
*/
class Solution_0125 {

public boolean isPalindrome(String s) {
if (s.isBlank() || s.length() == 1) {
return true;
}
s = s.toLowerCase();

int j = s.length() - 1;
for (int i = 0; i < j; i++) {
if (!isAlpNum(s.charAt(i))) {
continue;
}
if (!isAlpNum(s.charAt(j))) {
i--;
j--;
continue;
}
if (s.charAt(i) != s.charAt(j)) {
return false;
}
j--;
}

return true;
}

public boolean isAlpNum(char c) {
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'z');
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'z' 에는 <= 가 아닌 이유가 있을까요?

Copy link
Contributor Author

@bky373 bky373 Apr 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아고 제가 생각 정리한다고 코드를 지우고 재작성했다가 빠뜨렸군요 꼼꼼히 봐주셔서 감사합니다!
수정해서 포스 푸시했습니다!

}