diff --git a/climbing-stairs/dohee789.java b/climbing-stairs/dohee789.java new file mode 100644 index 0000000000..93f42b6c43 --- /dev/null +++ b/climbing-stairs/dohee789.java @@ -0,0 +1,23 @@ +/* +https://leetcode.com/problems/climbing-stairs/ + */ +class Solution { + public int climbStairs(int n) { + if (n <= 2) return n; + + int a = 1; + int b = 2; + int result = 0; + + for (int i = 3; i <= n; i++){ + result = a + b; + a = b; + b = result; + } + + return result; + } +} + + + diff --git a/contains-duplicate/dohee789.java b/contains-duplicate/dohee789.java new file mode 100644 index 0000000000..7c9e8cc9cb --- /dev/null +++ b/contains-duplicate/dohee789.java @@ -0,0 +1,17 @@ +/* +https://leetcode.com/problems/contains-duplicate/ + */ +class Solution { + public boolean containsDuplicate(int[] nums) { + Set set = new HashSet(); + + for(int n: nums){ + if(set.contains(n)){ + return true; + } + set.add(n); + } + + return false; + } +} diff --git a/house-robber/dohee789.java b/house-robber/dohee789.java new file mode 100644 index 0000000000..c820e003a5 --- /dev/null +++ b/house-robber/dohee789.java @@ -0,0 +1,21 @@ +/* +https://leetcode.com/problems/house-robber/ + */ +class Solution{ + public int rob(int[] nums){ + if(nums.length == 0 || nums == null) return 0; + if(nums.length == 1) return 0; + + int prev1 = 0; + int prev2 = 0; + for(int num: nums){ + int temp = Math.max(prev1, prev2+num); + prev2 = prev1; + prev1 = temp; + } + + return prev1; + } +} + + diff --git a/longest-consecutive-sequence/dohee789.java b/longest-consecutive-sequence/dohee789.java new file mode 100644 index 0000000000..eaf7ce6d92 --- /dev/null +++ b/longest-consecutive-sequence/dohee789.java @@ -0,0 +1,26 @@ +/* +https://leetcode.com/problems/longest-consecutive-sequence/ + */ +class Solution { + public int longestConsecutive(int[] nums) { + Set set = new HashSet<>(); + for(int num: nums){ + set.add(num); + } + + int maxLength = 0; + for(int num: set){ + if(!set.contains(num-1)){ + int currentNum = num; + int length = 1; + while(set.contains(currentNum+1)){ + currentNum++; + length++; + } + maxLength = Math.max(maxLength,length); + } + } + return maxLength; + } +} + diff --git a/product-of-array-except-self/dohee789.java b/product-of-array-except-self/dohee789.java new file mode 100644 index 0000000000..1048aeee27 --- /dev/null +++ b/product-of-array-except-self/dohee789.java @@ -0,0 +1,25 @@ +/* +https://leetcode.com/problems/product-of-array-except-self/ + */ +class Solution { + public int[] productExceptSelf(int[] nums) { + int n = nums.length; + int[] result = new int[n]; + + result[0] = 1; + for (int i = 1; i < n; i++) { + result[i] = result[i - 1] * nums[i - 1]; + } + + int right = 1; + for (int i = n - 1; i >= 0; i--) { + result[i] *= right; + right *= nums[i]; + } + + return result; + } +} + + + diff --git a/top-k-frequent-elements/dohee789.java b/top-k-frequent-elements/dohee789.java new file mode 100644 index 0000000000..9de7f19748 --- /dev/null +++ b/top-k-frequent-elements/dohee789.java @@ -0,0 +1,31 @@ +/* +https://leetcode.com/problems/top-k-frequent-elements/ + */ +class Solution { + public int[] topKFrequent(int[] nums, int k) { + Map freqMap = new HashMap<>(); + for(int num: nums){ + freqMap.put(num, freqMap.getOrDefault(num,0)+1); + } + + PriorityQueue> minHeap = new PriorityQueue<>( + Comparator.comparingInt(Map.Entry::getValue) + ); + + for (Map.Entry entry : freqMap.entrySet()) { + minHeap.offer(entry); + if (minHeap.size() > k) { + minHeap.poll(); + } + } + + int[] result = new int[k]; + for (int i = 0; i < k; i++) { + result[i] = minHeap.poll().getKey(); + } + + return result; + + } +} + diff --git a/two-sum/dohee789.java b/two-sum/dohee789.java new file mode 100644 index 0000000000..08afb456a7 --- /dev/null +++ b/two-sum/dohee789.java @@ -0,0 +1,19 @@ +/* +https://leetcode.com/problems/two-sum/ + */ +class Solution { + 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])) { + int j = map.get(target - nums[i]); + return new int[]{j, i}; + } + map.put(nums[i], i); + } + + return null; + } +} + diff --git a/valid-anagram/dohee789.java b/valid-anagram/dohee789.java new file mode 100644 index 0000000000..b394ec91ca --- /dev/null +++ b/valid-anagram/dohee789.java @@ -0,0 +1,19 @@ +/* +https://leetcode.com/problems/valid-anagram/description/ + */ +class Solution { + public boolean isAnagram(String s, String t) { + if (s.length() != t.length()) return false; + + char a[] = s.toCharArray(); + char b[] = t.toCharArray(); + + Array.sort(a); + Array.sort(b); + + returnArray.equals(a,b); + } +} + + +