Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
2086d5e
Create 2530. Maximal Score After Applying K Operations
Harshil525 Oct 14, 2024
512159b
Merge pull request #1 from Harshil525/patch-1
20sinkypriya Oct 14, 2024
336349c
Create 632. Smallest Range Covering Elements from K Lists
Harshil525 Oct 14, 2024
97ebf34
Merge pull request #2 from Harshil525/patch-2
20sinkypriya Oct 14, 2024
223b07b
Create substringWithConcatenationOfAllWorldsLeetcode30.java
aks007837 Oct 23, 2024
ccd0510
Create splitAStringIntoNumberOfUniqueSubstrings.java
aks007837 Oct 23, 2024
837f70c
Merge pull request #6 from aks007837/patch-2
20sinkypriya Oct 23, 2024
fc34a04
Merge pull request #5 from aks007837/patch-1
20sinkypriya Oct 23, 2024
bf7948c
Added GreddyAlgoProblems
Nilakshi13 Oct 28, 2024
d17a63c
Revert "Added GreddyAlgoProblems"
AyushKumar55 Oct 28, 2024
4ccea4b
Added Greddy Algo. Problems
AyushKumar55 Oct 28, 2024
b8da6b0
Merge pull request #9 from AyushKumar55/main
20sinkypriya Oct 28, 2024
ef65f71
leetcode 2501 solution uploaded
Oct 28, 2024
b855257
Merge pull request #10 from ayush-singh-26/leetcode-daily-solution
20sinkypriya Oct 29, 2024
4037fb6
initial status
meghakarmakar Oct 29, 2024
a435230
Merge branch '20sinkypriya:main' into main
meghakarmakar Oct 29, 2024
2f79a65
Merge pull request #11 from meghakarmakar/main
20sinkypriya Oct 29, 2024
cf36427
Solution to reverse integer in java
meghakarmakar Oct 29, 2024
ebe0585
Merge pull request #12 from meghakarmakar/main
20sinkypriya Oct 29, 2024
35a5dd9
Adding solution to power of an integer
meghakarmakar Oct 29, 2024
eca0ec1
Merge pull request #13 from meghakarmakar/main
20sinkypriya Oct 30, 2024
c2311e0
Add files via upload
Ayush07571 Oct 31, 2024
e6077d8
Add files via upload
Ayush07571 Oct 31, 2024
627900c
Delete Guess_a_number.java
Ayush07571 Oct 31, 2024
01b100a
Merge pull request #15 from Ayush07571/main
20sinkypriya Oct 31, 2024
998d5a3
Guess a number file
Ayush07571 Oct 31, 2024
8d11513
Merge pull request #17 from Ayush07571/main
20sinkypriya Oct 31, 2024
3dab302
Leetcode 31oct solution uploaded
Oct 31, 2024
227c65d
Merge pull request #18 from ayush-singh-26/leetcode-daily-solution
20sinkypriya Oct 31, 2024
8abe370
Create 1. Two Sum
Harshil525 Oct 28, 2025
36bf0b1
Merge pull request #22 from Harshil525/patch-3
20sinkypriya Oct 28, 2025
acbdc10
Create 4. Median of Two Sorted Arrays
Harshil525 Oct 28, 2025
6cb3f17
Merge pull request #27 from Harshil525/patch-6
20sinkypriya Oct 28, 2025
b2eb982
edited 4 files and added power code in python
Oct 28, 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
Binary file added .DS_Store
Binary file not shown.
8 changes: 8 additions & 0 deletions 1. Two Sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
pair_idx = {}

for i, num in enumerate(nums):
if target - num in pair_idx:
return [i, pair_idx[target - num]]
pair_idx[num] = i
60 changes: 60 additions & 0 deletions 2463.Minimum-Total-Distance-Traveled
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Problem Statement:

There are some robots and factories on the X-axis. You are given an integer array robot where robot[i] is the position of the ith robot. You are also given a 2D integer array factory where factory[j] = [positionj, limitj] indicates that positionj is the position of the jth factory and that the jth factory can repair at most limitj robots.

The positions of each robot are unique. The positions of each factory are also unique. Note that a robot can be in the same position as a factory initially.

All the robots are initially broken; they keep moving in one direction. The direction could be the negative or the positive direction of the X-axis. When a robot reaches a factory that did not reach its limit, the factory repairs the robot, and it stops moving.

At any moment, you can set the initial direction of moving for some robot. Your target is to minimize the total distance traveled by all the robots.

Return the minimum total distance traveled by all the robots. The test cases are generated such that all the robots can be repaired.

Note that

All robots move at the same speed.
If two robots move in the same direction, they will never collide.
If two robots move in opposite directions and they meet at some point, they do not collide. They cross each other.
If a robot passes by a factory that reached its limits, it crosses it as if it does not exist.
If the robot moved from a position x to a position y, the distance it moved is |y - x|.


Solution:

class Solution {
public:
long long minimumTotalDistance(vector<int>& robot, vector<vector<int>>& factory) {
sort(robot.begin(), robot.end());
sort(factory.begin(), factory.end());

int m = robot.size(), n = factory.size();
vector<vector<long long>> dp(m + 1, vector<long long>(n + 1));

for (int i = 0; i < m; i++) {
dp[i][n] = LLONG_MAX;
}

for (int j = n - 1; j >= 0; j--) {
long long prefix = 0;
deque<pair<int, long long>> qq;
qq.push_back({m, 0});

for (int i = m - 1; i >= 0; i--) {
prefix += abs(robot[i] - factory[j][0]);

while (!qq.empty() && qq.front().first > i + factory[j][1]) {
qq.pop_front();
}

while (!qq.empty() && qq.back().second >= dp[i][j + 1] - prefix) {
qq.pop_back();
}

qq.push_back({i, dp[i][j + 1] - prefix});
dp[i][j] = qq.front().second + prefix;
}
}

return dp[0][0];
}
};
60 changes: 60 additions & 0 deletions 2501. Longest Square Streak in an Array
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Problem Statement:

You are given an integer array nums. A subsequence of nums is called a square streak if:

The length of the subsequence is at least 2, and
after sorting the subsequence, each element (except the first element) is the square of the previous number.
Return the length of the longest square streak in nums, or return -1 if there is no square streak.

A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.


Example 1:

Input: nums = [4,3,6,16,8,2]
Output: 3
Explanation: Choose the subsequence [4,16,2]. After sorting it, it becomes [2,4,16].
- 4 = 2 * 2.
- 16 = 4 * 4.
Therefore, [4,16,2] is a square streak.
It can be shown that every subsequence of length 4 is not a square streak.
Example 2:

Input: nums = [2,3,5,6,7]
Output: -1
Explanation: There is no square streak in nums so return -1.


Constraints:

2 <= nums.length <= 105
2 <= nums[i] <= 105


Solution:

class Solution {
public:
int longestSquareStreak(vector<int>& nums) {
int max = -1;
unordered_set<int> numSet(nums.begin(), nums.end());
vector<int> sortedNums(numSet.begin(), numSet.end());

sort(sortedNums.begin(), sortedNums.end());

for (int num : sortedNums) {
long long curr = num;
int count = 0;

while (numSet.count(curr)) {
numSet.erase(curr);
curr *= curr;
count++;
}

max = max(max, count);
}

return max > 1 ? max : -1;
}
};
48 changes: 48 additions & 0 deletions 2530. Maximal Score After Applying K Operations
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
You are given a 0-indexed integer array nums and an integer k. You have a starting score of 0.

In one operation:

choose an index i such that 0 <= i < nums.length,
increase your score by nums[i], and
replace nums[i] with ceil(nums[i] / 3).
Return the maximum possible score you can attain after applying exactly k operations.

The ceiling function ceil(val) is the least integer greater than or equal to val.



Example 1:

Input: nums = [10,10,10,10,10], k = 5
Output: 50
Explanation: Apply the operation to each array element exactly once. The final score is 10 + 10 + 10 + 10 + 10 = 50.
Example 2:

Input: nums = [1,10,3,3,3], k = 3
Output: 17
Explanation: You can do the following operations:
Operation 1: Select i = 1, so nums becomes [1,4,3,3,3]. Your score increases by 10.
Operation 2: Select i = 1, so nums becomes [1,2,3,3,3]. Your score increases by 4.
Operation 3: Select i = 2, so nums becomes [1,1,1,3,3]. Your score increases by 3.
The final score is 10 + 4 + 3 = 17.


Constraints:

1 <= nums.length, k <= 105
1 <= nums[i] <= 109


Solution:
class Solution:
def maxKelements(self, nums: List[int], k: int) -> int:
nums = [-i for i in nums]
heapify(nums)

res = 0
for i in range(k):
t = -heappop(nums)
res += t
heappush(nums, -ceil(t / 3))

return res
13 changes: 13 additions & 0 deletions 4. Median of Two Sorted Arrays
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
# Merge the two sorted arrays
merged = sorted(nums1 + nums2)
length = len(merged)

# Check if the total length is even or odd
if length % 2 == 0:
# If even, return the average of the two middle elements
return (merged[length // 2 - 1] + merged[length // 2]) / 2
else:
# If odd, return the middle element
return merged[length // 2]
9 changes: 9 additions & 0 deletions 50.Pow(x,n).java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution {
public double myPow(double x, int n) {
return(Math.pow(x,n));
}
public static void main(String args[]){
Solution obj = new Solution();
System.out.println(obj.myPow(2.0,4));
}
}
63 changes: 63 additions & 0 deletions 632. Smallest Range Covering Elements from K Lists
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Problem Statement:

You have k lists of sorted integers in non-decreasing order. Find the smallest range that includes at least one number from each of the k lists.

We define the range [a, b] is smaller than range [c, d] if b - a < d - c or a < c if b - a == d - c.



Example 1:

Input: nums = [[4,10,15,24,26],[0,9,12,20],[5,18,22,30]]
Output: [20,24]
Explanation:
List 1: [4, 10, 15, 24,26], 24 is in range [20,24].
List 2: [0, 9, 12, 20], 20 is in range [20,24].
List 3: [5, 18, 22, 30], 22 is in range [20,24].
Example 2:

Input: nums = [[1,2,3],[1,2,3],[1,2,3]]
Output: [1,1]


Constraints:

nums.length == k
1 <= k <= 3500
1 <= nums[i].length <= 50
-105 <= nums[i][j] <= 105
nums[i] is sorted in non-decreasing order.


Solution:
from typing import List
import heapq


class Solution:
def smallestRange(self, nums: List[List[int]]) -> List[int]:
heap = [(row[0], i, 0) for i, row in enumerate(nums)]
heapq.heapify(heap)
ans = [-10**9, 10**9]
right = max(row[0] for row in nums)
while heap:
left, row, col = heapq.heappop(heap)
if right - left < ans[1] - ans[0]:
ans = [left, right]
if col + 1 == len(nums[row]):
return ans
right = max(right, nums[row][col + 1])
heapq.heappush(heap, (nums[row][col + 1], row, col + 1))

# Tests:
if __name__ == '__main__':
s = Solution()
# test case 1
output1 = s.smallestRange([[4,10,15,24,26],[0,9,12,20],[5,18,22,30]])
expected_output1 = [20,24]
assert output1 == expected_output1, f"Expected {expected_output1}, but got {output1}"
# test case 2
output2 = s.smallestRange([[1,2,3],[1,2,3],[1,2,3]])
expected_output2 = [1,1]
assert output2 == expected_output2, f"Expected {expected_output2}, but got {output2}"
print("All tests passed!")
37 changes: 37 additions & 0 deletions GreedyAlgoProblems/BuyTwoChocolates_2706.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Difficulty - EASY
You are given an integer array prices representing the prices of various chocolates in a store. You are also given a single integer money, which represents your initial amount of money.

You must buy exactly two chocolates in such a way that you still have some non-negative leftover money. You would like to minimize the sum of the prices of the two chocolates you buy.

Return the amount of money you will have leftover after buying the two chocolates. If there is no way for you to buy two chocolates without ending up in debt, return money. Note that the leftover must be non-negative.

Example 1:

Input: prices = [1,2,2], money = 3
Output: 0
Explanation: Purchase the chocolates priced at 1 and 2 units respectively. You will have 3 - 3 = 0 units of money afterwards. Thus, we return 0.

Example 2:

Input: prices = [3,2,3], money = 3
Output: 3
Explanation: You cannot buy 2 chocolates without going in debt, so we return 3.

Constraints:

2 <= prices.length <= 50
1 <= prices[i] <= 100
1 <= money <= 100
*/

import java.util.Arrays;

class Solution {
public int buyChoco(int[] prices, int money) {
Arrays.sort(prices);
if(prices[0] + prices[1] <= money)
return money - (prices[0] + prices[1]);
return money;
}
}
46 changes: 46 additions & 0 deletions GreedyAlgoProblems/JumpGame2_45.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0].

Each element nums[i] represents the maximum length of a forward jump from index i. In other words, if you are at nums[i], you can jump to any nums[i + j] where:

0 <= j <= nums[i] and
i + j < n
Return the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that you can reach nums[n - 1].

Example 1:

Input: nums = [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

Input: nums = [2,3,0,1,4]
Output: 2

Constraints:

1 <= nums.length <= 104
0 <= nums[i] <= 1000
It's guaranteed that you can reach nums[n - 1].
*/

class Solution {
public int jump(int[] nums) {
int left=0;
int right=0;
int jumps=0;
while(right<nums.length-1){
int maxIndex=0;
for(int i=left;i<=right;i++){
maxIndex=Math.max((i+nums[i]),maxIndex);

}
left=right+1;
right=maxIndex;
jumps++;


}return jumps;
}
}
35 changes: 35 additions & 0 deletions GreedyAlgoProblems/JumpGame_55.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Difficulty - MEDIUM
You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.

Return true if you can reach the last index, or false otherwise.

Example 1:

Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

Input: nums = [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.

Constraints:

1 <= nums.length <= 104
0 <= nums[i] <= 105
*/

class Solution {
public boolean canJump(int[] nums) {
int maxIndex = 0;
for(int i = 0; i < nums.length; i++){
if(i > maxIndex)
return false;
maxIndex = Math.max(maxIndex, i + nums[i]);
}
return true;
}
}
Loading