Skip to content

Commit ed72dca

Browse files
committed
Update
1 parent d34d303 commit ed72dca

18 files changed

+869
-4
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package array;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
/**
8+
* Description: https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence
9+
* Difficulty: Easy
10+
*/
11+
public class CanMakeArithmeticProgressionFromSequence {
12+
13+
/**
14+
* Time complexity: O(nlog n)
15+
* Space complexity: O(log n)
16+
*/
17+
public boolean canMakeArithmeticProgressionViaSorting(int[] arr) {
18+
Arrays.sort(arr);
19+
20+
int diff = arr[1] - arr[0];
21+
for (int i = 2; i < arr.length; i++) {
22+
if (arr[i] - arr[i - 1] != diff) return false;
23+
}
24+
25+
return true;
26+
}
27+
28+
/**
29+
* Time complexity: O(n)
30+
* Space complexity: O(n)
31+
*/
32+
public boolean canMakeArithmeticProgressionViaSet(int[] arr) {
33+
int min = Integer.MAX_VALUE;
34+
int max = Integer.MIN_VALUE;
35+
36+
for (int num : arr) {
37+
min = Math.min(min, num);
38+
max = Math.max(max, num);
39+
}
40+
41+
if (max == min) return true; // all elements are equal
42+
if ((max - min) % (arr.length - 1) != 0) return false;
43+
44+
int diff = (max - min) / (arr.length - 1);
45+
Set<Integer> seen = new HashSet<>();
46+
for (int num : arr) {
47+
if (!seen.add(num)) return false; // contains duplicates
48+
if ((max - num) % diff != 0) return false;
49+
}
50+
51+
return true;
52+
}
53+
}

src/array/MaxConsecutiveOnes.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package array;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/max-consecutive-ones
5+
* Difficulty: Easy
6+
* Time complexity: O(n)
7+
* Space complexity: O(1)
8+
*/
9+
public class MaxConsecutiveOnes {
10+
11+
public int findMaxConsecutiveOnes(int[] nums) {
12+
int count = 0;
13+
int max = 0;
14+
15+
for (int num : nums) {
16+
if (num == 1) {
17+
max = Math.max(max, count);
18+
count++;
19+
} else {
20+
count = 0;
21+
}
22+
}
23+
24+
return max;
25+
}
26+
}

src/array/MaxConsecutiveOnes2.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package array;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/max-consecutive-ones-ii
8+
* Difficulty: Medium
9+
* Time complexity: O(n)
10+
* Space complexity: O(1)
11+
*/
12+
public class MaxConsecutiveOnes2 {
13+
14+
public int findMaxConsecutiveOnes(int[] nums) {
15+
int flips = 1;
16+
int left = 0;
17+
int right = 0;
18+
while (right < nums.length) {
19+
if (nums[right] == 0) flips--;
20+
if (flips < 0) {
21+
if (nums[left] == 0) flips++;
22+
left++;
23+
}
24+
25+
right++;
26+
}
27+
28+
return right - left;
29+
}
30+
31+
public int findMaxConsecutiveOnesForDataStream(int[] nums) {
32+
int flips = 1;
33+
int left = 0;
34+
int max = 0;
35+
36+
Queue<Integer> zeroPositions = new LinkedList<>();
37+
for (int right = 0; right < nums.length; right++) {
38+
if (nums[right] == 0) zeroPositions.offer(right);
39+
if (zeroPositions.size() > flips) {
40+
left = zeroPositions.poll() + 1;
41+
}
42+
43+
max = Math.max(max, right - left + 1);
44+
}
45+
46+
return max;
47+
}
48+
}

src/array/MaxConsecutiveOnes3.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package array;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/max-consecutive-ones-iii
8+
* Difficulty: Medium
9+
*/
10+
public class MaxConsecutiveOnes3 {
11+
12+
/**
13+
* Time complexity: O(n)
14+
* Space complexity: O(1)
15+
*/
16+
public int longestOnes(int[] nums, int k) {
17+
int left = 0;
18+
int right = 0;
19+
while (right < nums.length) {
20+
if (nums[right] == 0) k--;
21+
if (k < 0) {
22+
if (nums[left] == 0) k++;
23+
left++;
24+
}
25+
26+
right++;
27+
}
28+
29+
return right - left;
30+
}
31+
32+
/**
33+
* Time complexity: O(n)
34+
* Space complexity: O(k)
35+
*/
36+
public int longestOnesForDataStream(int[] nums, int k) {
37+
int left = 0;
38+
int max = 0;
39+
40+
Queue<Integer> zeroPositions = new LinkedList<>();
41+
for (int right = 0; right < nums.length; right++) {
42+
if (nums[right] == 0) zeroPositions.offer(right);
43+
if (zeroPositions.size() > k) {
44+
left = zeroPositions.poll() + 1;
45+
}
46+
47+
max = Math.max(max, right - left + 1);
48+
}
49+
50+
return max;
51+
}
52+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package binary_search;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/find-smallest-letter-greater-than-target
5+
* Difficulty: Easy
6+
*/
7+
public class FindSmallestLetterGreaterThanTarget {
8+
9+
/**
10+
* Time complexity: O(nlog n)
11+
* Space complexity: O(1)
12+
*/
13+
public char nextGreatestLetterViaBinarySearch(char[] letters, char target) {
14+
int left = 0;
15+
int right = letters.length - 1;
16+
17+
18+
int result = 0;
19+
while (left <= right) {
20+
int mid = left + (right - left) / 2;
21+
if (letters[mid] > target) {
22+
result = mid;
23+
right = mid - 1;
24+
} else {
25+
left = mid + 1;
26+
}
27+
}
28+
29+
return letters[result];
30+
}
31+
32+
/**
33+
* Time complexity: O(n)
34+
* Space complexity: O(1)
35+
*/
36+
public char nextGreatestLetterViaBruteForce(char[] letters, char target) {
37+
for (char c : letters) {
38+
if (c > target) return c;
39+
}
40+
41+
return letters[0];
42+
}
43+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package design;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Description: https://leetcode.com/problems/design-in-memory-file-system
7+
* Difficulty: Hard
8+
* Time complexity: O(n)
9+
* Space complexity: O(n)
10+
*/
11+
public class DesignInMemoryFileSystem {
12+
13+
private static class FileSystemViaTrie {
14+
15+
private final Node trie;
16+
17+
public FileSystemViaTrie() {
18+
this.trie = new Node();
19+
trie.children.put("", new Node());
20+
}
21+
22+
public List<String> ls(String path) {
23+
// "/".split("/") returns an empty array
24+
String[] dirs = path.equals("/") ? new String[]{""} : path.split("/");
25+
26+
Node found = find(dirs);
27+
if (found == null) return List.of();
28+
29+
if (found.isFile) {
30+
// return file name
31+
return List.of(dirs[dirs.length - 1]);
32+
}
33+
34+
return found.children.keySet().stream()
35+
.sorted()
36+
.toList();
37+
}
38+
39+
public void mkdir(String path) {
40+
String[] dirs = path.split("/");
41+
findOrCreate(dirs);
42+
}
43+
44+
public void addContentToFile(String filePath, String content) {
45+
String[] dirs = filePath.split("/");
46+
47+
Node found = findOrCreate(dirs);
48+
found.isFile = true;
49+
found.content.append(content);
50+
}
51+
52+
public String readContentFromFile(String filePath) {
53+
String[] dirs = filePath.split("/");
54+
55+
Node found = find(dirs);
56+
if (found == null) return "";
57+
58+
return found.content.toString();
59+
}
60+
61+
private Node find(String[] path) {
62+
Node current = trie;
63+
for (String dir : path) {
64+
Node child = current.children.get(dir);
65+
if (child == null) return null;
66+
current = child;
67+
}
68+
69+
return current;
70+
}
71+
72+
private Node findOrCreate(String[] path) {
73+
Node current = trie;
74+
for (String dir : path) {
75+
Node child = current.children.computeIfAbsent(dir, __ -> new Node());
76+
current = child;
77+
}
78+
79+
return current;
80+
}
81+
82+
private static class Node {
83+
84+
private final Map<String, Node> children;
85+
private final StringBuilder content;
86+
private boolean isFile;
87+
88+
public Node() {
89+
this.children = new HashMap<>();
90+
this.content = new StringBuilder();
91+
}
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)