Competitive Programming Tasks 1
Competitive Programming Tasks 1
**Steps**:
**Example Code**:
```python
result = []
if i % 3 == 0 and i % 5 == 0:
result.append("FizzBuzz")
elif i % 3 == 0:
result.append("Fizz")
elif i % 5 == 0:
result.append("Buzz")
else:
result.append(str(i))
return result
```
**Steps**:
**Example Code**:
```python
```
**Steps**:
- If the complement is in the dictionary, return its index and the current index.
```python
num_map = {}
if complement in num_map:
return [num_map[complement], i]
num_map[num] = i
```
**Steps**:
4. If a character is repeated, move the left pointer until the substring is valid.
**Example Code**:
```python
char_set = set()
left = max_length = 0
char_set.remove(s[left])
left += 1
char_set.add(s[right])
return max_length
```
**Steps**:
- If the current interval overlaps with the last merged one, merge them.
**Example Code**:
```python
if not intervals:
return []
intervals.sort(key=lambda x: x[0])
merged = [intervals[0]]
for current in intervals[1:]:
last_merged = merged[-1]
else:
merged.append(current)
return merged
```
**Steps**:
**Example Code**:
```python
dp = [0] * (W + 1)
for i in range(len(weights)):
return dp[W]
```
**Steps**:
2. During the merge step, count how many elements from the right half are less than elements in the left
half.
**Example Code**:
```python
inv_count = 0
temp_arr[k] = arr[i]
i += 1
else:
temp_arr[k] = arr[j]
j += 1
k += 1
i += 1
k += 1
temp_arr[k] = arr[j]
j += 1
k += 1
arr[i] = temp_arr[i]
return inv_count
inv_count = 0
return inv_count
```
**Steps**:
- Each cell will be the min of the top and left cells plus its own value.
**Example Code**:
```python
if not grid:
return 0
dp[0][0] = grid[0][0]
```
**Steps**:
3. If it is, add it to the current partition and recursively call for the rest of the string.
4. If at the end of the string, add the current partition to the result.
**Example Code**:
```python
return s == s[::-1]
if start == len(s):
result.append(path.copy())
return
+ 1):
if is_palindrome(s[start:end]):
path.append(s[start:end])
path.pop()
def palindrome_partition(s: str) -> List[List[str]]:
result = []
return result
```
**Steps**:
1. Use a stack to keep track of the indices of the bars in the histogram.
- While the stack is not empty and the current height is less than the height of the bar at the index
stored at the top of the stack:
3. After finishing the loop, empty the stack to calculate the remaining areas.
**Example Code**:
```python
stack = []
max_area = 0
for i in range(len(heights)):
max_area = max(max_area, h * w)
stack.append(i)
return max_area
```
These step-by-step solutions should help you understand how to approach each problem systematically.
Let me know if you need further explanations or help with any specific task!