Skip to content

Commit 6269673

Browse files
committed
Solved Leetcode Easy 0121. Best Time to Buy and sell question and also added Algorithms for Part 01 of the Array questions
1 parent 4928eb2 commit 6269673

File tree

11 files changed

+62
-0
lines changed

11 files changed

+62
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
### Using Additional Sets
2+
Algorithm: Use two sets to keep track of rows and columns that need to be zeroed. Iterate through the matrix to populate these sets, and then iterate again to set the appropriate rows and columns to 0.
3+
4+
Time Complexity: O(m * n)
5+
6+
Space Complexity: O(m + n)
7+
8+
### Using First Row and Column as Markers (Optimal)
9+
Algorithm: Use the first row and first column to mark the rows and columns that need to be zeroed. Use separate variables to track whether the first row and first column should be zeroed. Iterate through the matrix to set markers, then update the matrix based on these markers.
10+
11+
Time Complexity: O(m * n)
12+
13+
Space Complexity: O(1)
14+

problems/01. Arrays/Part 01/01. Leetcode Medium 0073. Set Matrix Zeroes/readme.md renamed to problems/01. Arrays/Part 01/01. Leetcode Medium 0073. Set Matrix Zeroes/Question.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Leetcode Medium 0073. Set Matrix Zeroes
12
Question
23
Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's.
34

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
### Brute Force Approach
2+
Algorithm: Use nested loops to generate each row of Pascal's Triangle. For each row, use an inner loop to compute the values based on the previous row.
3+
4+
Time Complexity: O(n^2)
5+
6+
Space Complexity: O(n^2)
7+
8+
This problem has only brute force approach

problems/01. Arrays/Part 01/02. Leetcode Medium 0118. Pascal's Triangle/readme.md renamed to problems/01. Arrays/Part 01/02. Leetcode Medium 0118. Pascal's Triangle/Question.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Leetcode Medium 0118. Pascal's Triangle
12
Given an integer numRows, return the first numRows of Pascal's triangle.
23

34
In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
### Linear Approach (Optimal)
3+
Time Complexity: O(n)
4+
Space Complexity: O(1)
5+
Algorithm:
6+
1. Find the first decreasing element from the end.
7+
2. Swap it with the smallest element larger than it from the end,
8+
3. then reverse the elements after the original position of the first decreasing element.

problems/01. Arrays/Part 01/03. Leetcode Medium 0031. Next Permutation/readme.md renamed to problems/01. Arrays/Part 01/03. Leetcode Medium 0031. Next Permutation/Question.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Leetcode Medium 0031. Next Permutation
12
A permutation of an array of integers is an arrangement of its members into a sequence or linear order.
23

34
For example, for arr = [1,2,3], the following are all the permutations of arr: [1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2], [3,2,1].
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
### Brute Force Approach
2+
Algorithm: Instead of calculating the sum of each subarray from scratch, keep a running sum of the subarray.
3+
4+
Time Complexity: O(n^2)
5+
6+
Space Complexity: O(1)
7+
8+
### Kadane’s Algorithm (Optimal)
9+
Algorithm: Iterate through the array while keeping track of the maximum subarray sum ending at the current position. Update the global maximum if the current subarray sum is higher.
10+
11+
Time Complexity: O(n)
12+
13+
Space Complexity: O(1)
14+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### 1. Brute Force Approach
2+
Time Complexity: O(n^2)
3+
Space Complexity: O(1)
4+
Algorithm: Iterate through each pair of days, calculate the profit for each pair, and keep track of the maximum profit.
5+
6+
### 2. One Pass Approach
7+
Time Complexity: O(n)
8+
Space Complexity: O(1)
9+
Algorithm: Track the minimum price as we iterate through the array. Calculate the profit for each day by subtracting the minimum price from the current price, and update the maximum profit if the current profit is higher.
10+
11+
### 3. Divide and Conquer Approach
12+
Time Complexity: O(n log n)
13+
Space Complexity: O(log n)

0 commit comments

Comments
 (0)