-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'iamdestinychild:main' into 35-Search-Insert-Position
- Loading branch information
Showing
40 changed files
with
976 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# 71. Simplify Path | ||
|
||
Given a string path, which is an absolute path (starting with a slash '/') to a file or directory in a Unix-style file system, convert it to the simplified canonical path. | ||
|
||
In a Unix-style file system, a period '.' refers to the current directory, a double period '..' refers to the directory up a level, and any multiple consecutive slashes (i.e. '//') are treated as a single slash '/'. For this problem, any other format of periods such as '...' are treated as file/directory names. | ||
|
||
The canonical path should have the following format: | ||
|
||
1)The path starts with a single slash '/'. | ||
2)Any two directories are separated by a single slash '/'. | ||
3)The path does not end with a trailing '/'. | ||
4)The path only contains the directories on the path from the root directory to the target file or directory (i.e., no period '.' or double period '..') | ||
5)Return the simplified canonical path. | ||
|
||
## Examples | ||
|
||
### Example 1: | ||
|
||
Input: path = "/home/" | ||
Output: "/home" | ||
Explanation: Note that there is no trailing slash after the last directory name. | ||
|
||
### Example 2: | ||
|
||
Input: path = "/../" | ||
Output: "/" | ||
Explanation: Going one level up from the root directory is a no-op, as the root level is the highest level you can go. | ||
|
||
### Example 3: | ||
|
||
Input: path = "/home//foo/" | ||
Output: "/home/foo" | ||
Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one. | ||
|
||
## Constraints | ||
|
||
1 <= path.length <= 3000 | ||
path consists of English letters, digits, period '.', slash '/' or '_'. | ||
path is a valid absolute Unix path. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# 71. Simplify Path (https://leetcode.com/problems/simplify-path/description/) | ||
|
||
Given a string path, which is an absolute path (starting with a slash '/') to a file or directory in a Unix-style file system, convert it to the simplified canonical path. | ||
|
||
In a Unix-style file system, a period '.' refers to the current directory, a double period '..' refers to the directory up a level, and any multiple consecutive slashes (i.e. '//') are treated as a single slash '/'. For this problem, any other format of periods such as '...' are treated as file/directory names. | ||
|
||
The canonical path should have the following format: | ||
|
||
The path starts with a single slash '/'. | ||
Any two directories are separated by a single slash '/'. | ||
The path does not end with a trailing '/'. | ||
The path only contains the directories on the path from the root directory to the target file or directory (i.e., no period '.' or double period '..') | ||
Return the simplified canonical path. | ||
|
||
## Examples | ||
|
||
### Example 1: | ||
|
||
Input: path = "/home/" | ||
Output: "/home" | ||
Explanation: Note that there is no trailing slash after the last directory name. | ||
|
||
### Example 2: | ||
|
||
Input: path = "/../" | ||
Output: "/" | ||
Explanation: Going one level up from the root directory is a no-op, as the root level is the highest level you can go. | ||
|
||
### Example 3: | ||
|
||
Input: path = "/home//foo/" | ||
Output: "/home/foo" | ||
Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one. | ||
|
||
## Constraints | ||
|
||
1 <= path.length <= 3000 | ||
path consists of English letters, digits, period '.', slash '/' or '_'. | ||
path is a valid absolute Unix path. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
This code was done using python | ||
|
||
To run the above code in terminal type the below code | ||
|
||
python main.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Solution: | ||
def simplifyPath(self, path: str) -> str: | ||
stack = [] | ||
for dir in path.split('/'): | ||
if dir == '' or dir == '.': | ||
continue | ||
elif dir == '..': | ||
if stack: | ||
stack.pop() | ||
else: | ||
stack.append(dir) | ||
|
||
return '/' + '/'.join(stack) | ||
|
||
def main(): | ||
solution = Solution() | ||
path = "/home//foo/" #Example case hardcoded | ||
soln = solution.simplifyPath(path) | ||
print(soln) | ||
|
||
if __name__ =="__main__": | ||
main() |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
## 896. Monotonic Array | ||
An array is monotonic if it is either monotone increasing or monotone decreasing. | ||
|
||
An array nums is monotone increasing if for all i <= j, nums[i] <= nums[j]. An array nums is monotone decreasing if for all i <= j, nums[i] >= nums[j]. | ||
|
||
Given an integer array nums, return true if the given array is monotonic, or false otherwise. | ||
|
||
|
||
|
||
### Example 1: | ||
|
||
Input: nums = [1,2,2,3] | ||
Output: true | ||
|
||
### Example 2: | ||
|
||
Input: nums = [6,5,4,4] | ||
Output: true | ||
|
||
### Example 3: | ||
|
||
Input: nums = [1,3,2] | ||
Output: false | ||
|
||
|
||
### Constraints: | ||
|
||
1 <= nums.length <= 10^5 | ||
-10^5 <= nums[i] <= 10^5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Solution: | ||
def isMonotonic(self, nums: list[int]) -> bool: | ||
is_increasing = True # Indicates if the array is increasing. | ||
is_decreasing = True # Indicates if the array is decreasing. | ||
|
||
# Check if the array is either increasing or non-increasing. | ||
for i in range(1, len(nums)): | ||
# Check increasing condition. | ||
if nums[i] < nums[i - 1]: | ||
is_increasing = False | ||
|
||
# Check decreasing condition. | ||
elif nums[i] > nums[i - 1]: | ||
is_decreasing = False | ||
|
||
# If it is neither increasing nor decreasing then don't continue the loop. | ||
if not is_increasing and not is_decreasing: | ||
break | ||
|
||
return is_increasing or is_decreasing # Return true if either condition is met |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions
1
... Except Self/C++/.cph/.ProductofArrayExceptSelf.cpp_24f464cf1dffef995da744cd7929e46a.prob
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"name":"Local: ProductofArrayExceptSelf","url":"c:\\Hackoctober\\C++\\ProductofArrayExceptSelf.cpp","tests":[{"id":1696333954295,"input":"4\n1 2 3 4","output":"24 12 8 6"},{"id":1696333991467,"input":"5\n-1 1 0 -3 3","output":"0 0 9 0 0"}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\Hackoctober\\C++\\ProductofArrayExceptSelf.cpp","group":"local","local":true} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
## HINT | ||
For any nums[i], calculate its left product and calculate its right product, without including nums[i]. | ||
Then multiply these left and right product, This will give product of array excluding nums[i]. | ||
|
||
## HINT FOR FOLLOW UP | ||
The output array does not count as extra space for space complexity analysis. (This statement itself is a hint.) | ||
|
||
## TIME COMPLEXITY | ||
O(n) | ||
|
||
## SPACE COMPLEXITY | ||
O(1) (if we do not consider output array as extra space for space complexity analysis) |
40 changes: 40 additions & 0 deletions
40
LeetCode/Product of Array Except Self/C++/ProductofArrayExceptSelf.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
vector<int> productExceptSelf(vector<int>& nums) { | ||
vector <int> output(nums.size()); | ||
int prefix = 1; | ||
int suffix = 1; | ||
output[0] = 1; | ||
output[nums.size() - 1] = 1; | ||
for(int i = 1; i < nums.size(); i++){ | ||
prefix = prefix * nums[i - 1]; | ||
output[i] = prefix; | ||
} | ||
for(int i = 1; i < nums.size(); i++){ | ||
suffix = suffix * nums[nums.size() - i]; | ||
output[nums.size() - 1 - i] = output[nums.size() - 1 - i] * suffix; | ||
} | ||
return output; | ||
} | ||
}; | ||
|
||
int main(){ | ||
|
||
Solution solution; | ||
int n; | ||
cin>>n; | ||
vector<int> nums(n); | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cin>>nums[i]; | ||
} | ||
vector<int> result = solution.productExceptSelf(nums); | ||
for (int i = 0; i < n; i++) | ||
{ | ||
cout<<result[i]<<" "; | ||
} | ||
cout<<endl; | ||
} |
Binary file added
BIN
+60.8 KB
LeetCode/Product of Array Except Self/C++/Screenshot 2023-10-03 174225.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. | ||
|
||
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. | ||
|
||
You must write an algorithm that runs in O(n) time and without using the division operation. | ||
|
||
|
||
|
||
Example 1: | ||
|
||
Input: nums = [1,2,3,4] | ||
Output: [24,12,8,6] | ||
Example 2: | ||
|
||
Input: nums = [-1,1,0,-3,3] | ||
Output: [0,0,9,0,0] | ||
|
||
|
||
Constraints: | ||
|
||
2 <= nums.length <= 105 | ||
-30 <= nums[i] <= 30 | ||
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. | ||
|
||
|
||
Follow up: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.) |
9 changes: 9 additions & 0 deletions
9
LeetCode/Remove-Duplicates-From-Sorted-List/Java/ListNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
public class ListNode { | ||
int val; | ||
ListNode next; | ||
ListNode() {} | ||
ListNode(int val) { this.val = val; } | ||
ListNode(int val, ListNode next) { | ||
this.val = val; this.next = next; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
LeetCode/Remove-Duplicates-From-Sorted-List/Java/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# How To Start The Project | ||
|
||
**This code is written in java, You need to have jdk installed in your system to run the program.** | ||
**Use the below commands to Compile and run the Program** | ||
|
||
``` | ||
Compilation: javac Solution.java | ||
Run: java Solution | ||
``` | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions
40
LeetCode/Remove-Duplicates-From-Sorted-List/Java/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
public class Solution { | ||
public static ListNode deleteDuplicates(ListNode head) { | ||
|
||
if(head==null || head.next==null) | ||
return head; | ||
|
||
ListNode prev = head; | ||
ListNode temp = head.next; | ||
while(temp!=null){ | ||
if(temp.val==prev.val){ | ||
prev.next = temp.next; | ||
temp = prev.next; | ||
} | ||
else { | ||
temp= temp.next; | ||
prev=prev.next; | ||
} | ||
} | ||
return head; | ||
|
||
} | ||
|
||
public static void main(String[] args) { | ||
ListNode head = new ListNode(1); | ||
head.next = new ListNode(1); | ||
head.next.next = new ListNode(2); | ||
head.next.next.next = null; | ||
System.out.println("Given List"); | ||
printList(head); | ||
ListNode ans = deleteDuplicates(head); | ||
System.out.println("\nAnswer List"); | ||
printList(ans); | ||
} | ||
private static void printList(ListNode head) { | ||
while(head!=null) { | ||
System.out.print(head.val + " "); | ||
head = head.next; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# [83. Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/description/) | ||
|
||
Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. | ||
|
||
**Example 1:** | ||
|
||
![example-1](/LeetCode/Remove-Duplicates-From-Sorted-List/example-1.png) | ||
```example 1 | ||
Input: head = [1,1,2] | ||
Output: [1,2] | ||
``` | ||
|
||
**Example 2:** | ||
|
||
![example-2](/LeetCode/Remove-Duplicates-From-Sorted-List/example-2.png) | ||
``` | ||
Input: head = [1,1,2,3,3] | ||
Output: 5, nums = [1,2,3] | ||
``` | ||
|
||
**Constraints:** | ||
- The number of nodes in the list is in the range [0, 300]. | ||
- -100 <= Node.val <= 100 | ||
- The list is guaranteed to be sorted in ascending order. | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.