Skip to content

Commit

Permalink
Merge pull request #630 from GangBean/main
Browse files Browse the repository at this point in the history
[GangBean] Week 1
  • Loading branch information
GangBean authored Dec 14, 2024
2 parents dd891b6 + b58d47d commit ed353c4
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 0 deletions.
12 changes: 12 additions & 0 deletions contains-duplicate/GangBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import java.util.Arrays;
import java.util.stream.Collectors;

class Solution {
public boolean containsDuplicate(int[] nums) {
/***
compare length of array and length of set.
O(n) given that n is length of array nums
*/
return nums.length > Arrays.stream(nums).boxed().collect(Collectors.toSet()).size();
}
}
28 changes: 28 additions & 0 deletions house-robber/GangBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public int rob(int[] nums) {
/**
r[0] = a[0]
r[1] = max(a[1], r[0])
r[2] = max(r[1], a[2] + r[0])
r[3] = max(r[2], a[3] + r[1])
...
r[k] = max(r[k-1], a[k] + r[k-2]) O(1)
*/
int[] r = new int[nums.length];

for (int i = 0; i < nums.length; i++) { // O(N)
if (i == 0) {
r[i] = nums[i];
continue;
}
if (i == 1) {
r[i] = Math.max(nums[i], r[i-1]);
continue;
}
r[i] = Math.max(r[i-1], nums[i] + r[i-2]);
}

return r[nums.length - 1];
}
}

23 changes: 23 additions & 0 deletions longest-consecutive-sequence/GangBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.util.*;
import java.util.stream.Collectors;

class Solution {
public int longestConsecutive(int[] nums) {
Set<Integer> set = Arrays.stream(nums).boxed().collect(Collectors.toSet());

int maxLength = 0;
for (int num: nums) {
// ๊ฐ ์ˆซ์ž์— ๋Œ€ํ•ด ์ตœ์ดˆ ๊ฐ’์ด ๊ฐ€๋Šฅํ•˜๋ฉด, ์ฆ‰ num-1์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด ์ตœ๋Œ€ length ๊ตฌํ•˜๊ธฐ
if (set.contains(num - 1)) continue;
int length = 1;
int start = num;
while (set.contains(start + 1)) {
length++;
start++;
}
maxLength = Math.max(length, maxLength);
}

return maxLength;
}
}
29 changes: 29 additions & 0 deletions palindromic-substrings/GangBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public int countSubstrings(String s) {
/**
๊ฐ ๋ฌธ์ž๋ฅผ ์ค‘๊ฐ„์œผ๋กœ ๊ฐ–๋Š” palindrome ์—ฌ๋ถ€ ์ฒดํฌ
+ ๋‘๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ์ค‘๊ฐ„์œผ๋กœ ๊ฐ–๋Š” palindrome ์—ฌ๋ถ€ ์ฒดํฌ
*/
int count = 0;
int length = s.length();
for (int i = 0; i < length; i++) { // O(N)
int start = i;
int end = i;
while (0 <= start && end < length && start <= end && s.charAt(start) == s.charAt(end)) { // O(N)
count++;
start--;
end++;
}

start = i;
end = i + 1;
while (0 <= start && end < length && start <= end && s.charAt(start) == s.charAt(end)) { // O(N)
count++;
start--;
end++;
}
}

return count; // O(N^2)
}
}
47 changes: 47 additions & 0 deletions top-k-frequent-elements/GangBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.util.*;
import java.util.stream.Collectors;

class Solution {
public int[] topKFrequent(int[] nums, int k) {
/**
k๋ฒˆ์งธ๋กœ ๋งŽ์ด ๋“ฑ์žฅํ•œ ์ˆซ์ž๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ.
๊ตฌํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ.
1. ๋“ฑ์žฅํšŸ์ˆ˜์˜ ์ˆœ์œ„
2. k๋ฒˆ์งธ ์ˆœ์œ„์— ํ•ด๋‹นํ•˜๋Š” ์ˆ˜์˜ ๊ฐ’
1. ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•: ์ „์ฒด๋ฅผ ์ˆœํšŒํ•ด ์ˆซ์ž๋ณ„ ๋“ฑ์žฅ ํšŸ์ˆ˜๋ฅผ ์ €์žฅ -> O(N)
2. ๋“ฑ์žฅ ํšŸ์ˆ˜์˜ ์ˆœ์œ„๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•: ๋“ฑ์žฅ ํšŸ์ˆ˜์— ๋Œ€ํ•ด sort
-> O(MlogM) where N = M(M+1) / 2
-> logN = logM + log(M+1) + C
-> M = N^(1/2)
-> O(MlogM) = O(N^1/2logN) (??? ์—ฌ๊ธฐ๊ฐ€ ๋งž๋Š”์ง€ ๋ชจ๋ฅด๊ฒ ๋„ค์š”..)
3. ์—ญ ์ธ๋ฑ์Šค๋ฅผ ๊ตฌํ•ด ๋งคํ•‘๋˜๋Š” ์ˆ˜๋ฅผ ๊ตฌํ•จ -> O(N)
์ „์ฒด: O(N)
*/
Map<Integer, Integer> numToCount = new HashMap<>();
for (int num : nums) { // O(N)
numToCount.put(num, numToCount.getOrDefault(num, 0) + 1);
}

Map<Integer, List<Integer>> countToNum = new HashMap<>();
for (Map.Entry<Integer, Integer> entry: numToCount.entrySet()) {
List<Integer> numList = countToNum.getOrDefault(entry.getValue(), new ArrayList<>());
numList.add(entry.getKey());
countToNum.put(entry.getValue(), numList);
} // O(logN): because sum of all counts is equal to N

List<Integer> countRank = countToNum.keySet().stream().sorted(Collections.reverseOrder()).collect(Collectors.toList());
// O(MlogM) where N = M(M+1) / 2

int[] ret = new int[k];
int idx = 0;
int rank = 0;
while (idx < k) { // O(k) <= O(N)
for (int num : countToNum.get(countRank.get(rank++))) {
ret[idx++] = num;
}
}
return ret;
}
}
20 changes: 20 additions & 0 deletions valid-palindrome/GangBean.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public boolean isPalindrome(String s) {
/**
* Step 1: Convert the input string to lowercase. -> O(n)
* Step 2: Remove all non-alphanumeric characters using regex. -> O(n)
* Step 3: Use two pointers to compare characters from both ends of the string. -> O(n)
* Total time complexity: O(n), where n is the length of the string.
*/
s = s.toLowerCase();
s = s.replaceAll("[^a-z0-9]", "");
int left = 0;
int right = s.length() - 1;
if (right <= 0) return true;
while (left < right) {
if (s.charAt(left++) != s.charAt(right--)) return false;
}
return true;
}
}

0 comments on commit ed353c4

Please sign in to comment.