Skip to content

Commit 496d9e4

Browse files
authored
Merge branch 'DaleStudy:main' into main
2 parents 54a2f15 + 74db2c7 commit 496d9e4

File tree

10 files changed

+304
-0
lines changed

10 files changed

+304
-0
lines changed

โ€Ž3sum/bky373.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* time: O(n^2)
3+
* space: O(n)
4+
*
5+
* - time: becasue of two nested loop and inner loop having a linear time complexity.
6+
* - space: because of a HashSet to store the triplets.
7+
*/
8+
class Solution {
9+
10+
public List<List<Integer>> threeSum(int[] nums) {
11+
Arrays.sort(nums);
12+
int i = 0, j, k;
13+
int ni = 0, nj, nk;
14+
Set<List<Integer>> res = new HashSet<>();
15+
while (i < nums.length && ni <= 0) {
16+
ni = nums[i];
17+
j = i + 1;
18+
k = nums.length - 1;
19+
while (j < k) {
20+
nj = nums[j];
21+
nk = nums[k];
22+
int sum = ni + nj + nk;
23+
if (sum < 0) {
24+
j++;
25+
} else if (sum > 0) {
26+
k--;
27+
} else {
28+
res.add(List.of(ni, nj, nk));
29+
j++;
30+
}
31+
}
32+
i++;
33+
}
34+
return res.stream()
35+
.toList();
36+
}
37+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
- ๋ฌธ์ œ: https://leetcode.com/problems/container-with-most-water/
2+
- time complexity : O(n)
3+
- space complexity : O(1)
4+
- ๋ธ”๋กœ๊ทธ ์ฃผ์†Œ : https://algorithm.jonghoonpark.com/2024/06/02/leetcode-11
5+
6+
```java
7+
class Solution {
8+
public int maxArea(int[] height) {
9+
int start = 0;
10+
int end = height.length - 1;
11+
12+
int max = getArea(height, start, end);
13+
14+
while(start < end) {
15+
if(height[start] >= height[end]) {
16+
end--;
17+
max = Math.max(max, getArea(height, start, end));
18+
} else {
19+
start++;
20+
max = Math.max(max, getArea(height, start, end));
21+
}
22+
}
23+
24+
return max;
25+
}
26+
27+
public int getArea(int[] height, int start, int end) {
28+
if (start == end) {
29+
return 0;
30+
}
31+
return getMinHeight(height[end], height[start]) * (end - start);
32+
}
33+
34+
public int getMinHeight(int height1, int height2) {
35+
return Math.min(height1, height2);
36+
}
37+
}
38+
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* time: O(N)
3+
* space: O(N)
4+
*/
5+
public class Codec {
6+
7+
// Encodes a list of strings to a single string.
8+
public String encode(List<String> strs) {
9+
StringBuilder sb = new StringBuilder();
10+
for (String str : strs) {
11+
sb.append(str.length())
12+
.append(':')
13+
.append(str);
14+
}
15+
return sb.toString();
16+
}
17+
18+
// Decodes a single string to a list of strings.
19+
public List<String> decode(String s) {
20+
List<String> decoded = new ArrayList<>();
21+
int i = 0;
22+
while (i < s.length()) {
23+
int searchIndex = s.indexOf(':', i);
24+
int chunkSize = Integer.parseInt(s.substring(i, searchIndex));
25+
i = searchIndex + chunkSize + 1;
26+
decoded.add(s.substring(searchIndex + 1, i));
27+
}
28+
return decoded;
29+
}
30+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
- ๋ฌธ์ œ: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
2+
- time complexity : O(log n)
3+
- space complexity : O(1)
4+
- ๋ธ”๋กœ๊ทธ ์ฃผ์†Œ : https://algorithm.jonghoonpark.com/2024/06/03/leetcode-153
5+
6+
```java
7+
public int findMin(int[] nums) {
8+
int left = 0, right = nums.length - 1;
9+
while(left < right) {
10+
int mid = left + (right - left) / 2;
11+
12+
if(nums[mid] < nums[right]) {
13+
right = mid;
14+
} else {
15+
left = mid + 1;
16+
}
17+
}
18+
19+
return nums[left];
20+
}
21+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* time: O(N)
3+
* space: O(N)
4+
*/
5+
class Solution {
6+
7+
public int longestConsecutive(int[] nums) {
8+
Set<Integer> numSet = new HashSet<>();
9+
for (int n : nums) {
10+
numSet.add(n);
11+
}
12+
int longest = 0;
13+
for (int n : nums) {
14+
if (numSet.contains(n - 1)) {
15+
continue;
16+
}
17+
int seq = 1;
18+
while (numSet.contains(n + seq)) {
19+
seq++;
20+
}
21+
longest = Math.max(longest, seq);
22+
}
23+
return longest;
24+
}
25+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
- ๋ฌธ์ œ: https://leetcode.com/problems/longest-repeating-character-replacement/
2+
- time complexity : O(n)
3+
- space complexity : O(1)
4+
- ๋ธ”๋กœ๊ทธ ์ฃผ์†Œ : https://algorithm.jonghoonpark.com/2024/04/29/leetcode-424
5+
6+
```java
7+
class Solution {
8+
public int characterReplacement(String s, int k) {
9+
int[] counter = new int[26];
10+
int countOfMostFrequent = -1;
11+
12+
int headPointer = 0;
13+
int tailPointer = 0;
14+
15+
int maxLength = 0;
16+
17+
while (headPointer < s.length()) {
18+
char head = s.charAt(headPointer);
19+
char tail = s.charAt(tailPointer);
20+
counter[head - 'A']++;
21+
headPointer++;
22+
23+
countOfMostFrequent = Math.max(counter[head - 'A'], countOfMostFrequent);
24+
while (headPointer - tailPointer > countOfMostFrequent + k) {
25+
counter[tail - 'A']--;
26+
tailPointer++;
27+
tail = s.charAt(tailPointer);
28+
}
29+
30+
maxLength = Math.max(maxLength, headPointer - tailPointer);
31+
}
32+
33+
return maxLength;
34+
}
35+
}
36+
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
- ๋ฌธ์ œ: https://leetcode.com/problems/longest-substring-without-repeating-characters/
2+
- time complexity : O(n)
3+
- space complexity : O(n)
4+
- ๋ธ”๋กœ๊ทธ ์ฃผ์†Œ : https://algorithm.jonghoonpark.com/2024/02/18/leetcode-3
5+
6+
```java
7+
class Solution {
8+
public int lengthOfLongestSubstring(String s) {
9+
if (s.isEmpty()) {
10+
return 0;
11+
}
12+
13+
Set<Character> set = new HashSet<>();
14+
int pointer = 0;
15+
int longest = 0;
16+
17+
while (pointer < s.length()) {
18+
char _char = s.charAt(pointer);
19+
while (set.contains(_char)) {
20+
set.remove(s.charAt(pointer - set.size()));
21+
}
22+
set.add(_char);
23+
longest = Math.max(longest, set.size());
24+
pointer++;
25+
}
26+
27+
return longest;
28+
}
29+
}
30+
```
31+
32+
## TC ์ถ”๊ฐ€ ์„ค๋ช…
33+
34+
๋‚ด๋ถ€ while์ด ์žˆ์ง€๋งŒ O(n)์œผ๋กœ ์ ์„ ์ˆ˜ ์žˆ๋Š” ์ด์œ ๋Š” hashset์˜ ๊ฒฝ์šฐ search ํ•˜๋Š”๋ฐ ๋“œ๋Š” ๋น„์šฉ์ด O(1) ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* time: O(N)
3+
* space: O(N)
4+
*/
5+
class Solution {
6+
public int[] productExceptSelf(int[] nums) {
7+
int len = nums.length;
8+
int[] res = new int[len];
9+
int[] left = Arrays.copyOf(nums, len);
10+
int[] right = Arrays.copyOf(nums, len);
11+
for (int i = 1; i < len; i++) {
12+
left[i] *= left[i - 1];
13+
}
14+
for (int i = len - 2; i >= 0; i--) {
15+
right[i] *= right[i + 1];
16+
}
17+
for (int i = 1; i < len - 1; i++) {
18+
res[i] = left[i - 1] * right[i + 1];
19+
}
20+
res[0] = right[1];
21+
res[len - 1] = left[len - 2];
22+
return res;
23+
}
24+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
- ๋ฌธ์ œ: https://leetcode.com/problems/search-in-rotated-sorted-array/
2+
- time complexity : O(log n)
3+
- space complexity : O(1)
4+
- ๋ธ”๋กœ๊ทธ ์ฃผ์†Œ : https://algorithm.jonghoonpark.com/2024/06/03/leetcode-33
5+
6+
```java
7+
public int search(int[] nums, int target) {
8+
int left = 0, right = nums.length - 1;
9+
while(left <= right) {
10+
int mid = left + (right - left) / 2;
11+
12+
if (nums[mid] == target) {
13+
return mid;
14+
}
15+
16+
if(nums[mid] < nums[right]) {
17+
if (target < nums[mid] || target > nums[right]) {
18+
right = mid - 1;
19+
} else {
20+
left = mid + 1;
21+
}
22+
} else {
23+
if (target < nums[left] || target > nums[mid]) {
24+
left = mid + 1;
25+
} else {
26+
right = mid - 1;
27+
}
28+
}
29+
}
30+
31+
return -1;
32+
}
33+
```
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* time: O(N log k)
3+
* space: O(N + k)
4+
*
5+
* - time: N ๊ฐœ๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ heap ์— ์š”์†Œ ์ถ”๊ฐ€, ์ตœ๋Œ€ k ๊ฐœ๋งŒํผ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ log k ๋งŒํผ ์‹œ๊ฐ„์ด ๊ณฑํ•ด์ง.
6+
* - space: ๋นˆ๋„ ์ˆ˜๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด N, ํž™์— ์š”์†Œ๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด K ๋งŒํผ ์ €์žฅ ๊ณต๊ฐ„์„ ์‚ฌ์šฉํ•จ.
7+
*/
8+
class Solution {
9+
10+
public int[] topKFrequent(int[] nums, int k) {
11+
Map<Integer, Integer> freqMap = new HashMap<>();
12+
for (int n : nums) {
13+
freqMap.put(n, freqMap.getOrDefault(n, 0) + 1);
14+
}
15+
Queue<Integer> pq = new PriorityQueue<>(Comparator.comparingInt(freqMap::get));
16+
for (Integer n : freqMap.keySet()) {
17+
pq.add(n);
18+
if (pq.size() > k) {
19+
pq.poll();
20+
}
21+
}
22+
return IntStream.range(0, k)
23+
.map(i -> pq.poll())
24+
.toArray();
25+
}
26+
}

0 commit comments

Comments
ย (0)