Skip to content

Commit 40cc6f9

Browse files
authored
Merge pull request #1 from iamAntimPal/Daily-Task
Daily task
2 parents 5376283 + 78aec05 commit 40cc6f9

File tree

10 files changed

+100631
-0
lines changed

10 files changed

+100631
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Python Package using Conda
2+
3+
on: [push]
4+
5+
jobs:
6+
build-linux:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
max-parallel: 5
10+
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: Set up Python 3.10
14+
uses: actions/setup-python@v3
15+
with:
16+
python-version: '3.10'
17+
- name: Add conda to system path
18+
run: |
19+
# $CONDA is an environment variable pointing to the root of the miniconda directory
20+
echo $CONDA/bin >> $GITHUB_PATH
21+
- name: Install dependencies
22+
run: |
23+
conda env update --file environment.yml --name base
24+
- name: Lint with flake8
25+
run: |
26+
conda install flake8
27+
# stop the build if there are Python syntax errors or undefined names
28+
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
29+
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
30+
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
31+
- name: Test with pytest
32+
run: |
33+
conda install pytest
34+
pytest
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def mostPoints(self, questions: List[List[int]]) -> int:
3+
@cache
4+
def dfs(i: int) -> int:
5+
if i >= len(questions):
6+
return 0
7+
p, b = questions[i]
8+
return max(p + dfs(i + b + 1), dfs(i + 1))
9+
10+
return dfs(0)
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
2+
3+
# 2140. Solving Questions With Brainpower
4+
5+
## 📌 Problem Statement
6+
You are given a 0-indexed 2D integer array `questions` where `questions[i] = [pointsi, brainpoweri]`.
7+
For each question, you must decide whether to **solve** it or **skip** it:
8+
- **Solve:** Earn `pointsi` points, but you must skip the next `brainpoweri` questions.
9+
- **Skip:** Move to the next question without earning any points.
10+
11+
The goal is to maximize the total points earned by processing the questions in order.
12+
13+
---
14+
15+
## 📊 Table Structure
16+
17+
Although this problem is algorithmic in nature and doesn't use a database, we can imagine a **Questions Table** that represents each question. For demonstration purposes, we’ll show a sample table structure similar to a "Teacher Table":
18+
19+
### Teacher Table (Questions Table)
20+
| QuestionID | Points | Brainpower |
21+
| ---------- | ------ | ---------- |
22+
| 0 | 3 | 2 |
23+
| 1 | 4 | 3 |
24+
| 2 | 4 | 4 |
25+
| 3 | 2 | 5 |
26+
27+
---
28+
29+
## 📊 Example 1
30+
31+
### Input: Teacher Table
32+
| QuestionID | Points | Brainpower |
33+
| ---------- | ------ | ---------- |
34+
| 0 | 3 | 2 |
35+
| 1 | 4 | 3 |
36+
| 2 | 4 | 4 |
37+
| 3 | 2 | 5 |
38+
39+
### Output:
40+
```
41+
5
42+
```
43+
44+
### Explanation:
45+
- **Solve question 0:** Earn 3 points, then skip questions 1 and 2.
46+
- **Solve question 3:** Earn 2 points.
47+
- **Total points:** 3 + 2 = 5.
48+
49+
50+
---
51+
52+
## ✅ Approach: Python (Recursion with Memoization)
53+
We use a recursive depth-first search (DFS) with memoization to decide for each question whether to solve it or skip it.
54+
For each question `i`:
55+
- **Solve:** Add `pointsi` and then jump to question `i + brainpoweri + 1`.
56+
- **Skip:** Move to question `i + 1`.
57+
58+
The recurrence is:
59+
```python
60+
dfs(i) = max(pointsi + dfs(i + brainpoweri + 1), dfs(i + 1))
61+
```
62+
63+
---
64+
65+
## 🐍 Python (Pandas) Solution 1
66+
67+
```python
68+
from functools import cache
69+
from typing import List
70+
71+
class Solution:
72+
def mostPoints(self, questions: List[List[int]]) -> int:
73+
@cache
74+
def dfs(i: int) -> int:
75+
if i >= len(questions):
76+
return 0
77+
p, b = questions[i]
78+
return max(p + dfs(i + b + 1), dfs(i + 1))
79+
80+
return dfs(0)
81+
```
82+
83+
## 🐍 Python (Pandas) Solution 2
84+
85+
```python
86+
class Solution:
87+
def mostPoints(self, questions: List[List[int]]) -> int:
88+
@cache
89+
def dfs(i: int) -> int:
90+
if i >= len(questions):
91+
return 0
92+
p, b = questions[i]
93+
return max(p + dfs(i + b + 1), dfs(i + 1))
94+
95+
return dfs(0)
96+
```
97+
98+
### ✅ Approach Explanation:
99+
- **Recursion with Memoization:** The function `dfs(i)` computes the maximum points achievable starting from the `i`th question.
100+
- **Decision:** For each question, it chooses the maximum between solving the question (adding its points and skipping the next `brainpower` questions) and skipping the question.
101+
- **Efficiency:** The use of caching (`@cache`) ensures each state is computed only once.
102+
103+
---
104+
105+
## 📁 File Structure
106+
107+
```plaintext
108+
.
109+
├── README.md # This file
110+
└── solution.py # Contains the Python solution implementation
111+
```
112+
113+
---
114+
115+
## 🔗 Useful Links
116+
- [LeetCode Problem 2140](https://leetcode.com/problems/solving-questions-with-brainpower/)
117+
- [Dynamic Programming - Wikipedia](https://en.wikipedia.org/wiki/Dynamic_programming)
118+
- [Python functools.cache Documentation](https://docs.python.org/3/library/functools.html#functools.cache)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Apply Operation to maximize Score
2+
import math
3+
from heapq import nlargest
4+
from collections import defaultdict
5+
6+
MOD = 10**9 + 7
7+
8+
def count_prime_factors(n):
9+
"""Returns the number of distinct prime factors of n."""
10+
factors = set()
11+
for i in range(2, int(math.sqrt(n)) + 1):
12+
while n % i == 0:
13+
factors.add(i)
14+
n //= i
15+
if n > 1:
16+
factors.add(n)
17+
return len(factors)
18+
19+
def precompute_prime_scores(limit=10**5):
20+
"""Precomputes the prime scores for numbers from 1 to limit."""
21+
prime_scores = [0] * (limit + 1)
22+
23+
for i in range(2, limit + 1):
24+
if prime_scores[i] == 0: # `i` is a prime
25+
for j in range(i, limit + 1, i):
26+
prime_scores[j] += 1 # Mark multiples of `i`
27+
28+
return prime_scores
29+
30+
def maxScore(nums, k):
31+
"""Computes the maximum score using `k` operations."""
32+
prime_scores = precompute_prime_scores(max(nums))
33+
34+
# Store elements as (prime_score, value, index)
35+
elements = []
36+
37+
for i, num in enumerate(nums):
38+
elements.append((prime_scores[num], num, i))
39+
40+
# Sort by (prime_score descending, value descending, index ascending)
41+
elements.sort(reverse=True, key=lambda x: (x[0], x[1], -x[2]))
42+
43+
# Take `k` highest scoring elements
44+
result = 1
45+
for _, value, _ in elements[:k]:
46+
result = (result * value) % MOD
47+
48+
return result

0 commit comments

Comments
 (0)