Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some new Leetcode questions #160

Open
wants to merge 147 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
147 commits
Select commit Hold shift + click to select a range
b15e834
Added README.md file for Buy Two Chocolates
2215000411AtulKumar Oct 21, 2024
b40b9cd
Time: 2 ms (72.85%) | Memory: 44.2 MB (72.23%) - LeetSync
2215000411AtulKumar Oct 21, 2024
48062ff
Added README.md file for Count Elements With Maximum Frequency
2215000411AtulKumar Oct 21, 2024
3f33042
Time: 1 ms (99.41%) | Memory: 42.4 MB (25.55%) - LeetSync
2215000411AtulKumar Oct 21, 2024
b6c781b
Added README.md file for Increasing Triplet Subsequence
2215000411AtulKumar Nov 13, 2024
1530d0d
Added README.md file for Binary Tree Inorder Traversal
2215000411AtulKumar Nov 13, 2024
27c45d4
Time: 0 ms (100.00%) | Memory: 41.5 MB (47.93%) - LeetSync
2215000411AtulKumar Nov 13, 2024
b199efc
Added README.md file for Sort Colors
2215000411AtulKumar Nov 14, 2024
6ed0fc5
Time: 2 ms (13.21%) | Memory: 41.8 MB (67.14%) - LeetSync
2215000411AtulKumar Nov 14, 2024
7c5c267
Added README.md file for Sort Colors
2215000411AtulKumar Nov 14, 2024
55143c4
Time: 0 ms (100.00%) | Memory: 42 MB (32.97%) - LeetSync
2215000411AtulKumar Nov 14, 2024
0e207b5
Added README.md file for Merge Sorted Array
2215000411AtulKumar Nov 14, 2024
88b3cb6
Time: 0 ms (100.00%) | Memory: 42.3 MB (30.05%) - LeetSync
2215000411AtulKumar Nov 14, 2024
293eff1
Added README.md file for Sort List
2215000411AtulKumar Nov 16, 2024
f396e63
Time: 9 ms (93.98%) | Memory: 57.2 MB (23.52%) - LeetSync
2215000411AtulKumar Nov 16, 2024
bbdbf9d
Added README.md file for Delete Node in a Linked List
2215000411AtulKumar Nov 17, 2024
ee0b5ff
Time: 0 ms (100.00%) | Memory: 43.9 MB (55.16%) - LeetSync
2215000411AtulKumar Nov 17, 2024
22e04e9
Added README.md file for Linked List Cycle
2215000411AtulKumar Nov 17, 2024
631d483
Time: 0 ms (100.00%) | Memory: 44.4 MB (52.78%) - LeetSync
2215000411AtulKumar Nov 17, 2024
5c00035
Added README.md file for Merge Two Sorted Lists
2215000411AtulKumar Nov 17, 2024
ae3ae3b
Time: 0 ms (100.00%) | Memory: 42.5 MB (36.25%) - LeetSync
2215000411AtulKumar Nov 17, 2024
0d28d31
Added README.md file for Merge Two Sorted Lists
2215000411AtulKumar Nov 17, 2024
54c664d
Time: 0 ms (100.00%) | Memory: 42.1 MB (87.71%) - LeetSync
2215000411AtulKumar Nov 17, 2024
8858208
Added README.md file for Add Two Numbers
2215000411AtulKumar Nov 17, 2024
53eeb67
Time: 1 ms (100.00%) | Memory: 44.4 MB (57.22%) - LeetSync
2215000411AtulKumar Nov 17, 2024
edeea62
Added README.md file for Length of Last Word
2215000411AtulKumar Nov 18, 2024
c64f7f8
Time: 0 ms (100.00%) | Memory: 41.9 MB (21.98%) - LeetSync
2215000411AtulKumar Nov 18, 2024
54b12dd
Added README.md file for Length of Last Word
2215000411AtulKumar Nov 18, 2024
a2b52ee
Time: 0 ms (100.00%) | Memory: 41.8 MB (35.54%) - LeetSync
2215000411AtulKumar Nov 18, 2024
a0fd447
Added README.md file for Reverse Integer
2215000411AtulKumar Nov 19, 2024
c82a378
Time: 0 ms (100.00%) | Memory: 40.8 MB (48.48%) - LeetSync
2215000411AtulKumar Nov 19, 2024
e4551c6
Added README.md file for Unique Paths
2215000411AtulKumar Nov 21, 2024
65170f2
Time: 0 ms (100.00%) | Memory: 40.6 MB (29.05%) - LeetSync
2215000411AtulKumar Nov 21, 2024
a6827b2
Added README.md file for Swap Nodes in Pairs
2215000411AtulKumar Nov 23, 2024
f3daf6e
Time: 0 ms (100.00%) | Memory: 41.1 MB (70.87%) - LeetSync
2215000411AtulKumar Nov 23, 2024
3a893a9
Added README.md file for Combination Sum II
2215000411AtulKumar Nov 24, 2024
8e00553
Time: 3 ms (79.01%) | Memory: 43.2 MB (40.97%) - LeetSync
2215000411AtulKumar Nov 24, 2024
b153198
Added README.md file for Binary Search
2215000411AtulKumar Dec 22, 2024
50061a2
Time: 0 ms (100.00%) | Memory: 45.7 MB (45.03%) - LeetSync
2215000411AtulKumar Dec 22, 2024
4595ff8
Added README.md file for Contains Duplicate
2215000411AtulKumar Dec 23, 2024
db3cbb8
Time: 17 ms (38.77%) | Memory: 59.6 MB (13.79%) - LeetSync
2215000411AtulKumar Dec 23, 2024
4505637
Added README.md file for Climbing Stairs
2215000411AtulKumar Dec 23, 2024
1419e40
Time: 0 ms (100.00%) | Memory: 40.2 MB (78.27%) - LeetSync
2215000411AtulKumar Dec 23, 2024
318c335
Added README.md file for Best Time to Buy and Sell Stock
2215000411AtulKumar Dec 23, 2024
85e861c
Time: 2 ms (76.99%) | Memory: 61.6 MB (67.18%) - LeetSync
2215000411AtulKumar Dec 23, 2024
72a17db
Added README.md file for Best Time to Buy and Sell Stock II
2215000411AtulKumar Dec 23, 2024
cb28a5e
Time: 1 ms (92.59%) | Memory: 46 MB (24.80%) - LeetSync
2215000411AtulKumar Dec 23, 2024
aecab63
Added README.md file for Missing Number
2215000411AtulKumar Dec 25, 2024
602f528
Time: 0 ms (100.00%) | Memory: 45.4 MB (32.91%) - LeetSync
2215000411AtulKumar Dec 25, 2024
f74b42c
Added README.md file for Find All Numbers Disappeared in an Array
2215000411AtulKumar Dec 25, 2024
317c948
Time: 7 ms (44.61%) | Memory: 56.4 MB (44.92%) - LeetSync
2215000411AtulKumar Dec 25, 2024
4a10b6c
Added README.md file for Single Number
2215000411AtulKumar Dec 26, 2024
8da9fbb
Time: 1 ms (99.88%) | Memory: 46.9 MB (6.55%) - LeetSync
2215000411AtulKumar Dec 26, 2024
7e116cf
Added README.md file for Counting Bits
2215000411AtulKumar Dec 26, 2024
e3616e4
Time: 2 ms (95.52%) | Memory: 46.8 MB (36.41%) - LeetSync
2215000411AtulKumar Dec 26, 2024
a9d0c5e
Added README.md file for Valid Anagram
2215000411AtulKumar Dec 26, 2024
9666af0
Time: 18 ms (17.91%) | Memory: 44.5 MB (45.34%) - LeetSync
2215000411AtulKumar Dec 26, 2024
9ae0317
Added README.md file for Trapping Rain Water
2215000411AtulKumar Dec 26, 2024
31b2e39
Time: 0 ms (100.00%) | Memory: 46.6 MB (29.22%) - LeetSync
2215000411AtulKumar Dec 26, 2024
f7ea40a
Added README.md file for Single Number II
2215000411AtulKumar Dec 26, 2024
8b03d01
Time: 5 ms (39.72%) | Memory: 45.2 MB (77.51%) - LeetSync
2215000411AtulKumar Dec 26, 2024
19a98e1
Added README.md file for Permutations
2215000411AtulKumar Dec 27, 2024
7b32320
Time: 2 ms (43.47%) | Memory: 44.6 MB (48.62%) - LeetSync
2215000411AtulKumar Dec 27, 2024
278c67c
Added README.md file for Permutations II
2215000411AtulKumar Dec 27, 2024
c61d9bf
Time: 2 ms (68.87%) | Memory: 44.6 MB (89.59%) - LeetSync
2215000411AtulKumar Dec 27, 2024
b7ce8d9
Added README.md file for Array Partition
2215000411AtulKumar Dec 27, 2024
59435c3
Time: 12 ms (97.81%) | Memory: 46.4 MB (74.23%) - LeetSync
2215000411AtulKumar Dec 27, 2024
76573bb
Added README.md file for Keyboard Row
2215000411AtulKumar Dec 27, 2024
af2d233
Time: 2 ms (23.02%) | Memory: 41.4 MB (58.41%) - LeetSync
2215000411AtulKumar Dec 27, 2024
5b371c0
Added README.md file for Island Perimeter
2215000411AtulKumar Dec 27, 2024
836626f
Time: 4 ms (99.84%) | Memory: 45.3 MB (89.27%) - LeetSync
2215000411AtulKumar Dec 27, 2024
741c59c
Added README.md file for Intersection of Two Arrays
2215000411AtulKumar Dec 28, 2024
36b0a68
Time: 4 ms (34.94%) | Memory: 43.1 MB (71.58%) - LeetSync
2215000411AtulKumar Dec 28, 2024
356803c
Added README.md file for Distribute Candies
2215000411AtulKumar Dec 28, 2024
b15f525
Time: 30 ms (84.92%) | Memory: 45.7 MB (64.56%) - LeetSync
2215000411AtulKumar Dec 28, 2024
463ab8f
Added README.md file for Next Greater Element I
2215000411AtulKumar Dec 28, 2024
38b623c
Time: 3 ms (92.01%) | Memory: 43.8 MB (52.06%) - LeetSync
2215000411AtulKumar Dec 28, 2024
f4d554e
Added README.md file for Relative Ranks
2215000411AtulKumar Dec 28, 2024
b5210eb
Time: 9 ms (55.98%) | Memory: 46 MB (23.60%) - LeetSync
2215000411AtulKumar Dec 28, 2024
e522a6a
Added README.md file for N-Queens
2215000411AtulKumar Dec 28, 2024
38961e1
Time: 2 ms (88.38%) | Memory: 44.9 MB (42.39%) - LeetSync
2215000411AtulKumar Dec 28, 2024
8577dfd
Added README.md file for Transpose Matrix
2215000411AtulKumar Dec 29, 2024
0cd042e
Time: 1 ms (28.77%) | Memory: 45 MB (25.98%) - LeetSync
2215000411AtulKumar Dec 29, 2024
00f8fa0
Added README.md file for Find Common Characters
2215000411AtulKumar Dec 29, 2024
3c53a7c
Time: 1 ms (99.68%) | Memory: 43.1 MB (69.40%) - LeetSync
2215000411AtulKumar Dec 29, 2024
896d3b0
Added README.md file for Minimum Time Visiting All Points
2215000411AtulKumar Dec 29, 2024
b41744a
Time: 1 ms (92.99%) | Memory: 43 MB (68.53%) - LeetSync
2215000411AtulKumar Dec 29, 2024
d22de42
Added README.md file for Sum of Unique Elements
2215000411AtulKumar Dec 29, 2024
ae7dd77
Time: 1 ms (78.44%) | Memory: 41.7 MB (18.88%) - LeetSync
2215000411AtulKumar Dec 29, 2024
eebe01f
Added README.md file for Find All Duplicates in an Array
2215000411AtulKumar Dec 29, 2024
d1a511d
Time: 6 ms (74.77%) | Memory: 55.7 MB (7.15%) - LeetSync
2215000411AtulKumar Dec 29, 2024
221f43f
Added README.md file for Sliding Puzzle
2215000411AtulKumar Dec 30, 2024
2bfaff8
Time: 15 ms (35.86%) | Memory: 44.9 MB (9.09%) - LeetSync
2215000411AtulKumar Dec 30, 2024
38918a2
Added README.md file for Rotate Image
2215000411AtulKumar Dec 30, 2024
07d7464
Time: 0 ms (100.00%) | Memory: 42 MB (87.43%) - LeetSync
2215000411AtulKumar Dec 30, 2024
dbaf520
Added README.md file for Maximum Number of Pairs in Array
2215000411AtulKumar Dec 30, 2024
b45fade
Time: 0 ms (100.00%) | Memory: 41.8 MB (81.95%) - LeetSync
2215000411AtulKumar Dec 30, 2024
2076bc7
Added README.md file for Flipping an Image
2215000411AtulKumar Dec 30, 2024
3a72a82
Time: 1 ms (41.95%) | Memory: 44.9 MB (19.17%) - LeetSync
2215000411AtulKumar Dec 30, 2024
dc6ea7c
Added README.md file for Baseball Game
2215000411AtulKumar Dec 31, 2024
f93fd45
Time: 2 ms (93.80%) | Memory: 41.9 MB (36.19%) - LeetSync
2215000411AtulKumar Dec 31, 2024
5e7bf4c
Added README.md file for Reverse String
2215000411AtulKumar Dec 31, 2024
b0830f7
Time: 0 ms (100.00%) | Memory: 49.1 MB (57.15%) - LeetSync
2215000411AtulKumar Dec 31, 2024
3c93913
Added README.md file for Add Strings
2215000411AtulKumar Dec 31, 2024
a556da4
Time: 1 ms (100.00%) | Memory: 42.6 MB (62.86%) - LeetSync
2215000411AtulKumar Dec 31, 2024
c972c5a
Added README.md file for Reverse Words in a String III
2215000411AtulKumar Dec 31, 2024
af82021
Time: 4 ms (88.88%) | Memory: 45.4 MB (50.97%) - LeetSync
2215000411AtulKumar Dec 31, 2024
46fbbe6
Added README.md file for Squares of a Sorted Array
2215000411AtulKumar Dec 31, 2024
5496467
Time: 1 ms (100.00%) | Memory: 47.5 MB (17.56%) - LeetSync
2215000411AtulKumar Dec 31, 2024
24198d3
Added README.md file for Merge k Sorted Lists
2215000411AtulKumar Jan 1, 2025
639eaa3
Time: 2 ms (82.54%) | Memory: 44.2 MB (88.80%) - LeetSync
2215000411AtulKumar Jan 1, 2025
b55da01
Added README.md file for Number of 1 Bits
2215000411AtulKumar Jan 1, 2025
461cd85
Time: 0 ms (100.00%) | Memory: 40.6 MB (59.69%) - LeetSync
2215000411AtulKumar Jan 1, 2025
5105cb1
Added README.md file for Sqrt(x)
2215000411AtulKumar Jan 1, 2025
b4dee69
Time: 1 ms (87.12%) | Memory: 40.8 MB (58.68%) - LeetSync
2215000411AtulKumar Jan 1, 2025
5284092
Added README.md file for N-Queens II
2215000411AtulKumar Jan 2, 2025
75e04e0
Time: 1 ms (82.65%) | Memory: 40.6 MB (53.94%) - LeetSync
2215000411AtulKumar Jan 2, 2025
1287be3
Added README.md file for Subsets
2215000411AtulKumar Jan 2, 2025
a1f2266
Time: 1 ms (54.69%) | Memory: 42.7 MB (69.41%) - LeetSync
2215000411AtulKumar Jan 2, 2025
8e85945
Added README.md file for Remove Linked List Elements
2215000411AtulKumar Jan 2, 2025
f6e0676
Time: 1 ms (94.20%) | Memory: 45.6 MB (58.52%) - LeetSync
2215000411AtulKumar Jan 2, 2025
967ff65
Added README.md file for Palindrome Linked List
2215000411AtulKumar Jan 2, 2025
68d859e
Time: 3 ms (99.74%) | Memory: 68.5 MB (40.16%) - LeetSync
2215000411AtulKumar Jan 2, 2025
8f17226
Added README.md file for Find Lucky Integer in an Array
2215000411AtulKumar Jan 2, 2025
62bea07
Time: 1 ms (100.00%) | Memory: 43.4 MB (46.72%) - LeetSync
2215000411AtulKumar Jan 2, 2025
2a15a71
Added README.md file for Basic Calculator
2215000411AtulKumar Jan 3, 2025
a67cc6e
Time: 9 ms (61.75%) | Memory: 43.7 MB (68.06%) - LeetSync
2215000411AtulKumar Jan 3, 2025
e2e7178
Added README.md file for Zigzag Conversion
2215000411AtulKumar Jan 3, 2025
9b31571
Time: 5 ms (66.93%) | Memory: 45.3 MB (51.06%) - LeetSync
2215000411AtulKumar Jan 3, 2025
0bcf5e5
Added README.md file for Add Binary
2215000411AtulKumar Jan 4, 2025
90e21b8
Time: 1 ms (99.89%) | Memory: 42.4 MB (34.61%) - LeetSync
2215000411AtulKumar Jan 4, 2025
4e7e4a7
Added README.md file for Valid Sudoku
2215000411AtulKumar Jan 4, 2025
d05a2fd
Time: 2 ms (77.34%) | Memory: 43.9 MB (98.62%) - LeetSync
2215000411AtulKumar Jan 4, 2025
383a3c4
Added README.md file for Median of Two Sorted Arrays
2215000411AtulKumar Jan 23, 2025
6b17245
Time: 4 ms (33.85%) | Memory: 46 MB (82.96%) - LeetSync
2215000411AtulKumar Jan 23, 2025
996ea27
Added README.md file for Missing Number
2215000411AtulKumar Jan 28, 2025
eddef61
Time: 0 ms (100.00%) | Memory: 45.2 MB (83.49%) - LeetSync
2215000411AtulKumar Jan 28, 2025
e9d1391
Added README.md file for Contains Duplicate
2215000411AtulKumar Jan 28, 2025
1cc1071
Time: 23 ms (6.41%) | Memory: 59.1 MB (18.30%) - LeetSync
2215000411AtulKumar Jan 28, 2025
4fb4edd
Added README.md file for Counting Bits
2215000411AtulKumar Jan 30, 2025
f17c350
Time: 2 ms (95.17%) | Memory: 46.6 MB (82.91%) - LeetSync
2215000411AtulKumar Jan 30, 2025
43f0272
Added README.md file for Counting Bits
2215000411AtulKumar Jan 30, 2025
a08bbbf
Time: 2 ms (95.17%) | Memory: 46.7 MB (67.70%) - LeetSync
2215000411AtulKumar Jan 30, 2025
15446c5
Added README.md file for Integer Replacement
2215000411AtulKumar Jan 30, 2025
4e6b786
Time: 0 ms (100.00%) | Memory: 41 MB (10.94%) - LeetSync
2215000411AtulKumar Jan 30, 2025
7e6b704
Added README.md file for Gas Station
2215000411AtulKumar Feb 17, 2025
f8ea020
Time: 1 ms (48.78%) | Memory: 112.5 MB (15.42%) - LeetSync
2215000411AtulKumar Feb 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions 1019-squares-of-a-sorted-array/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<h2><a href="https://leetcode.com/problems/squares-of-a-sorted-array">Squares of a Sorted Array</a></h2> <img src='https://img.shields.io/badge/Difficulty-Easy-brightgreen' alt='Difficulty: Easy' /><hr><p>Given an integer array <code>nums</code> sorted in <strong>non-decreasing</strong> order, return <em>an array of <strong>the squares of each number</strong> sorted in non-decreasing order</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [-4,-1,0,3,10]
<strong>Output:</strong> [0,1,9,16,100]
<strong>Explanation:</strong> After squaring, the array becomes [16,1,0,9,100].
After sorting, it becomes [0,1,9,16,100].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [-7,-3,2,3,11]
<strong>Output:</strong> [4,9,9,49,121]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code><span>1 &lt;= nums.length &lt;= </span>10<sup>4</sup></code></li>
<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
<li><code>nums</code> is sorted in <strong>non-decreasing</strong> order.</li>
</ul>

<p>&nbsp;</p>
<strong>Follow up:</strong> Squaring each element and sorting the new array is very trivial, could you find an <code>O(n)</code> solution using a different approach?
17 changes: 17 additions & 0 deletions 1019-squares-of-a-sorted-array/squares-of-a-sorted-array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public int[] sortedSquares(int[] nums) {
int[] res = new int[nums.length];
int left = 0;
int right = nums.length - 1;
for (int i = nums.length - 1; i >= 0; i--) {
if (Math.abs(nums[left]) > Math.abs(nums[right])) {
res[i] = nums[left] * nums[left];
left++;
} else {
res[i] = nums[right] * nums[right];
right--;
}
}
return res;
}
}
18 changes: 18 additions & 0 deletions 1044-find-common-characters/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<h2><a href="https://leetcode.com/problems/find-common-characters">Find Common Characters</a></h2> <img src='https://img.shields.io/badge/Difficulty-Easy-brightgreen' alt='Difficulty: Easy' /><hr><p>Given a string array <code>words</code>, return <em>an array of all characters that show up in all strings within the </em><code>words</code><em> (including duplicates)</em>. You may return the answer in <strong>any order</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> words = ["bella","label","roller"]
<strong>Output:</strong> ["e","l","l"]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> words = ["cool","lock","cook"]
<strong>Output:</strong> ["c","o"]
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= words.length &lt;= 100</code></li>
<li><code>1 &lt;= words[i].length &lt;= 100</code></li>
<li><code>words[i]</code> consists of lowercase English letters.</li>
</ul>
33 changes: 33 additions & 0 deletions 1044-find-common-characters/find-common-characters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
public List<String> commonChars(String[] A) {
int[] last = count(A[0]);
for (int i = 1; i < A.length; i++) {
last = intersection(last, count(A[i]));
}
List<String> arr = new ArrayList<>();
for (int i = 0; i < 26; i++) {
if (last[i] != 0) {
char a = 'a';
a += i;
String s = String.valueOf(a);
while (last[i] > 0) {
arr.add(s);
last[i]--;
}
}
}
return arr;
}
int[] intersection(int[] a, int[] b) {
int[] t = new int[26];
for (int i = 0; i < 26; i++) {
t[i] = Math.min(a[i], b[i]);
}
return t;
}
int[] count(String str) {
int[] t = new int[26];
for (char c : str.toCharArray()) t[c - 'a']++;
return t;
}
}
31 changes: 31 additions & 0 deletions 121-best-time-to-buy-and-sell-stock/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<h2><a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock">Best Time to Buy and Sell Stock</a></h2> <img src='https://img.shields.io/badge/Difficulty-Easy-brightgreen' alt='Difficulty: Easy' /><hr><p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p>

<p>You want to maximize your profit by choosing a <strong>single day</strong> to buy one stock and choosing a <strong>different day in the future</strong> to sell that stock.</p>

<p>Return <em>the maximum profit you can achieve from this transaction</em>. If you cannot achieve any profit, return <code>0</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> prices = [7,1,5,3,6,4]
<strong>Output:</strong> 5
<strong>Explanation:</strong> Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> prices = [7,6,4,3,1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> In this case, no transactions are done and the max profit = 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= prices.length &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= prices[i] &lt;= 10<sup>4</sup></code></li>
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public int maxProfit(int[] prices) {
int left =0; //left = buy
int right =1; // right = sell
int max_profit = 0;
while(right < prices.length){
if(prices[left] < prices[right]){
int profit = prices[right] - prices[left];
max_profit = Math.max(max_profit, profit);
}
else{
left = right;
}
right += 1;
}
return max_profit;
}
}
41 changes: 41 additions & 0 deletions 122-best-time-to-buy-and-sell-stock-ii/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<h2><a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii">Best Time to Buy and Sell Stock II</a></h2> <img src='https://img.shields.io/badge/Difficulty-Medium-orange' alt='Difficulty: Medium' /><hr><p>You are given an integer array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p>

<p>On each day, you may decide to buy and/or sell the stock. You can only hold <strong>at most one</strong> share of the stock at any time. However, you can buy it then immediately sell it on the <strong>same day</strong>.</p>

<p>Find and return <em>the <strong>maximum</strong> profit you can achieve</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> prices = [7,1,5,3,6,4]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Total profit is 4 + 3 = 7.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> prices = [1,2,3,4,5]
<strong>Output:</strong> 4
<strong>Explanation:</strong> Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Total profit is 4.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> prices = [7,6,4,3,1]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= prices.length &lt;= 3 * 10<sup>4</sup></code></li>
<li><code>0 &lt;= prices[i] &lt;= 10<sup>4</sup></code></li>
</ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public int maxProfit(int[] prices) {
int profit = 0;
for(int i=1; i<prices.length; i++){
if(prices[i] > prices[i-1]){
profit += prices[i] - prices[i-1];
}
}
return profit;
}
}
44 changes: 44 additions & 0 deletions 134-gas-station/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<h2><a href="https://leetcode.com/problems/gas-station">Gas Station</a></h2> <img src='https://img.shields.io/badge/Difficulty-Medium-orange' alt='Difficulty: Medium' /><hr><p>There are <code>n</code> gas stations along a circular route, where the amount of gas at the <code>i<sup>th</sup></code> station is <code>gas[i]</code>.</p>

<p>You have a car with an unlimited gas tank and it costs <code>cost[i]</code> of gas to travel from the <code>i<sup>th</sup></code> station to its next <code>(i + 1)<sup>th</sup></code> station. You begin the journey with an empty tank at one of the gas stations.</p>

<p>Given two integer arrays <code>gas</code> and <code>cost</code>, return <em>the starting gas station&#39;s index if you can travel around the circuit once in the clockwise direction, otherwise return</em> <code>-1</code>. If there exists a solution, it is <strong>guaranteed</strong> to be <strong>unique</strong>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> gas = [1,2,3,4,5], cost = [3,4,5,1,2]
<strong>Output:</strong> 3
<strong>Explanation:</strong>
Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 4. Your tank = 4 - 1 + 5 = 8
Travel to station 0. Your tank = 8 - 2 + 1 = 7
Travel to station 1. Your tank = 7 - 3 + 2 = 6
Travel to station 2. Your tank = 6 - 4 + 3 = 5
Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
Therefore, return 3 as the starting index.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> gas = [2,3,4], cost = [3,4,3]
<strong>Output:</strong> -1
<strong>Explanation:</strong>
You can&#39;t start at station 0 or 1, as there is not enough gas to travel to the next station.
Let&#39;s start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 0. Your tank = 4 - 3 + 2 = 3
Travel to station 1. Your tank = 3 - 3 + 3 = 3
You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.
Therefore, you can&#39;t travel around the circuit once no matter where you start.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>n == gas.length == cost.length</code></li>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= gas[i], cost[i] &lt;= 10<sup>4</sup></code></li>
</ul>
18 changes: 18 additions & 0 deletions 134-gas-station/gas-station.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
int deficit = 0;
int currFuel = 0;
int start = 0;
for(int i =0;i<gas.size();i++){
currFuel += gas[i]-cost[i];
if(currFuel<0){
deficit+=currFuel;
start = i+1;
currFuel = 0;
}
}
if(currFuel+deficit>=0) return start;
return -1;
}
};
23 changes: 23 additions & 0 deletions 136-single-number/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<h2><a href="https://leetcode.com/problems/single-number">Single Number</a></h2> <img src='https://img.shields.io/badge/Difficulty-Easy-brightgreen' alt='Difficulty: Easy' /><hr><p>Given a <strong>non-empty</strong>&nbsp;array of integers <code>nums</code>, every element appears <em>twice</em> except for one. Find that single one.</p>

<p>You must&nbsp;implement a solution with a linear runtime complexity and use&nbsp;only constant&nbsp;extra space.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [2,2,1]
<strong>Output:</strong> 1
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [4,1,2,1,2]
<strong>Output:</strong> 4
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> nums = [1]
<strong>Output:</strong> 1
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li>
<li><code>-3 * 10<sup>4</sup> &lt;= nums[i] &lt;= 3 * 10<sup>4</sup></code></li>
<li>Each element in the array appears twice except for one element which appears only once.</li>
</ul>
9 changes: 9 additions & 0 deletions 136-single-number/single-number.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution {
public int singleNumber(int[] nums) {
int res = 0;
for(int n : nums){
res = n ^ res;
}
return res;
}
}
20 changes: 20 additions & 0 deletions 137-single-number-ii/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<h2><a href="https://leetcode.com/problems/single-number-ii">Single Number II</a></h2> <img src='https://img.shields.io/badge/Difficulty-Medium-orange' alt='Difficulty: Medium' /><hr><p>Given an integer array <code>nums</code> where&nbsp;every element appears <strong>three times</strong> except for one, which appears <strong>exactly once</strong>. <em>Find the single element and return it</em>.</p>

<p>You must&nbsp;implement a solution with a linear runtime complexity and use&nbsp;only constant&nbsp;extra space.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [2,2,3,2]
<strong>Output:</strong> 3
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [0,1,0,1,0,1,99]
<strong>Output:</strong> 99
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li>
<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
<li>Each element in <code>nums</code> appears exactly <strong>three times</strong> except for one element which appears <strong>once</strong>.</li>
</ul>
17 changes: 17 additions & 0 deletions 137-single-number-ii/single-number-ii.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public int singleNumber(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();

for (int x : nums) {
map.put(x, map.getOrDefault(x, 0) + 1);
}

for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() == 1) {
return entry.getKey();
}
}

return -1;
}
}
44 changes: 44 additions & 0 deletions 1395-minimum-time-visiting-all-points/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<h2><a href="https://leetcode.com/problems/minimum-time-visiting-all-points">Minimum Time Visiting All Points</a></h2> <img src='https://img.shields.io/badge/Difficulty-Easy-brightgreen' alt='Difficulty: Easy' /><hr><p>On a 2D plane, there are <code>n</code> points with integer coordinates <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>. Return <em>the <strong>minimum time</strong> in seconds to visit all the points in the order given by </em><code>points</code>.</p>

<p>You can move according to these rules:</p>

<ul>
<li>In <code>1</code> second, you can either:

<ul>
<li>move vertically by one&nbsp;unit,</li>
<li>move horizontally by one unit, or</li>
<li>move diagonally <code>sqrt(2)</code> units (in other words, move one unit vertically then one unit horizontally in <code>1</code> second).</li>
</ul>
</li>
<li>You have to visit the points in the same order as they appear in the array.</li>
<li>You are allowed to pass through points that appear later in the order, but these do not count as visits.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/11/14/1626_example_1.PNG" style="width: 500px; height: 428px;" />
<pre>
<strong>Input:</strong> points = [[1,1],[3,4],[-1,0]]
<strong>Output:</strong> 7
<strong>Explanation: </strong>One optimal path is <strong>[1,1]</strong> -&gt; [2,2] -&gt; [3,3] -&gt; <strong>[3,4] </strong>-&gt; [2,3] -&gt; [1,2] -&gt; [0,1] -&gt; <strong>[-1,0]</strong>
Time from [1,1] to [3,4] = 3 seconds
Time from [3,4] to [-1,0] = 4 seconds
Total time = 7 seconds</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> points = [[3,2],[-2,2]]
<strong>Output:</strong> 5
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>points.length == n</code></li>
<li><code>1 &lt;= n&nbsp;&lt;= 100</code></li>
<li><code>points[i].length == 2</code></li>
<li><code>-1000&nbsp;&lt;= points[i][0], points[i][1]&nbsp;&lt;= 1000</code></li>
</ul>
Loading