From 386bcd06cc3d5028fdee6e4be748698dc7f2c638 Mon Sep 17 00:00:00 2001 From: borahm-lee Date: Sat, 27 Apr 2024 22:36:52 +0900 Subject: [PATCH] solve week 1 problems - 0001. Two Sum - 0121. Best Time to Buy and Sell Stock - 0125. Valid Palindrome - 0217. Contains Duplicate - 0242. Valid Anagram --- best-time-to-buy-and-sell-stock/bky373.java | 22 +++++++++++++ contains-duplicate/bky373.java | 22 +++++++++++++ two-sum/bky373.java | 22 +++++++++++++ valid-anagram/bky373.java | 26 +++++++++++++++ valid-palindrome/bky373.java | 36 +++++++++++++++++++++ 5 files changed, 128 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/bky373.java create mode 100644 contains-duplicate/bky373.java create mode 100644 two-sum/bky373.java create mode 100644 valid-anagram/bky373.java create mode 100644 valid-palindrome/bky373.java diff --git a/best-time-to-buy-and-sell-stock/bky373.java b/best-time-to-buy-and-sell-stock/bky373.java new file mode 100644 index 000000000..33e293ec4 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/bky373.java @@ -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; + } +} diff --git a/contains-duplicate/bky373.java b/contains-duplicate/bky373.java new file mode 100644 index 000000000..22938e842 --- /dev/null +++ b/contains-duplicate/bky373.java @@ -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 map = new HashMap(); + for (int n : nums) { + if (map.containsKey(n)) { + return true; + } else { + map.put(n, 1); + } + } + return false; + } +} diff --git a/two-sum/bky373.java b/two-sum/bky373.java new file mode 100644 index 000000000..e6e1a2d85 --- /dev/null +++ b/two-sum/bky373.java @@ -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 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[]{}; + } +} diff --git a/valid-anagram/bky373.java b/valid-anagram/bky373.java new file mode 100644 index 000000000..ce3fa5120 --- /dev/null +++ b/valid-anagram/bky373.java @@ -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