Skip to content

Instantly share code, notes, and snippets.

View TheGreatJoules's full-sized avatar
Working from home

Julian Carachure TheGreatJoules

Working from home
  • 09:56 (UTC -07:00)
View GitHub Profile
@TheGreatJoules
TheGreatJoules / Leetcode_438_SolutionA.py
Last active October 19, 2023 06:30
[Leetcode/438A/Python] Find All Anagrams in a String #python #slidingwindow
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
result = []
if len(s) < len(p):
return result
s_freq = [0 for _ in range(26)]
p_freq = [0 for _ in range(26)]
for i in range(len(p)):
@TheGreatJoules
TheGreatJoules / Leetcode_28_Solution_A.java
Last active October 22, 2023 08:05
[Leetcode/28] Find the Index of the First Occurrence in a String #kmp #slidingwindow
class Solution {
public int strStr(String haystack, String needle) {
// Check edge cases and if the needle is greater than the haystack
if (haystack == null || needle == null || (needle.length() > haystack.length())) {
return -1;
}
// Check if the needle is empty
if (needle.length() == 0) {
return 0;
}
@TheGreatJoules
TheGreatJoules / Leetcode_1_Solution_A.java
Last active October 22, 2023 08:05
[Leetcode/1] Two Sum #map #blind75
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
int a = Integer.MIN_VALUE;
int b = Integer.MIN_VALUE;
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(target - nums[i])) {
a = i;
b = map.get(target - nums[i]);
break;
@TheGreatJoules
TheGreatJoules / Leetcode_56_Solution_A.java
Last active October 22, 2023 08:06
[Leetcode/56] Merge Intervals #sorting
class Solution {
public int[][] merge(int[][] intervals) {
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
LinkedList<int[]> result = new LinkedList<>();
for (int[] interval : intervals) {
if (result.isEmpty() || result.getLast()[1] < interval[0]) {
result.add(interval);
} else {
result.getLast()[1] = Math.max(result.getLast()[1], interval[1]);
}
@TheGreatJoules
TheGreatJoules / Leetcode_15_Solution_A.java
Last active October 22, 2023 08:02
[Leetcode/15] 3Sum #twopointers #blind75
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
if (i == 0 || nums[i] != nums[i-1]) {
twoSum(nums, result, i);
}
}
return result;
@TheGreatJoules
TheGreatJoules / Leetcode_121_Solution_A.java
Last active October 22, 2023 08:03
[Leetcode/121] Best Time to Buy and Sell Stock #slidingwindow #blind75
class Solution {
public int maxProfit(int[] prices) {
int min_price = Integer.MAX_VALUE;
int max_profit = Integer.MIN_VALUE;
for (int i = 0; i < prices.length; i++) {
min_price = Math.min(min_price, prices[i]);
max_profit = Math.max(max_profit, prices[i] - min_price);
}
return max_profit;
}
@TheGreatJoules
TheGreatJoules / Leetcode_11_Solution_A.java
Last active October 22, 2023 08:02
[Leetcode/11] Container With Most Water #slidingwindow #blind75
class Solution {
public int maxArea(int[] height) {
int result = 0;
int left = 0;
int right = height.length - 1;
while (left < right) {
result = Math.max(result, Math.min(height[left], height[right]) * (right - left));
if (height[left] < height[right]) {
left++;
} else {
@TheGreatJoules
TheGreatJoules / Leetcode_152_Solution_A.java
Last active October 22, 2023 08:06
[Leetcode/152] Maximum Product Subarray #slidingwindow #array #blind75
class Solution {
public int maxProduct(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int max = nums[0];
int min = nums[0];
int result = nums[0];
for (int i = 1; i < nums.length; i++) {
int current = nums[i];
@TheGreatJoules
TheGreatJoules / Leetcode_53_Solution_A.java
Last active October 22, 2023 08:04
[Leetcode/53] Maximum Subarray #slidingwindow #array #blind75
class Solution {
public int maxSubArray(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int sum = 0;
int result = Integer.MIN_VALUE;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
result = Math.max(result, sum);
@TheGreatJoules
TheGreatJoules / Leetcode_217_Solution_A.java
Last active October 22, 2023 08:05
[Leetcode/217] Contains Duplicate #set #blind75
class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet<>();
for (int num : nums) {
if (set.contains(num)) {
return true;
}
set.add(num);
}
return false;