Skip to content

Commit

Permalink
add leetcode
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrikSD committed Mar 6, 2024
1 parent cdcf1bf commit 6fd7ca2
Show file tree
Hide file tree
Showing 19 changed files with 784 additions and 0 deletions.
49 changes: 49 additions & 0 deletions projects/Leetcode/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar

.settings/

.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace

# Local History for Visual Studio Code
.history/
.project
.classpath
129 changes: 129 additions & 0 deletions projects/Leetcode/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Need to Revisit

- 1775 - [Equal Sum Arrays With Minimum Number of Operations](https://leetcode.com/problems/equal-sum-arrays-with-minimum-number-of-operations/)
Topics: Array, Hash Table, Greedy, Counting

- 337 - [House Robber III](https://leetcode.com/problems/house-robber-iii/)
Topics: Binary Tree

- 766 - [Toeplitz Matrix](https://leetcode.com/problems/toeplitz-matrix/)
Topics: Array, Matrix

# Done

## Easy

- 1 - [Two Sum](https://leetcode.com/problems/two-sum/)
- 1189 - [Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/)

## Medium

- 1493 - [Longest Subarray of 1s After Deleting One Element](https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element)

# 'The' 75

## Array

- [x] [Two Sum](https://leetcode.com/problems/two-sum/)
- [ ] [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)
- [x] [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/)
- [ ] [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/)
- [ ] [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/)
- [ ] [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/)
- [ ] [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/)
- [ ] [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)
- [ ] [3 Sum](https://leetcode.com/problems/3sum/)
- [ ] [Container With Most Water](https://leetcode.com/problems/container-with-most-water/)

## Binary

- [ ] [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/)
- [ ] [Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/)
- [ ] [Counting Bits](https://leetcode.com/problems/counting-bits/)
- [ ] [Missing Number](https://leetcode.com/problems/missing-number/)
- [ ] [Reverse Bits](https://leetcode.com/problems/reverse-bits/)

## Dynamic Programming

- [x] [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/)
- [ ] [Coin Change](https://leetcode.com/problems/coin-change/)
- [ ] [Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/)
- [ ] [Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/)
- [ ] [Word Break Problem](https://leetcode.com/problems/word-break/)
- [ ] [Combination Sum](https://leetcode.com/problems/combination-sum-iv/)
- [ ] [House Robber](https://leetcode.com/problems/house-robber/)
- [ ] [House Robber II](https://leetcode.com/problems/house-robber-ii/)
- [ ] [Decode Ways](https://leetcode.com/problems/decode-ways/)
- [ ] [Unique Paths](https://leetcode.com/problems/unique-paths/)
- [ ] [Jump Game](https://leetcode.com/problems/jump-game/)

## Graph

- [ ] [Clone Graph](https://leetcode.com/problems/clone-graph/)
- [ ] [Course Schedule](https://leetcode.com/problems/course-schedule/)
- [ ] [Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/)
- [x] [Number of Islands](https://leetcode.com/problems/number-of-islands/)
- [ ] [Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)
- [ ] [Alien Dictionary](https://leetcode.com/problems/alien-dictionary/)
- [ ] [Graph Valid Tree](https://leetcode.com/problems/graph-valid-tree/)
- [ ] [Number of Connected Components in an Undirected Graph](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/)

## Interval

- [ ] [Insert Interval](https://leetcode.com/problems/insert-interval/)
- [ ] [Merge Intervals](https://leetcode.com/problems/merge-intervals/)
- [ ] [Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/)
- [ ] [Meeting Rooms](https://leetcode.com/problems/meeting-rooms/)
- [ ] [Meeting Rooms II](https://leetcode.com/problems/meeting-rooms-ii/)

## Linked List

- [ ] [Reverse a Linked List](https://leetcode.com/problems/reverse-linked-list/)
- [ ] [Detect Cycle in a Linked List](https://leetcode.com/problems/linked-list-cycle/)
- [ ] [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/)
- [ ] [Merge K Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/)
- [ ] [Remove Nth Node From End Of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)
- [ ] [Reorder List](https://leetcode.com/problems/reorder-list/)

## Matrix

- [ ] [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/)
- [ ] [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/)
- [ ] [Rotate Image](https://leetcode.com/problems/rotate-image/)
- [ ] [Word Search](https://leetcode.com/problems/word-search/)

## String

- [ ] [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)
- [ ] [Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/)
- [ ] [Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/)
- [ ] [Valid Anagram](https://leetcode.com/problems/valid-anagram/)
- [ ] [Group Anagrams](https://leetcode.com/problems/group-anagrams/)
- [ ] [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)
- [ ] [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)
- [ ] [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/)
- [ ] [Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/)
- [ ] [Encode and Decode Strings](https://leetcode.com/problems/encode-and-decode-strings/)

## Tree

- [x] [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/)
- [ ] [Same Tree](https://leetcode.com/problems/same-tree/)
- [ ] [Invert/Flip Binary Tree](https://leetcode.com/problems/invert-binary-tree/)
- [ ] [Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/)
- [ ] [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/)
- [ ] [Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/)
- [ ] [Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/)
- [ ] [Construct Binary Tree from Preorder and Inorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)
- [ ] [Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/)
- [ ] [Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/)
- [ ] [Lowest Common Ancestor of BST](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/)
- [ ] [Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/)
- [ ] [Add and Search Word](https://leetcode.com/problems/add-and-search-word-data-structure-design/)
- [ ] [Word Search II](https://leetcode.com/problems/word-search-ii/)

## Heap

- [ ] [Merge K Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/)
- [ ] [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/)
- [ ] [Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/)
75 changes: 75 additions & 0 deletions projects/Leetcode/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>de.amrik</groupId>
<artifactId>Leetcode</artifactId>
<version>1.0-SNAPSHOT</version>

<name>Leetcode</name>
<!-- FIXME change it to the project's website -->
<url>https://amrik.de/CV</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
17 changes: 17 additions & 0 deletions projects/Leetcode/src/main/java/de/amrik/ClimbingStairs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package de.amrik;

import java.util.HashMap;

public class ClimbingStairs {
public static int waysToClimbStairs(int n) {
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>(n);
for (int i = 1; i <= 2; i++) {
hm.put(i, i);
}
for (int i = 3; i <= n; i++) {
hm.put(i, (hm.get(i - 1) + hm.get(i - 2)));
}
return hm.get(n);

}
}
19 changes: 19 additions & 0 deletions projects/Leetcode/src/main/java/de/amrik/ContainsDuplicate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.amrik;

import java.util.HashSet;
import java.util.Set;

public class ContainsDuplicate {

public static boolean containsDuplicate(int[] arr) {
Set<Integer> hs = new HashSet<>(arr.length);
for (int i : arr) {
if (hs.contains(i)) {
return true;
}
hs.add(i);
}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package de.amrik.DataStructures;

public class TreeNode {

public int val;
public TreeNode left;
public TreeNode right;

TreeNode() {
}

TreeNode(int val) {
this.val = val;
}

TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}

public static TreeNode fromArray(Integer[] arr) {
if (arr.length == 0) {
return null;
}
TreeNode root = new TreeNode(arr[0]);
return fromArray(arr, root, 0);
}

static TreeNode fromArray(Integer[] arr, TreeNode root, int depth) {
if (depth < arr.length) {
if (arr[depth] == null) {
return null;
}
TreeNode temp = new TreeNode(arr[depth]);
root = temp;
root.left = fromArray(arr, root.left, depth * 2 + 1);
root.right = fromArray(arr, root.right, depth * 2 + 2);
}
return root;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package de.amrik;

import java.util.Arrays;

public class LongestSubarrayOfOnesAfterDeletingOneElement {

/**
* Given a binary array nums, you should delete one element from it.
*
* @return the size of the longest non-empty subarray containing only 1's in the
* resulting array. Return 0 if there is no such subarray.
*/
public static int getLengthOfSubarray(int[] nums) {

// If we have all 0s in the array, return 0.
if (Arrays.stream(nums).sum() == 0) {
return 0;
}
if (Arrays.stream(nums).sum() >= nums.length - 1) {
return nums.length - 1;
}
int maxSeen = 0;
int left = 0;
int right = 0;

for (int i = 0; i < nums.length; i++) {
left += nums[i];

if (nums[i] == 0) {
for (int j = i + 1; j < nums.length; j++) {
right += nums[j];
if (nums[j] == 0 || j == nums.length - 1) {
i = j - 1;
maxSeen = Math.max(maxSeen, left + right);
left = right;
right = 0;
break;
}
}
}

}

return maxSeen;
}
}
Loading

0 comments on commit 6fd7ca2

Please sign in to comment.