Skip to content

Commit 287de99

Browse files
Initial commit: LeetCode solutions repository with 2 Medium problems
0 parents  commit 287de99

File tree

13 files changed

+858
-0
lines changed

13 files changed

+858
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# GitHub Actions workflow for validating repository structure
2+
# This is a template for future use
3+
4+
name: Validate Repository Structure
5+
6+
on:
7+
push:
8+
branches: [ main, master ]
9+
pull_request:
10+
branches: [ main, master ]
11+
12+
jobs:
13+
validate:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v3
19+
20+
- name: Validate folder structure
21+
run: |
22+
echo "Validating repository structure..."
23+
# Add validation logic here in the future
24+
echo "✅ Repository structure validation passed"
25+
26+
- name: Check README format
27+
run: |
28+
echo "Checking README.md format..."
29+
if [ -f "README.md" ]; then
30+
echo "✅ README.md exists"
31+
else
32+
echo "❌ README.md not found"
33+
exit 1
34+
fi

.gitignore

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# LeetCode Solutions Repository .gitignore
2+
3+
# IDE and Editor files
4+
.vscode/
5+
.idea/
6+
*.swp
7+
*.swo
8+
*~
9+
10+
# OS generated files
11+
.DS_Store
12+
.DS_Store?
13+
._*
14+
.Spotlight-V100
15+
.Trashes
16+
ehthumbs.db
17+
Thumbs.db
18+
19+
# Compiled files
20+
*.class
21+
*.o
22+
*.exe
23+
*.out
24+
25+
# Logs
26+
*.log
27+
28+
# Temporary files
29+
*.tmp
30+
*.temp
31+
32+
# Node modules (if using any JavaScript tools)
33+
node_modules/
34+
35+
# Python cache
36+
__pycache__/
37+
*.pyc
38+
*.pyo
39+
*.pyd
40+
.Python
41+
*.so
42+
43+
# Java compiled files
44+
*.class
45+
*.jar
46+
47+
# C++ compiled files
48+
*.exe
49+
*.out
50+
*.o
51+
*.a
52+
*.so
53+
*.dylib
54+
55+
# Backup files
56+
*.bak
57+
*.backup
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Two Sum - Problem #1
2+
3+
## Problem Statement
4+
Given an array of integers `nums` and an integer `target`, return indices of the two numbers such that they add up to `target`.
5+
6+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
7+
8+
You can return the answer in any order.
9+
10+
## Examples
11+
```
12+
Input: nums = [2,7,11,15], target = 9
13+
Output: [0,1]
14+
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
15+
16+
Input: nums = [3,2,4], target = 6
17+
Output: [1,2]
18+
19+
Input: nums = [3,3], target = 6
20+
Output: [0,1]
21+
```
22+
23+
## Approach
24+
**Hash Map Approach:**
25+
1. Use a HashMap to store each number and its index
26+
2. For each number, calculate the complement (target - current_number)
27+
3. If complement exists in HashMap, we found our pair
28+
4. Otherwise, add current number and its index to HashMap
29+
30+
## Complexity Analysis
31+
- **Time Complexity**: O(n) - Single pass through the array
32+
- **Space Complexity**: O(n) - HashMap to store numbers and indices
33+
34+
## Key Insights
35+
- Using HashMap allows O(1) lookup time for complements
36+
- Single pass solution is more efficient than nested loops
37+
- Trade-off: extra space for better time complexity
38+
39+
## Alternative Approaches
40+
1. **Brute Force**: O(n²) time, O(1) space
41+
2. **Two Pointers**: Requires sorted array, O(n log n) time
42+
43+
## Solutions in Different Languages
44+
45+
### Java
46+
```java
47+
// See solution.java
48+
class Solution {
49+
public int[] twoSum(int[] nums, int target) {
50+
Map<Integer, Integer> map = new HashMap<>();
51+
52+
for (int i = 0; i < nums.length; i++) {
53+
int complement = target - nums[i];
54+
55+
if (map.containsKey(complement)) {
56+
return new int[] {map.get(complement), i};
57+
}
58+
59+
map.put(nums[i], i);
60+
}
61+
62+
return new int[] {}; // No solution found
63+
}
64+
}
65+
```
66+
67+
### JavaScript
68+
```javascript
69+
// See solution.js
70+
var twoSum = function(nums, target) {
71+
const map = new Map();
72+
73+
for (let i = 0; i < nums.length; i++) {
74+
const complement = target - nums[i];
75+
76+
if (map.has(complement)) {
77+
return [map.get(complement), i];
78+
}
79+
80+
map.set(nums[i], i);
81+
}
82+
83+
return []; // No solution found
84+
};
85+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Solution for LeetCode Problem #1: Two Sum
2+
// Date: 2025-01-27
3+
// Difficulty: Easy
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
class Solution {
9+
public int[] twoSum(int[] nums, int target) {
10+
Map<Integer, Integer> map = new HashMap<>();
11+
12+
for (int i = 0; i < nums.length; i++) {
13+
int complement = target - nums[i];
14+
15+
if (map.containsKey(complement)) {
16+
return new int[] {map.get(complement), i};
17+
}
18+
19+
map.put(nums[i], i);
20+
}
21+
22+
return new int[] {}; // No solution found
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Solution for LeetCode Problem #1: Two Sum
2+
// Date: 2025-01-27
3+
// Difficulty: Easy
4+
// Language: JavaScript
5+
6+
/**
7+
* @param {number[]} nums
8+
* @param {number} target
9+
* @return {number[]}
10+
*/
11+
var twoSum = function(nums, target) {
12+
const map = new Map();
13+
14+
for (let i = 0; i < nums.length; i++) {
15+
const complement = target - nums[i];
16+
17+
if (map.has(complement)) {
18+
return [map.get(complement), i];
19+
}
20+
21+
map.set(nums[i], i);
22+
}
23+
24+
return []; // No solution found
25+
};
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# Count Number of Maximum Bitwise OR Subsets - Problem #2044
2+
3+
## Problem Statement
4+
You are given an integer array nums. We call a subset of nums good if its bitwise OR is a maximum value among all possible subsets of nums.
5+
6+
Let size be the number of integers in a good subset.
7+
8+
Return the number of different good subsets of size size.
9+
10+
## Examples
11+
```
12+
Input: nums = [3,1]
13+
Output: 2
14+
Explanation: The maximum possible bitwise OR of a subset is 3. There are 2 subsets with a bitwise OR of 3:
15+
- [3]
16+
- [3,1]
17+
18+
Input: nums = [2,2,2]
19+
Output: 7
20+
Explanation: All non-empty subsets of [2,2,2] have a bitwise OR of 2. There are 2^3 - 1 = 7 total subsets.
21+
22+
Input: nums = [3,2,1,5]
23+
Output: 6
24+
Explanation: The maximum possible bitwise OR of a subset is 7, and the subsets with a bitwise OR of 7 are:
25+
- [3,5]
26+
- [3,1,5]
27+
- [3,2,5]
28+
- [3,2,1,5]
29+
- [1,5]
30+
- [2,1,5]
31+
```
32+
33+
## Approach
34+
**Backtracking with Bitwise OR Tracking:**
35+
1. First, find the maximum possible bitwise OR value among all possible subsets
36+
2. Use backtracking to generate all possible subsets
37+
3. For each subset, calculate its bitwise OR value
38+
4. Count how many subsets achieve the maximum OR value
39+
5. Among those subsets, count how many have the maximum size
40+
41+
**Algorithm Steps:**
42+
1. Calculate the maximum possible OR value by OR-ing all elements
43+
2. Use backtracking to generate all possible subsets
44+
3. For each subset, track its OR value and size
45+
4. Count subsets that achieve maximum OR and have maximum size among those
46+
47+
## Complexity Analysis
48+
- **Time Complexity**: O(2^n) - We generate all possible subsets using backtracking
49+
- **Space Complexity**: O(n) - Recursion stack depth for backtracking
50+
51+
## Key Insights
52+
- The maximum possible OR value is the OR of all elements in the array
53+
- We need to find all subsets that achieve this maximum OR value
54+
- Among those subsets, we count the ones with the maximum size
55+
- Backtracking is efficient for generating all possible subsets
56+
- We can optimize by tracking the maximum size among subsets that achieve the target OR
57+
58+
## Alternative Approaches
59+
1. **Bit Manipulation**: Use bit masks to represent subsets - O(2^n)
60+
2. **Dynamic Programming**: Use DP to track OR values for different subset sizes
61+
3. **Greedy**: Optimize by stopping early when maximum OR is achieved
62+
63+
## Solutions in Different Languages
64+
65+
### Java
66+
```java
67+
class Solution {
68+
private int maxOR = 0;
69+
private int maxSize = 0;
70+
private int count = 0;
71+
72+
public int countMaxOrSubsets(int[] nums) {
73+
// Calculate the maximum possible OR value
74+
maxOR = 0;
75+
for (int num : nums) {
76+
maxOR |= num;
77+
}
78+
79+
// Reset counters
80+
maxSize = 0;
81+
count = 0;
82+
83+
// Use backtracking to find all subsets
84+
backtrack(nums, 0, 0, 0);
85+
86+
return count;
87+
}
88+
89+
private void backtrack(int[] nums, int index, int currentOR, int currentSize) {
90+
// If we've processed all elements
91+
if (index == nums.length) {
92+
// If this subset achieves the maximum OR
93+
if (currentOR == maxOR) {
94+
// If this is the first subset with max OR, or same size as current max
95+
if (currentSize > maxSize) {
96+
maxSize = currentSize;
97+
count = 1;
98+
} else if (currentSize == maxSize) {
99+
count++;
100+
}
101+
}
102+
return;
103+
}
104+
105+
// Include the current element
106+
backtrack(nums, index + 1, currentOR | nums[index], currentSize + 1);
107+
108+
// Exclude the current element
109+
backtrack(nums, index + 1, currentOR, currentSize);
110+
}
111+
}
112+
```
113+
114+
### JavaScript
115+
```javascript
116+
var countMaxOrSubsets = function(nums) {
117+
// Calculate the maximum possible OR value
118+
let maxOR = 0;
119+
for (let num of nums) {
120+
maxOR |= num;
121+
}
122+
123+
let maxSize = 0;
124+
let count = 0;
125+
126+
// Use backtracking to find all subsets
127+
function backtrack(index, currentOR, currentSize) {
128+
// If we've processed all elements
129+
if (index === nums.length) {
130+
// If this subset achieves the maximum OR
131+
if (currentOR === maxOR) {
132+
// If this is the first subset with max OR, or same size as current max
133+
if (currentSize > maxSize) {
134+
maxSize = currentSize;
135+
count = 1;
136+
} else if (currentSize === maxSize) {
137+
count++;
138+
}
139+
}
140+
return;
141+
}
142+
143+
// Include the current element
144+
backtrack(index + 1, currentOR | nums[index], currentSize + 1);
145+
146+
// Exclude the current element
147+
backtrack(index + 1, currentOR, currentSize);
148+
}
149+
150+
backtrack(0, 0, 0);
151+
return count;
152+
};
153+
```

0 commit comments

Comments
 (0)