-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #630 from GangBean/main
[GangBean] Week 1
- Loading branch information
Showing
6 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|