Skip to content

Commit 331bb28

Browse files
Some Questions
1 parent c4f51fb commit 331bb28

File tree

539 files changed

+17448
-51
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

539 files changed

+17448
-51
lines changed

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Compiled class file
2+
*.class
3+
4+
# Log file
5+
*.log
6+
7+
# BlueJ files
8+
*.ctxt
9+
10+
# Mobile Tools for Java (J2ME)
11+
.mtj.tmp/
12+
13+
# Package Files #
14+
*.jar
15+
*.war
16+
*.nar
17+
*.ear
18+
*.zip
19+
*.tar.gz
20+
*.rar
21+
22+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23+
hs_err_pid*

1-two-sum/1-two-sum.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public int[] twoSum(int[] nums, int target) {
3+
HashMap<Integer, Integer> map = new HashMap<>();
4+
for(int i = 0; i < nums.length; i++){
5+
int val = target - nums[i];
6+
if(map.containsKey(val)){
7+
return new int[] {i, map.get(val)};
8+
}
9+
map.put(nums[i], i);
10+
}
11+
return new int[] {-1, -1};
12+
}
13+
}

1-two-sum/NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

1-two-sum/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<h2><a href="https://leetcode.com/problems/two-sum/">1. Two Sum</a></h2><h3>Easy</h3><hr><div><p>Given an array of integers <code>nums</code>&nbsp;and an integer <code>target</code>, return <em>indices of the two numbers such that they add up to <code>target</code></em>.</p>
2+
3+
<p>You may assume that each input would have <strong><em>exactly</em> one solution</strong>, and you may not use the <em>same</em> element twice.</p>
4+
5+
<p>You can return the answer in any order.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong>Example 1:</strong></p>
9+
10+
<pre><strong>Input:</strong> nums = [2,7,11,15], target = 9
11+
<strong>Output:</strong> [0,1]
12+
<strong>Explanation:</strong> Because nums[0] + nums[1] == 9, we return [0, 1].
13+
</pre>
14+
15+
<p><strong>Example 2:</strong></p>
16+
17+
<pre><strong>Input:</strong> nums = [3,2,4], target = 6
18+
<strong>Output:</strong> [1,2]
19+
</pre>
20+
21+
<p><strong>Example 3:</strong></p>
22+
23+
<pre><strong>Input:</strong> nums = [3,3], target = 6
24+
<strong>Output:</strong> [0,1]
25+
</pre>
26+
27+
<p>&nbsp;</p>
28+
<p><strong>Constraints:</strong></p>
29+
30+
<ul>
31+
<li><code>2 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
32+
<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
33+
<li><code>-10<sup>9</sup> &lt;= target &lt;= 10<sup>9</sup></code></li>
34+
<li><strong>Only one valid answer exists.</strong></li>
35+
</ul>
36+
37+
<p>&nbsp;</p>
38+
<strong>Follow-up:&nbsp;</strong>Can you come up with an algorithm that is less than&nbsp;<code>O(n<sup>2</sup>)&nbsp;</code>time complexity?</div>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<h2><a href="https://leetcode.com/problems/next-greater-node-in-linked-list/">1019. Next Greater Node In Linked List</a></h2><h3>Medium</h3><hr><div><p>You are given the <code>head</code> of a linked list with <code>n</code> nodes.</p>
2+
3+
<p>For each node in the list, find the value of the <strong>next greater node</strong>. That is, for each node, find the value of the first node that is next to it and has a <strong>strictly larger</strong> value than it.</p>
4+
5+
<p>Return an integer array <code>answer</code> where <code>answer[i]</code> is the value of the next greater node of the <code>i<sup>th</sup></code> node (<strong>1-indexed</strong>). If the <code>i<sup>th</sup></code> node does not have a next greater node, set <code>answer[i] = 0</code>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong>Example 1:</strong></p>
9+
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/05/linkedlistnext1.jpg" style="width: 304px; height: 133px;">
10+
<pre><strong>Input:</strong> head = [2,1,5]
11+
<strong>Output:</strong> [5,5,0]
12+
</pre>
13+
14+
<p><strong>Example 2:</strong></p>
15+
<img alt="" src="https://assets.leetcode.com/uploads/2021/08/05/linkedlistnext2.jpg" style="width: 500px; height: 113px;">
16+
<pre><strong>Input:</strong> head = [2,7,4,3,5]
17+
<strong>Output:</strong> [7,0,5,5,0]
18+
</pre>
19+
20+
<p>&nbsp;</p>
21+
<p><strong>Constraints:</strong></p>
22+
23+
<ul>
24+
<li>The number of nodes in the list is <code>n</code>.</li>
25+
<li><code>1 &lt;= n &lt;= 10<sup>4</sup></code></li>
26+
<li><code>1 &lt;= Node.val &lt;= 10<sup>9</sup></code></li>
27+
</ul>
28+
</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<h2><a href="https://leetcode.com/problems/remove-outermost-parentheses/">1021. Remove Outermost Parentheses</a></h2><h3>Easy</h3><hr><div><p>A valid parentheses string is either empty <code>""</code>, <code>"(" + A + ")"</code>, or <code>A + B</code>, where <code>A</code> and <code>B</code> are valid parentheses strings, and <code>+</code> represents string concatenation.</p>
2+
3+
<ul>
4+
<li>For example, <code>""</code>, <code>"()"</code>, <code>"(())()"</code>, and <code>"(()(()))"</code> are all valid parentheses strings.</li>
5+
</ul>
6+
7+
<p>A valid parentheses string <code>s</code> is primitive if it is nonempty, and there does not exist a way to split it into <code>s = A + B</code>, with <code>A</code> and <code>B</code> nonempty valid parentheses strings.</p>
8+
9+
<p>Given a valid parentheses string <code>s</code>, consider its primitive decomposition: <code>s = P<sub>1</sub> + P<sub>2</sub> + ... + P<sub>k</sub></code>, where <code>P<sub>i</sub></code> are primitive valid parentheses strings.</p>
10+
11+
<p>Return <code>s</code> <em>after removing the outermost parentheses of every primitive string in the primitive decomposition of </em><code>s</code>.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong>Example 1:</strong></p>
15+
16+
<pre><strong>Input:</strong> s = "(()())(())"
17+
<strong>Output:</strong> "()()()"
18+
<strong>Explanation:</strong>
19+
The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
20+
After removing outer parentheses of each part, this is "()()" + "()" = "()()()".
21+
</pre>
22+
23+
<p><strong>Example 2:</strong></p>
24+
25+
<pre><strong>Input:</strong> s = "(()())(())(()(()))"
26+
<strong>Output:</strong> "()()()()(())"
27+
<strong>Explanation:</strong>
28+
The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))".
29+
After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".
30+
</pre>
31+
32+
<p><strong>Example 3:</strong></p>
33+
34+
<pre><strong>Input:</strong> s = "()()"
35+
<strong>Output:</strong> ""
36+
<strong>Explanation:</strong>
37+
The input string is "()()", with primitive decomposition "()" + "()".
38+
After removing outer parentheses of each part, this is "" + "" = "".
39+
</pre>
40+
41+
<p>&nbsp;</p>
42+
<p><strong>Constraints:</strong></p>
43+
44+
<ul>
45+
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
46+
<li><code>s[i]</code> is either <code>'('</code> or <code>')'</code>.</li>
47+
<li><code>s</code> is a valid parentheses string.</li>
48+
</ul>
49+
</div>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public String removeDuplicates(String s) {
3+
char[] stack = new char[s.length()];
4+
int i = 0;
5+
for(int j = 0; j < s.length(); j++){
6+
char currChar = s.charAt(j);
7+
if(i > 0 && currChar == stack[i - 1]){
8+
i--;
9+
}
10+
else{
11+
stack[i] = currChar;
12+
i++;
13+
}
14+
}
15+
return new String(stack, 0, i);
16+
}
17+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<h2><a href="https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/">1047. Remove All Adjacent Duplicates In String</a></h2><h3>Easy</h3><hr><div><p>You are given a string <code>s</code> consisting of lowercase English letters. A <strong>duplicate removal</strong> consists of choosing two <strong>adjacent</strong> and <strong>equal</strong> letters and removing them.</p>
2+
3+
<p>We repeatedly make <strong>duplicate removals</strong> on <code>s</code> until we no longer can.</p>
4+
5+
<p>Return <em>the final string after all such duplicate removals have been made</em>. It can be proven that the answer is <strong>unique</strong>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong>Example 1:</strong></p>
9+
10+
<pre><strong>Input:</strong> s = "abbaca"
11+
<strong>Output:</strong> "ca"
12+
<strong>Explanation:</strong>
13+
For example, in "abbaca" we could remove "bb" since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is "aaca", of which only "aa" is possible, so the final string is "ca".
14+
</pre>
15+
16+
<p><strong>Example 2:</strong></p>
17+
18+
<pre><strong>Input:</strong> s = "azxxzy"
19+
<strong>Output:</strong> "ay"
20+
</pre>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Constraints:</strong></p>
24+
25+
<ul>
26+
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
27+
<li><code>s</code> consists of lowercase English letters.</li>
28+
</ul>
29+
</div>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int maxArea(int[] height) {
3+
int water = 0;
4+
int i = 0;
5+
int j = height.length - 1;
6+
while(i < j){
7+
int diff = (j - i) * Math.min(height[i], height[j]);
8+
if(diff > water)
9+
water = diff;
10+
if(height[i] < height[j])
11+
i++;
12+
else
13+
j--;
14+
}
15+
return water;
16+
}
17+
}

11-container-with-most-water/NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<h2><a href="https://leetcode.com/problems/container-with-most-water/">11. Container With Most Water</a></h2><h3>Medium</h3><hr><div><p>You are given an integer array <code>height</code> of length <code>n</code>. There are <code>n</code> vertical lines drawn such that the two endpoints of the <code>i<sup>th</sup></code> line are <code>(i, 0)</code> and <code>(i, height[i])</code>.</p>
2+
3+
<p>Find two lines that together with the x-axis form a container, such that the container contains the most water.</p>
4+
5+
<p>Return <em>the maximum amount of water a container can store</em>.</p>
6+
7+
<p><strong>Notice</strong> that you may not slant the container.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong>Example 1:</strong></p>
11+
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg" style="width: 600px; height: 287px;">
12+
<pre><strong>Input:</strong> height = [1,8,6,2,5,4,8,3,7]
13+
<strong>Output:</strong> 49
14+
<strong>Explanation:</strong> The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
15+
</pre>
16+
17+
<p><strong>Example 2:</strong></p>
18+
19+
<pre><strong>Input:</strong> height = [1,1]
20+
<strong>Output:</strong> 1
21+
</pre>
22+
23+
<p>&nbsp;</p>
24+
<p><strong>Constraints:</strong></p>
25+
26+
<ul>
27+
<li><code>n == height.length</code></li>
28+
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
29+
<li><code>0 &lt;= height[i] &lt;= 10<sup>4</sup></code></li>
30+
</ul>
31+
</div>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public int val;
5+
public Node left;
6+
public Node right;
7+
public Node next;
8+
9+
public Node() {}
10+
11+
public Node(int _val) {
12+
val = _val;
13+
}
14+
15+
public Node(int _val, Node _left, Node _right, Node _next) {
16+
val = _val;
17+
left = _left;
18+
right = _right;
19+
next = _next;
20+
}
21+
};
22+
*/
23+
24+
class Solution {
25+
public Node connect(Node root) {
26+
if(root == null)
27+
return null;
28+
if(root.left != null)
29+
root.left.next = root.right;
30+
if(root.right != null && root.next != null)
31+
root.right.next = root.next.left;
32+
connect(root.left);
33+
connect(root.right);
34+
return root;
35+
}
36+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<h2><a href="https://leetcode.com/problems/populating-next-right-pointers-in-each-node/">116. Populating Next Right Pointers in Each Node</a></h2><h3>Medium</h3><hr><div><p>You are given a <strong>perfect binary tree</strong> where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:</p>
2+
3+
<pre>struct Node {
4+
int val;
5+
Node *left;
6+
Node *right;
7+
Node *next;
8+
}
9+
</pre>
10+
11+
<p>Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to <code>NULL</code>.</p>
12+
13+
<p>Initially, all next pointers are set to <code>NULL</code>.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong>Example 1:</strong></p>
17+
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/14/116_sample.png" style="width: 500px; height: 171px;">
18+
<pre><strong>Input:</strong> root = [1,2,3,4,5,6,7]
19+
<strong>Output:</strong> [1,#,2,3,#,4,5,6,7,#]
20+
<strong>Explanation: </strong>Given the above perfect binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
21+
</pre>
22+
23+
<p><strong>Example 2:</strong></p>
24+
25+
<pre><strong>Input:</strong> root = []
26+
<strong>Output:</strong> []
27+
</pre>
28+
29+
<p>&nbsp;</p>
30+
<p><strong>Constraints:</strong></p>
31+
32+
<ul>
33+
<li>The number of nodes in the tree is in the range <code>[0, 2<sup>12</sup> - 1]</code>.</li>
34+
<li><code>-1000 &lt;= Node.val &lt;= 1000</code></li>
35+
</ul>
36+
37+
<p>&nbsp;</p>
38+
<p><strong>Follow-up:</strong></p>
39+
40+
<ul>
41+
<li>You may only use constant extra space.</li>
42+
<li>The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.</li>
43+
</ul>
44+
</div>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int maxProfit(int[] prices) {
3+
int n = prices.length;
4+
int min = prices[0];
5+
int pr = 0;
6+
for(int i = 0; i < n; i++){
7+
if(prices[i] < min){
8+
min = prices[i];
9+
}
10+
if(Math.abs(min - prices[i]) > pr){
11+
pr = Math.abs(min - prices[i]);
12+
}
13+
}
14+
return pr;
15+
}
16+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)