question_slug
stringlengths 3
77
| title
stringlengths 1
183
| slug
stringlengths 12
45
| summary
stringlengths 1
160
⌀ | author
stringlengths 2
30
| certification
stringclasses 2
values | created_at
stringdate 2013-10-25 17:32:12
2025-04-12 09:38:24
| updated_at
stringdate 2013-10-25 17:32:12
2025-04-12 09:38:24
| hit_count
int64 0
10.6M
| has_video
bool 2
classes | content
stringlengths 4
576k
| upvotes
int64 0
11.5k
| downvotes
int64 0
358
| tags
stringlengths 2
193
| comments
int64 0
2.56k
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
lemonade-change
|
Simplest O(N) C++ straight forward solution with explanation
|
simplest-on-c-straight-forward-solution-9hkqf
|
If we get 5$ then five++\nif we get 10$ then do five--\nif we get 20$ {\n if(ten) ten--,five--;\n\t else five -= 3;\n\t }\n\t lastly check if five < 0\n
|
NextThread
|
NORMAL
|
2022-01-25T11:10:41.438659+00:00
|
2022-01-25T11:11:16.695054+00:00
| 481 | false |
If we get 5$ then five++\nif we get 10$ then do five--\nif we get 20$ {\n if(ten) ten--,five--;\n\t else five -= 3;\n\t }\n\t lastly check if five < 0\n\t \n```\nclass Solution {\npublic:\n bool lemonadeChange(vector<int>& bills) {\n int five = 0;\n int ten = 0;\n for(int i = 0 ; i < bills.size() ; i++){\n if(bills[i] == 5) five++;\n if(bills[i] == 10) ten++,five--;\n if(bills[i] == 20) {\n if(ten)ten--,five--;\n else five -= 3;\n }\n if(five < 0) return false;\n }\n return true;\n }\n};\n```
| 3 | 0 |
['C']
| 1 |
lemonade-change
|
C++| EASY TO UNDERSTAND solution| Intuitive approach
|
c-easy-to-understand-solution-intuitive-486kt
|
Please upvote to motivate me in my quest of documenting all leetcode solutions. HAPPY CODING:)\nclass Solution {\npublic:\n bool lemonadeChange(vector& bills
|
aarindey
|
NORMAL
|
2021-05-31T20:41:16.578254+00:00
|
2021-05-31T20:41:33.430988+00:00
| 233 | false |
**Please upvote to motivate me in my quest of documenting all leetcode solutions. HAPPY CODING:)**\nclass Solution {\npublic:\n bool lemonadeChange(vector<int>& bills) {\n unordered_map<int,int> mp; \n for(int i=0;i<bills.size();i++)\n {\n if(bills[i]==5)\n {\n mp[5]++;\n } \n if(bills[i]==10)\n {\n if(mp[5]==0)\n return false;\n else\n {\n mp[10]++;\n mp[5]--;\n }\n }\n if(bills[i]==20)\n {\n if(mp[5]==0||(mp[5]<=2)&&(mp[10]==0))\n return false;\n else\n {\n if(mp[5]>0&&mp[10]>0)\n {\n mp[10]--;\n mp[5]--;\n mp[20]++;\n }\n else if(mp[5]>=3)\n {\n mp[20]++;\n mp[5]-=3;\n }\n }\n }\n }\n return true;\n }\n};
| 3 | 0 |
['Greedy', 'C']
| 0 |
lemonade-change
|
[JAVA] Straight forward | Greedy
|
java-straight-forward-greedy-by-redeyedb-dx3o
|
The reason this problem is classified under greedy is we choose to return the maximum denomination everytime we return the change to a customer.\n2. We return m
|
redeyedbeast
|
NORMAL
|
2020-06-15T21:10:30.035251+00:00
|
2020-06-15T21:10:30.035287+00:00
| 371 | false |
1. The reason this problem is classified under greedy is we choose to return the maximum denomination everytime we return the change to a customer.\n2. We return maximum denomination because we can use smaller changes for future and we can build a bigger sum of change using the smaller denomination.\n3. My solution includes tracking the 20 denomination, but you never will be using it because you never return back a 20 denomination to the customer. You will either return a 10, 5 or the combination of both. But I have included 20 denomination for making the code more understandable for readers.\n\n\n```\nclass Solution {\n public boolean lemonadeChange(int[] bills) {\n int five = 0;\n int ten = 0;\n int twenty = 0;\n \n for(int i = 0; i < bills.length; i++){\n if(bills[i] == 5) { five++; bills[i] -= 5;}\n if(bills[i] == 10) { ten++; bills[i] -= 5; }\n if(bills[i] == 20) { twenty++; bills[i] -= 5; }\n \n if(bills[i] > 0){\n while(bills[i] >= 20 && twenty > 0){\n twenty--;\n bills[i] -= 20;\n }\n \n while(bills[i] >= 10 && ten > 0){\n ten--;\n bills[i] -= 10;\n }\n \n while(bills[i] >= 5 && five > 0){\n five--;\n bills[i] -= 5;\n }\n \n if(bills[i] > 0) return false;\n } \n }\n \n return true;\n }\n}\n```
| 3 | 0 |
['Greedy', 'Java']
| 0 |
lemonade-change
|
Java - concise code
|
java-concise-code-by-tianchi-seattle-vu45
|
java\nclass Solution {\n \n public boolean lemonadeChange(int[] bills) {\n int five = 0, ten = 0;\n \n for (int bill : bills) {\n
|
tianchi-seattle
|
NORMAL
|
2019-04-29T06:29:02.633729+00:00
|
2019-04-29T06:29:02.633773+00:00
| 474 | false |
```java\nclass Solution {\n \n public boolean lemonadeChange(int[] bills) {\n int five = 0, ten = 0;\n \n for (int bill : bills) {\n if (bill == 5) five++;\n else if (bill == 10) {five--; ten++;}\n else if (bill == 20 && ten > 0) {ten--; five--;}\n else five -= 3;\n \n if (five < 0) return false;\n }\n return true;\n }\n}\n```
| 3 | 0 |
[]
| 0 |
lemonade-change
|
Python Solution using Queue
|
python-solution-using-queue-by-yuanzhu5-patq
|
\'\'\'\n\n def lemonadeChange(self, bills):\n \n q = collections.deque()\n for bill in bills:\n if bill == 5:\n q.
|
yuanzhu5
|
NORMAL
|
2019-04-19T19:37:39.996818+00:00
|
2019-04-19T19:37:39.996892+00:00
| 654 | false |
\'\'\'\n\n def lemonadeChange(self, bills):\n \n q = collections.deque()\n for bill in bills:\n if bill == 5:\n q.appendleft(bill)\n if bill == 10:\n if not q: return False\n if q[0] == 5:\n q.popleft()\n q.append(bill)\n else:\n return False\n if bill == 20:\n if not q: return False\n while bill > 10:\n if not q: return False\n bill -= q.pop()\n if not q: return False\n if bill > 5:\n bill -= q.popleft()\n if bill != 5: return False\n return True\n\t\t\n\'\'\'\nExplanation: Append 5 to the left, append 10 to the right. Chose where to pop. We don\'t have to append 20. Just for fun. : )
| 3 | 0 |
[]
| 0 |
lemonade-change
|
easiest solution -beats 100%
|
easiest-solution-beats-100-by-apoorvashu-rqjp
|
Complexity
Time complexity:
O(n)
Space complexity:
O(1)
Code
|
apoorvashukla744
|
NORMAL
|
2025-03-23T15:34:41.168338+00:00
|
2025-03-23T15:34:41.168338+00:00
| 147 | false |
# Complexity
- Time complexity:
O(n)
- Space complexity:
O(1)
# Code
```cpp []
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
int cnt5=0,cnt10=0;
for(int i=0;i<bills.size();i++){
if(bills[i]==5){
cnt5++;
}else if(bills[i]==10){
if(cnt5==0){
return false;
}else{
cnt5--;
}
cnt10++;
}else if(bills[i]==20){
if(cnt5>0 && cnt10>0){
cnt5--;
cnt10--;
}else if(cnt5>=3){
cnt5-=3;
}else{
return false;
}
}
}
return true;
}
};
```
| 2 | 0 |
['Array', 'Greedy', 'C++']
| 0 |
lemonade-change
|
✅✅ Just do with basic maths || Beats 100% || Easy Solution || C++ Solution
|
just-do-with-basic-maths-easy-solution-c-8wuw
|
Approach
Start by checking if the first bill is greater than 5; if so, return false since change cannot be provided for the first customer.
Use two variables, f
|
Karan_Aggarwal
|
NORMAL
|
2025-01-19T17:15:28.228897+00:00
|
2025-01-19T17:15:54.485910+00:00
| 5 | false |
# Approach
1. Start by checking if the first bill is greater than 5; if so, return `false` since change cannot be provided for the first customer.
2. Use two variables, `five` and `ten`, to track the count of $5 and $10 bills respectively.
3. Iterate through the `bills` array:
- If the bill is 5, increment the `five` counter.
- If the bill is 10:
- Check if at least one 5 bill is available; if yes, decrement the `five` counter and increment the `ten` counter.
- Otherwise, return `false` as no change can be provided.
- If the bill is 20:
- First, try to give one 10 and one 5 as change if possible.
- If not, check if three 5 bills can be used as change.
- If neither option is available, return `false`.
4. If all bills are processed successfully, return `true`.
# Intuition
The problem revolves around managing change efficiently. Prioritize using higher denomination bills ($10) for change first, as this conserves $5 bills, which are more versatile for future transactions. This greedy approach ensures the optimal handling of changes to maximize flexibility.
# Time Complexity
- **O(n)**: We iterate through the `bills` array once, where `n` is the number of customers.
# Space Complexity
- **O(1)**: Only two integer variables, `five` and `ten`, are used for tracking the count of 5 and 10 bills.
# Code
```cpp []
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
if(bills[0]>5) return false;
int five=0,ten=0;
for(int i:bills){
if(i==5) five++;
else if(i==10){
if(five) five--,ten++;
else return false;
}
else{
if(five && ten) five--,ten--;
else if(five>=3) five -= 3;
else return false;
}
}
return true;
}
};
```
# Have a good day 😊 Upvote?
| 2 | 0 |
['Array', 'Greedy', 'C++']
| 0 |
lemonade-change
|
EASY TO UNDERSTAND JAVA SOLUTION BEATING 94.52% in 2ms
|
easy-to-understand-java-solution-beating-nk3e
|
Complexity
Time complexity: O(n)
Space complexity: O(1)
Code
|
arshi_bansal
|
NORMAL
|
2025-01-18T13:01:14.941045+00:00
|
2025-01-18T13:01:14.941045+00:00
| 210 | false |

# Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public boolean lemonadeChange(int[] bills) {
int n=bills.length;
int five=0,ten=0,twenty=0;
for(int i=0;i<n;i++){
if(bills[i]==5){
five+=1;
}
if(bills[i]==10){
ten+=1;
if(five>0){
five-=1;
}else{
return false;
}
}
if(bills[i]==20){
twenty+=1;
if(five>=1 && ten>=1){
five-=1;
ten-=1;
}else if(five>=3){
five-=3;
}else{
return false;
}
}
}
return true;
}
}
```
| 2 | 0 |
['Array', 'Greedy', 'Java']
| 0 |
lemonade-change
|
USING unordered_map | SIMPLE SOLUTION | EASY EXPLANATION
|
using-unordered_map-simple-solution-easy-ujzx
|
IntuitionKeep track of bills and see if you have enough change to give cutomers.Complexity
Time complexity: O(n)
Space complexity: O(1)
Approach
Initialize a d
|
arpita_sat
|
NORMAL
|
2025-01-07T17:22:08.684414+00:00
|
2025-01-07T17:22:08.684414+00:00
| 111 | false |
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Keep track of bills and see if you have enough change to give cutomers.
# Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Approach
<!-- Describe your approach to solving the problem. -->
- Initialize a dictionary to track the number of 5, 10, and 20 dollar bills on hand.
- Then iterate through the list of bills received, updating the dictionary accordingly.
- For each 10 bill, checks if there is at least one 5 bill to provide change.
- For each 20 bill, checks if there is either one 10 bill and one $5 bill, or at least three 5 bills, to provide change.
- If at any point the cashier cannot provide change for a customer, the function returns False; otherwise, it returns True.
# Code
```cpp []
class Solution {
public:
bool lemonadeChange(vector<int>& bills) {
unordered_map <int,int> umap;
for(int i=0; i<bills.size(); i++)
{
if(bills[i] == 5)
umap[5]++;
else if(bills[i] == 10)
{
umap[10]++;
if(umap[5] == 0) return false;
umap[5]--;
}
else
{
umap[20]++;
if(umap[10] > 0 && umap[5] > 0)
{ umap[10]--, umap[5]--; }
else if(umap[5] > 2) umap[5] -=3;
else return false;
}
}
return true;
}
};
```
| 2 | 0 |
['C++']
| 0 |
lemonade-change
|
Beginner Friendly || Greedy Approach || Explanation ||
|
beginner-friendly-greedy-approach-explan-ph4m
|
Intuition\nThe goal is to determine if we can provide correct change to every customer. We start with no money and can only make change with the bills we receiv
|
Anurag_Basuri
|
NORMAL
|
2024-09-24T13:02:07.513530+00:00
|
2024-09-24T13:02:07.513557+00:00
| 299 | false |
# Intuition\nThe goal is to determine if we can provide correct change to every customer. We start with no money and can only make change with the bills we receive. If a customer pays with a $5$ bill, no change is needed. If a customer pays with a $10$ bill, we need to give back $5$ as change. If a customer pays with a $20$ bill, we need to give $15$ back, preferably using one $10$ and one $5$ bill or three $5$ bills.\n\nThe key intuition is that when giving change, we should prioritize using the larger bills first. This is because larger bills will be more useful in future transactions that may require smaller bills as change.\n\n# Approach\n1. Initialize two variables `five` and `ten` to track the number of $5$ and $10$ bills we have.\n2. Iterate over the `bills` array:\n - If the bill is $5$, increment the `five` counter since no change is required.\n - If the bill is $10$, decrement the `five` counter by 1 to give $5$ as change and increment the `ten` counter.\n - If the bill is $20$, check if we can give one $10$ and one $5$ as change (preferred). If not, check if we can give three $5$ bills. If neither is possible, return `false`.\n3. If we successfully process all customers, return `true`.\n\n# Complexity\n- **Time complexity**: \n The time complexity is $$O(n)$$ where `n` is the number of bills in the input list. We process each bill exactly once in a single loop.\n\n- **Space complexity**: \n The space complexity is $$O(1)$$ since we are using only a fixed amount of extra space for the `five` and `ten` variables, regardless of the size of the input.\n \n\n# Code\n``` cpp []\nclass Solution {\npublic:\n bool lemonadeChange(vector<int>& bills) {\n unordered_map<int, int> track;\n \n for(int i = 0; i < bills.size(); i++){\n if(bills[i] == 5) track[5]++;\n else if(bills[i] == 10){\n track[10]++;\n if(track[5]) track[5]--;\n else return false;\n }\n else{\n if(track[10] && track[5]){\n track[10]--;\n track[5]--;\n }\n else if(track[5] >= 3){\n track[5] -= 3;\n }\n else return false;\n }\n }\n return true;\n }\n};\n```\n``` java []\nclass Solution {\n public boolean lemonadeChange(int[] bills) {\n int five = 0, ten = 0;\n\n for (int bill : bills) {\n if (bill == 5) {\n five++;\n } \n else if (bill == 10) {\n if (five == 0) return false;\n five--;\n ten++;\n } \n else {\n if (ten > 0 && five > 0) {\n ten--;\n five--;\n } \n else if (five >= 3) {\n five -= 3;\n } \n else {\n return false;\n }\n }\n }\n return true;\n }\n}\n```\n```python3 []\nclass Solution:\n def lemonadeChange(self, bills: List[int]) -> bool:\n five, ten = 0, 0\n \n for bill in bills:\n if bill == 5:\n five += 1\n elif bill == 10:\n if five == 0:\n return False\n five -= 1\n ten += 1\n else:\n if ten > 0 and five > 0:\n ten -= 1\n five -= 1\n elif five >= 3:\n five -= 3\n else:\n return False\n \n return True\n\n```
| 2 | 0 |
['Array', 'Greedy', 'C++', 'Java', 'Python3']
| 0 |
lemonade-change
|
lemonde easy solution
|
lemonde-easy-solution-by-manas_mittal-yf90
|
\n# Complexity\n- Time complexity:O(n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:0(1)\n Add your space complexity here, e.g. O(n) \n\n#
|
Manas_Mittal_
|
NORMAL
|
2024-08-26T12:48:23.166568+00:00
|
2024-08-26T12:48:23.166598+00:00
| 256 | false |
\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:0(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\nclass Solution:\n def lemonadeChange(self, bills: List[int]) -> bool:\n if bills[0] != 5:\n return False\n five =0 \n ten = 0 \n for i in bills:\n if i== 5:\n five+=1\n elif i == 10:\n if five > 0:\n five -=1\n else:\n return False\n ten+=1\n else:\n if five > 0 and ten >0 :\n five -=1\n ten -=1\n elif five > 2:\n five-=3\n else:\n return False\n return True\n```\n```java []\npublic class Solution {\n public boolean lemonadeChange(List<Integer> bills) {\n int five = 0;\n int ten = 0;\n\n for (int bill : bills) {\n if (bill == 5) {\n five++;\n } else if (bill == 10) {\n if (five > 0) {\n five--;\n } else {\n return false;\n }\n ten++;\n } else { // bill == 20\n if (five > 0 && ten > 0) {\n five--;\n ten--;\n } else if (five >= 3) {\n five -= 3;\n } else {\n return false;\n }\n }\n }\n return true;\n }\n}\n\n```
| 2 | 0 |
['Array', 'Greedy', 'Java', 'Python3']
| 0 |
lemonade-change
|
Using Map in C++ with a single for loop
|
using-map-in-c-with-a-single-for-loop-by-xm26
|
Intuition\n Describe your first thoughts on how to solve this problem. \nImagine, you have a shop of lemonade costs 5.\nIn the beginning of the day, you have on
|
sa_hilll94
|
NORMAL
|
2024-08-16T13:48:54.474503+00:00
|
2024-08-16T13:48:54.474530+00:00
| 13 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nImagine, you have a shop of lemonade costs 5.\nIn the beginning of the day, you have only lemonade NO MONEY in your drawer of shop(it is written in the question).\nNow, the first customer comes, he gives you 5 and in return you give him the lemonade. (Drawer is having 5)\nThen, second customer comes up with 10 and you served him a lemonade and then in return you gave him 5 as he is having 10 and the cost of a lemonade is 5 (here, you will check whether you have one 5 dollar note or not, if not then return false).\nThen a customers comes up with 20 dollar and you served him a lemonade which cost 5 dollar. Now think, you have 20 dollar and return for your betterment you will give him 5 dollar\'s 3 notes or one 10 dollar and one 5 dollar note? You will give him one 10 and 5 dollar note if you have and if you don\'t have then three 5 dollar notes.\nThat\'s it, now your business will run on the sky. LOL.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nFirst use a map to store the key as 5,10,20 dollar and increment or decrement it\'s value as per the condition.\nFirst check, if the customer have dollar 5, if yes increment the value of key 5 in the map.\nSecond, check if the customer have 10 dollar note, if yes then increment the value of key 10 and decrement the value of key 5 as you will give him back Dollar 5 return of 10 dollar for the lemonade.\nThird, check if the customer have 15 dollar note, if he is having 20 dollar then first check whether you have the value of key 5 and key 10 more than 0 then only increment the value of key 20 and decrement the value of key 5 and key 10 in exchange of 20 dollar with 10 and 5 dollar for the lemonade.\nIf all the conditions fails then return false. \n\n# Complexity\n- Time complexity: $$0(n)$$ \n **Only one for loop is used.**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$ O(1) $$ \n **constant space is used.**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n bool lemonadeChange(vector<int>& bills) {\n\n map<int,int>KeeperMoney;\n\n // For $5, created a key with 0 value;\n KeeperMoney[5]=0;\n\n // For $10, created a key with 0 value;\n KeeperMoney[10]=0;\n\n // For $20, created a key with 0 value;\n KeeperMoney[20]=0;\n\n // Now traverse over the array and increment decrement as per the requirement;\n for(int i=0;i<bills.size();i++)\n {\n // First, we added $5 when the first customer came;\n if(bills[i]==5){\n KeeperMoney[5]++;\n }\n // Now, if the customer comes up with $10, then we will return them $5 as exchange.\n else if(bills[i]==10 && KeeperMoney[5]>0){\n // tells that we have given $5 back to the customer;\n KeeperMoney[5]--;\n // tells that we received $10 from the customer in exchange of $5;\n KeeperMoney[10]++;\n }\n // Now, if the customer comes up with $20, then first check to give $5 and $10 together and then try to give $5 3 notes;\n else if(bills[i]==20){\n if(KeeperMoney[5]>0 && KeeperMoney[10]>0){\n // decrements $5 and $10;\n KeeperMoney[5]--;\n KeeperMoney[10]--;\n // Increment $20;\n KeeperMoney[20]++;\n }\n else if(KeeperMoney[5]>2){\n // Check if $5 is more than 2 then give $5\'s 3 notes to the customer\n KeeperMoney[5] -= 3;\n // and stores $20 notes;\n KeeperMoney[20]++;\n }\n else{\n return false;\n }\n }\n else{\n return false;;\n }\n }\n\n return true;\n\n }\n};\n```\n\n\n# ***Kindly Upvote this solution if like the explanation.***
| 2 | 0 |
['Array', 'Ordered Map', 'C++']
| 0 |
lemonade-change
|
✅Very Easy single loop Code✅. Check it out🫡.
|
very-easy-single-loop-code-check-it-out-689n5
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
|
ASHWANI_1404
|
NORMAL
|
2024-08-16T00:06:30.773399+00:00
|
2024-08-16T00:06:30.773438+00:00
| 6 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n bool lemonadeChange(vector<int>& bills) {\n int five=0, ten=0;\n for(int i=0;i<bills.size();i++)\n {\n if(bills[i]==5)\n five++;\n else if(bills[i]==10)\n {\n if(five>0)\n {\n five--;\n ten++;\n }\n else\n return false;\n }\n else\n {\n if(five>0 && ten>0)\n {\n five--;\n ten--;\n }\n else if(five>=3)\n {\n five=five-3;\n }\n else\n return false;\n }\n }\n return true;\n \n }\n};\n```
| 2 | 1 |
['Array', 'Greedy', 'C++']
| 0 |
lemonade-change
|
Easy and Simple Solution (for beginners)
|
easy-and-simple-solution-for-beginners-b-hhy7
|
# Intuition \n\n\n\n\n\n\n\n\n\n\n\n\n# Code\n\nclass Solution {\npublic:\n bool lemonadeChange(vector<int>& nums) {\n if(nums[0]!=5){\n
|
lakshaykamboj309
|
NORMAL
|
2024-08-15T13:59:30.185210+00:00
|
2024-08-15T13:59:30.185271+00:00
| 84 | false |
<!-- # Intuition -->\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n<!-- # Approach -->\n<!-- Describe your approach to solving the problem. -->\n\n<!-- # Complexity -->\n<!-- - Time complexity: -->\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n<!-- - Space complexity: -->\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n bool lemonadeChange(vector<int>& nums) {\n if(nums[0]!=5){\n return false;\n }\n int count_5=0,count_10=0;\n for(int i=0;i<nums.size();i++){\n if(nums[i]==5){\n count_5++;\n }\n else if(nums[i]==10){\n if(count_5>=1){\n count_5--;\n count_10++;\n }\n else{\n return false;\n }\n }\n else if(nums[i]==20){\n if(count_5>=1 && count_10>=1){\n count_5--;\n count_10--;\n }\n else if(count_5>=3){\n count_5=count_5-3;\n }\n else{\n return false;\n }\n }\n }\n return true;\n }\n};\n```
| 2 | 0 |
['C++']
| 0 |
lemonade-change
|
Brute Force
|
brute-force-by-karthipannerselvam-kemp
|
\n\n# Code\n\nclass Solution {\npublic:\n bool lemonadeChange(vector<int>& bills) {\n int n=bills.size();\n int five=0,ten=0,twenty=0;\n
|
karthipannerselvam
|
NORMAL
|
2024-08-15T13:55:50.871403+00:00
|
2024-08-15T13:55:50.871446+00:00
| 78 | false |
\n\n# Code\n```\nclass Solution {\npublic:\n bool lemonadeChange(vector<int>& bills) {\n int n=bills.size();\n int five=0,ten=0,twenty=0;\n for(int i=0;i<n;i++){\n if(bills[i]==5){\n five+=5;\n }\n if(bills[i]==10){\n ten+=10;\n if(five>=5){\n five-=5;\n }\n else{\n return false;\n }\n }\n if(bills[i]==20){\n twenty+=20;\n if(ten>=10 && five>=5){\n ten-=10;\n five-=5;\n }\n else if(five>=15){\n five-=15;\n }\n else{\n return false;\n }\n \n }\n }\n return true;\n }\n};\n```
| 2 | 0 |
['C++']
| 0 |
lemonade-change
|
Easiest Approach || T.C: O(n) || S.C: O(n) || Beats 100%
|
easiest-approach-tc-on-sc-on-beats-100-b-q7sv
|
Intuition\n Describe your first thoughts on how to solve this problem. \n- The core of this problem is to manage the change you have efficiently. Since customer
|
prayashbhuria931
|
NORMAL
|
2024-08-15T13:36:41.112665+00:00
|
2024-08-15T13:36:41.112690+00:00
| 11 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- The core of this problem is to manage the change you have efficiently. Since customers can only pay with Dollar 5, Dollar 10,or Dollar 20 bills, you need to ensure that you always have enough smaller bills on hand to provide the necessary change.\n\n1. **Dollar 5 Bill**: When a customer pays with a Dollar 5 bill, you simply increase your count of Dollar 5 bills. No change is required.\n\n2. **Dollar 10 Bill**: If a customer pays with a Dollar 10 bill, you need to give them Dollar 5 as change. Therefore, you decrease the count of Dollar 5 bills and increase the count of Dollar 10 bills. If you don\'t have a Dollar 5 bill to give as change, you can\'t proceed, and the answer is false.\n\n3. **Dollar 20 Bill**: If a customer pays with a Dollar 20 bill, you can give change in two ways:\n\n- Preferably, give one Dollar 10 bill and one Dollar 5 bill as change. This way, you maximize the higher denomination bills for future transactions.\n- If you don\'t have a Dollar 10 bill, you must give three Dollar 5 bills as change. If neither option is possible, the answer is false.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Track the Count of Dollar 5 and Dollar 10 Bills**: We use two variables, fivenote and tennote, to keep track of the number of Dollar 5 and Dollar 10 bills we have at any point.\n2. **Iterate Through Each Bill**: For each bill in the bills array:\n - If it\'s a Dollar 5 bill, increment fivenote.\n - If it\'s a Dollar 10 bill, check if you have a Dollar 5 bill to give as change. If so, decrement fivenote and increment tennote. If not, return false.\n - If it\'s a Dollar 20 bill, try to give one Dollar 10 bill and one Dollar 5 bill as change. If not possible, try giving three Dollar 5 bills. If neither is possible, return false.\n3. **Return True**: If you\'ve successfully provided change for all customers, return true.\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n bool lemonadeChange(vector<int>& bills) {\n\n int fivenote=0;\n int tennote=0;\n\n for(auto bill:bills)\n {\n if(bill==5) fivenote++;\n if(bill==10)\n {\n if(fivenote>=1){\n fivenote--;\n tennote++;\n }\n else return false;\n }\n if(bill==20)\n {\n if(tennote>=1 && fivenote>=1)\n {\n tennote--;\n fivenote--;\n }else if(fivenote>=3) fivenote=fivenote-3;\n else return false;\n }\n }\n return true;\n }\n};\n```\n\n\n
| 2 | 0 |
['Array', 'Greedy', 'C++']
| 0 |
lemonade-change
|
Very Easy Solution Sove And Understand in 2 mins beats 99% in T.C.
|
very-easy-solution-sove-and-understand-i-xj8k
|
\n# Code\n\nclass Solution {\n public boolean lemonadeChange(int[] bills) {\n int five=0,ten=0;\n for(int i:bills)\n {\n if(i
|
vansh0123
|
NORMAL
|
2024-08-15T11:56:41.955491+00:00
|
2024-08-15T11:56:41.955514+00:00
| 140 | false |
\n# Code\n```\nclass Solution {\n public boolean lemonadeChange(int[] bills) {\n int five=0,ten=0;\n for(int i:bills)\n {\n if(i==5)\n five++;\n else if(i==10)\n {\n if(five > 0)\n {\n ten++;\n five--;\n }\n else \n return false;\n }\n else\n {\n if(five>0&&ten>0)\n {\n five--;\n ten--;\n }\n else if(five>2)\n {\n five-=3;\n\n }\n else \n return false;\n\n }\n }\n return true;\n \n }\n}\n```
| 2 | 0 |
['Java']
| 0 |
lemonade-change
|
Simple If-Else conditions
|
simple-if-else-conditions-by-vansh_ag-qm7c
|
\n# Code\n\nclass Solution {\n public boolean lemonadeChange(int[] bills) {\n int f=0,ten=0;\n\n for(int i=0; i<bills.length; i++){\n
|
vansh_ag
|
NORMAL
|
2024-08-15T10:47:22.828741+00:00
|
2024-08-15T10:47:22.828771+00:00
| 13 | false |
\n# Code\n```\nclass Solution {\n public boolean lemonadeChange(int[] bills) {\n int f=0,ten=0;\n\n for(int i=0; i<bills.length; i++){\n if(bills[i]==5) f++;\n else if(bills[i]==10) {\n if(f>0) {f--; ten++;}\n else return false;\n }\n else{\n if(f>0 && ten>0) {f--; ten--;}\n else if(f>2) f-=3;\n else return false;\n }\n }\n return true;\n }\n}\n```
| 2 | 0 |
['Java']
| 2 |
lemonade-change
|
beats 100% users..........
|
beats-100-users-by-rudramanikanta02-0z0v
|
Intuition\nwe need to count the five ten for giving change no need of 20 then if count of the change decrease by less than zero return false else true\n\n# Code
|
rudramanikanta02
|
NORMAL
|
2024-08-15T10:39:24.558894+00:00
|
2024-08-15T10:39:24.558914+00:00
| 87 | false |
# Intuition\nwe need to count the five ten for giving change no need of 20 then if count of the change decrease by less than zero return false else true\n\n# Code\n```\nclass Solution {\n public boolean lemonadeChange(int[] bills) {\n int five = 0, ten = 0;\n \n for (int bill:bills) {\n if (bill == 5) {\n five++;\n } else if (bill == 10) {\n ten++;\n five--;\n } else {\n if (ten > 0) {\n ten--; \n } else {\n five -= 2;\n }\n\n five--;\n }\n\n if (five < 0) return false;\n }\n return true;\n }\n}\n```
| 2 | 0 |
['Java']
| 0 |
lemonade-change
|
goalang easy solution
|
goalang-easy-solution-by-zarifjorayev-tj8g
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
|
zarifjorayev
|
NORMAL
|
2024-08-15T08:26:47.151530+00:00
|
2024-08-15T08:26:47.151560+00:00
| 89 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nfunc lemonadeChange(bills []int) bool {\n five, ten := 0, 0\n\n for _, val := range bills {\n if val == 5 {\n five++\n } else if val == 10 {\n if five <= 0 {\n return false\n }\n five--\n ten++\n } else if val == 20 {\n if ten > 0 {\n if five <= 0 {\n return false\n }\n ten--\n five--\n } else if five >= 3 {\n five -= 3\n } else {\n return false\n }\n }\n\n if five < 0 {\n return false\n }\n }\n\n return true\n}\n\n```
| 2 | 0 |
['Go']
| 1 |
lemonade-change
|
Java Solution for beginners | Greedy approach
|
java-solution-for-beginners-greedy-appro-yybj
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
|
btwitsrohit
|
NORMAL
|
2024-08-15T07:06:58.650602+00:00
|
2024-08-15T07:06:58.650636+00:00
| 45 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public boolean lemonadeChange(int[] bills) {\n int fiveDollar = 0;\n int tenDollar = 0;\n\n for(int bill : bills){\n if(bill == 5){\n fiveDollar++;\n }\n else if(bill == 10){\n if(fiveDollar>0){\n fiveDollar--;\n tenDollar++;\n }else{\n return false;\n }\n }else{\n if(tenDollar>0 && fiveDollar>0){\n fiveDollar--;\n tenDollar--;\n }\n else if(fiveDollar>=3){\n fiveDollar-=3;\n }else{\n return false;\n }\n }\n }\n return true;\n }\n}\n```
| 2 | 0 |
['Java']
| 0 |
lemonade-change
|
efficient and easy-to-understand solution.
|
efficient-and-easy-to-understand-solutio-ekbl
|
Complexity\n- Time complexity:O(n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(1)\n Add your space complexity here, e.g. O(n) \n\n\n\n#
|
NehaSinghal032415
|
NORMAL
|
2024-08-15T07:06:08.065442+00:00
|
2024-08-15T07:06:08.065485+00:00
| 82 | false |
# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n\n\n# Code\n```\nclass Solution {\n public boolean lemonadeChange(int[] bills) {\n //first we will take two variables which will track number of five and number ten\n int totalFive=0;\n int totalTen=0;\n for(int bill:bills){\n if(bill==20 && (totalFive>=3||totalFive>=1&&totalTen>=1)){\n if(totalFive>=1&&totalTen>=1){\n totalFive=totalFive-1;\n totalTen=totalTen-1;\n }\n else{\n totalFive=totalFive-3;\n }\n }\n else if(bill==10&& totalFive>=1){\n totalFive=totalFive-1;\n totalTen=totalTen+1;\n }\n else if(bill==5){\n totalFive=totalFive+1;\n }\n else return false;\n }\n return true;\n }\n}\n```
| 2 | 0 |
['Array', 'Java']
| 0 |
lemonade-change
|
C++ || Easy Video Solution || Faster than 98%
|
c-easy-video-solution-faster-than-98-by-p3pfk
|
The video solution for the below code is\nhttps://youtu.be/LCmr0JJUORU\n\n# Code\n\nclass Solution {\npublic:\n bool lemonadeChange(vector<int>& bills) {\n
|
__SAI__NIVAS__
|
NORMAL
|
2024-08-15T05:52:32.798670+00:00
|
2024-08-15T05:52:32.798691+00:00
| 101 | false |
The video solution for the below code is\nhttps://youtu.be/LCmr0JJUORU\n\n# Code\n```\nclass Solution {\npublic:\n bool lemonadeChange(vector<int>& bills) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n // Note\n // Each lemonade costs only 5$\n // Each customer gets only one lemonade\n // Each customer gives either 5$, 10$ or 20$ notes\n // We need to provide change to each and every customer.\n // 5 5 5 10 20\n // First served\n // Second served\n // Third served\n // (3 5$ coins) 10 -> 5$ (2 5$, 1 10$)\n // Fourth custoemr --> (1 10$ and 1 5$) --> Served --> (1 5$, 1 20$) with us that will add up to 25$\n // Give them either 3 5$ as a change\n // Give them 1 5$ and 1 10$ as a change\n\n int count5 = 0;\n int count10 = 0;\n int count20 = 0;\n for(auto &ele : bills){\n if(ele == 5){ count5 += 1; }\n else if(ele == 10) {\n if(count5 >= 1){\n count10 += 1;\n count5 -= 1;\n } \n else return false;\n }\n else{\n if(count5 >= 1 and count10 >= 1){\n count5 -= 1;\n count10 -= 1;\n count20 += 1;\n }\n else if(count5 >= 3){\n count5 -= 3;\n count20 += 1;\n }\n else return false;\n }\n }\n return true;\n }\n};\n```
| 2 | 0 |
['C++']
| 0 |
lemonade-change
|
💯✅Simple and effective solution | Python, C++ and Java Solution | Upvote pliss✅💯
|
simple-and-effective-solution-python-c-a-9qdv
|
Result (Python):\n\n\n# Result (C++):\n\n# Result (Java):\n\n\n# \uD83D\uDCCB Problem Description:\n\nThe lemonadeChange function is designed to determine if it
|
Hamza_the_codar_786
|
NORMAL
|
2024-08-15T05:26:27.200007+00:00
|
2024-08-15T05:27:11.902617+00:00
| 78 | false |
# Result (Python):\n\n\n# Result (C++):\n\n# Result (Java):\n\n\n# \uD83D\uDCCB Problem Description:\n\nThe lemonadeChange function is designed to determine if it\'s possible to provide correct change to every customer in a line at a lemonade stand. Customers pay with $$5$$, $$10$$, or $$20$$ dollar bills, and the goal is to check if you can give the correct change back using the money you\'ve collected so far, assuming you start with no money.\n\n# \uD83E\uDDE9 Code Explanation:\n\n1. **Main Function: lemonadeChange:**\n``` Python []\ndef lemonadeChange(self, bills):\n fives, tens = 0, 0\n```\n\n- Variables Initialization:\n - fives keeps track of the number of $$5$$ dollar bills collected.\n - tens keeps track of the number of $$10$$ dollar bills collected.\n\n2. **Loop Through Customer Payments:**\n``` Python []\nfor i in bills:\n if i == 5:\n fives += 1\n```\n\n- Payment of 5 dollar:\n - If a customer pays with a 5 dollar bill, no change is required, so you simply increase the count of fives by one.\n``` Python []\n elif i == 10:\n if fives == 0:\n return False\n fives -= 1\n tens += 1\n```\n\n- **Payment of 10 dollar**:\n - If a customer pays with a $$10$$ dollar bill, you need to give back $$5$$ dollar in change.\n - **Check Availability:** If there are no $$5$$ dollar bills available (`fives` == $$0$$), you cannot provide the correct change, so the function returns **False**.\n - **Give Change:** If you have a $$5$$ dollar bill, you decrease the count of fives by one and increase the count of tens by one (since you now have a $$10$$ dollar bill).\n``` Python []\n else:\n if tens > 0 and fives > 0:\n tens -= 1\n fives -= 1\n elif fives >= 3:\n fives -= 3\n else:\n return False\n```\n\n- **Payment of $$20$$ dollar:**\n - If a customer pays with a $$20$$ dollar bill, you need to give back $$15$$ dollar in change.\n - **Option 1:** If you have both a $$10$$ dollar bill and a $$5$$ dollar bill, you give one of each as change. This is the preferred method because it conserves more $$5$$ dollar bills.\n - **Option 2:** If you don\u2019t have a $$10$$ dollar bill but have at least three $$5$$ dollar bills, you give three $$5$$ dollar bills as change.\n - **Fail Condition:** If neither option is available, you cannot provide the correct change, so the function returns False.\n\n3. **Return Statement:**\n``` Python []\nreturn True\n```\n- **Final Output:**\n - If you successfully provide the correct change to every customer in the line, the function returns True.\n\n## \uD83D\uDD70\uFE0F Time Complexity: $$O(n)$$\n\n**Time Complexity:** The function iterates through the bills list once, making the time complexity $$O(n)$$, where `n` is the number of customers (or the length of the bills list).\n\n## \uD83D\uDCBE Space Complexity: $$O(1)$$\n\n**Space Complexity:** The space complexity is $$O(1)$$ since only a fixed amount of extra space (for the `fives` and `tens` counters) is used, regardless of the input size.\n\n## \uD83E\uDDE9 Example Walkthrough:\n\nConsider the `input` **bills** = $$[5, 5, 5, 10, 20]$$:\n\n- **First Customer:**\n - Pays with $$5$$ dollars. **No change needed**. `fives` = $$1$$, `tens` = $$0$$.\n- **Second Customer:**\n - Pays with $$5$$ dollars. No change needed. `fives` = $$2$$, `tens` = $$0$$.\n- **Third Customer:**\n - Pays with $$5$$ dollars. No change needed. `fives` = $$3$$, `tens` = $$0$$.\n- **Fourth Customer:**\n - Pays with $$10$$ dollars. Change needed: $$5$$ dollars.\n - Use one $$5$$ dollar bill. `fives` = 2, `tens` = 1.\n- **Fifth Customer:**\n - Pays with $$20$$ dollars. Change needed: $$15$$ dollars.\n - Use one $$10$$ dollars bill and one $$5$$ dollars bill. `fives` = $$1$$, `tens` = $$0$$.\n\nSince all customers were provided with the correct change, the function returns **True**.\n\n# \uD83D\uDCBBCode:\n``` Python []\nclass Solution(object):\n def lemonadeChange(self, bills):\n fives, tens = 0, 0\n\n for i in bills:\n if i == 5:\n fives += 1\n elif i == 10:\n if fives == 0:\n return False\n fives -= 1\n tens += 1\n else:\n if tens > 0 and fives > 0:\n tens -= 1\n fives -= 1\n elif fives >= 3:\n fives -= 3\n else:\n return False\n \n return True\n```\n``` C++ []\nclass Solution {\npublic:\n bool lemonadeChange(vector<int>& bills) {\n int fives = 0, tens = 0;\n\n for (int bill : bills) {\n if (bill == 5) {\n fives++;\n } else if (bill == 10) {\n if (fives == 0) {\n return false;\n }\n fives--;\n tens++;\n } else {\n if (tens > 0 && fives > 0) {\n tens--;\n fives--;\n } else if (fives >= 3) {\n fives -= 3;\n } else {\n return false;\n }\n }\n }\n return true;\n }\n};\n```\n``` Java []\nclass Solution {\n public boolean lemonadeChange(int[] bills) {\n int fives = 0, tens = 0;\n\n for (int bill : bills) {\n if (bill == 5) {\n fives++;\n } else if (bill == 10) {\n if (fives == 0) {\n return false;\n }\n fives--;\n tens++;\n } else {\n if (tens > 0 && fives > 0) {\n tens--;\n fives--;\n } else if (fives >= 3) {\n fives -= 3;\n } else {\n return false;\n }\n }\n }\n return true;\n }\n}\n```\n> # Upvote and make the kitty happy\uD83D\uDE0A\n\n
| 2 | 0 |
['Array', 'Greedy', 'Simulation', 'Python', 'C++', 'Java', 'Python3']
| 1 |
lemonade-change
|
🧑🎓Very Easy C++ Approach✨ Detailed Explanation✅....Beats 99.99% solutions on LeetCode ❤️🔥
|
very-easy-c-approach-detailed-explanatio-zr03
|
Intuition\nFirst of all I tried to analyse that what are the different amounts in which the customer will be paying the bill and then I realised that this could
|
codonaut_anant_05
|
NORMAL
|
2024-08-15T05:16:21.434335+00:00
|
2024-08-15T05:16:21.434372+00:00
| 58 | false |
# Intuition\nFirst of all I tried to analyse that what are the different amounts in which the customer will be paying the bill and then I realised that this could be solved using simple if-else-if ladder statements.\n\n\n\n\n# Approach\n- Initialise two variables `five_bill` and `ten_bill` which will denote the count of the 5 dollar bills and 10 dollar bills respectively.\n- Traverse through the bills vector using for loop.\n- If amount given by customer is equal to 5 then increment the count of five_bill.\n- Else if amount given by the customer is equal to 10 then check if `five_bill>0` then increment `ten_bill` and decrement `five_bill` else return `false`.\n- Else check if `(five_bill>0 and ten_bill>0)` then decrement `five_bill` and `ten_bill` else check if `five_bill>2` then decrease count of `five_bill` by 3 else return `false`;\n- Finally, return `true` if nothing is returned till now.\n\n# Complexity\n- **Time complexity:** O(N) \u23F3\n\n- **Space complexity:** O(1) \uD83D\uDE80\n\n# Code\n```\nclass Solution {\npublic:\n bool lemonadeChange(vector<int>& bills) {\n int five_bill=0, ten_bill=0;\n for(auto x: bills){\n if(x==5) ++five_bill;\n else if(x==10){\n if(five_bill>0){\n --five_bill;\n ++ten_bill;\n }\n else return false;\n }\n else {\n if(five_bill>0 and ten_bill>0){\n --five_bill;\n --ten_bill;\n }\n else if(five_bill>2){\n five_bill-=3;\n }\n else return false;\n }\n }\n return true;\n }\n};\n```\n\n*Please upvote\uD83D\uDC4D if you found it helpful guys as it would encourage me to post more such solutions. \uD83D\uDE07*
| 2 | 0 |
['Array', 'C++']
| 0 |
lemonade-change
|
simplest javascript solution | O(n) time | O(1) space
|
simplest-javascript-solution-on-time-o1-86br4
|
Approach\n Describe your approach to solving the problem. \nFor each iteration do the following\n- if 5 dollar bill, increment variable c5 \n- if 10 dollar bill
|
ahladshetty
|
NORMAL
|
2024-08-15T04:39:50.740153+00:00
|
2024-08-15T04:39:50.740183+00:00
| 27 | false |
# Approach\n<!-- Describe your approach to solving the problem. -->\nFor each iteration do the following\n- if 5 dollar bill, increment variable c5 \n- if 10 dollar bill, increment variable c10 and decrement c5\n- if 20 dollar bill, if 10 dollar bills exist, pay with one 10 and one 5. Otherwise pay with three 5\n\nAt the end of each iteration check if variable c5 or c10 is less than 0 ( return false )\n\n# Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(1)$$\n\n# Code\n```\n/**\n * @param {number[]} bills\n * @return {boolean}\n */\nvar lemonadeChange = function (bills) {\n let c5 = 0\n let c10 = 0\n for(let i of bills){\n if(i === 5)\n c5 +=1\n else if(i === 10){\n c10 +=1\n c5 -=1\n }\n else{ // 20\n if(c10 != 0){\n c10 -= 1\n c5 -= 1\n } else \n c5 -= 3\n }\n if(c5 < 0 || c10 < 0) return false\n }\n return true;\n};\n```
| 2 | 0 |
['JavaScript']
| 0 |
lemonade-change
|
Java Solution
|
java-solution-by-shree_govind_jee-2nzm
|
Code\n\nclass Solution {\n public boolean lemonadeChange(int[] bills) {\n int[] noteCount = new int[3];\n Arrays.fill(noteCount, 0);\n\n
|
Shree_Govind_Jee
|
NORMAL
|
2024-08-15T03:47:04.508839+00:00
|
2024-08-15T03:47:04.508890+00:00
| 140 | false |
# Code\n```\nclass Solution {\n public boolean lemonadeChange(int[] bills) {\n int[] noteCount = new int[3];\n Arrays.fill(noteCount, 0);\n\n for (int i = 0; i < bills.length; i++) {\n if (bills[i] == 5) {\n noteCount[0] += 1;\n }\n if (bills[i] == 10) {\n if (noteCount[0] == 0)\n return false;\n else {\n noteCount[1] += 1;\n noteCount[0] -= 1;\n }\n }\n if (bills[i] == 20) {\n if (noteCount[0] == 0)\n return false;\n if (noteCount[0] < 3 && noteCount[1] == 0)\n return false;\n else {\n if (noteCount[1] != 0 && noteCount[0] != 0) {\n noteCount[1] -= 1;\n noteCount[0] -= 1;\n noteCount[2] += 1;\n } else if (noteCount[0] >= 3) {\n noteCount[2] += 1;\n noteCount[0] -= 3;\n }\n }\n }\n }\n\n return true;\n }\n}\n```
| 2 | 0 |
['Array', 'Greedy', 'Java']
| 0 |
lemonade-change
|
C# Solution for Lemonade Change Problem
|
c-solution-for-lemonade-change-problem-b-zz37
|
Intuition\n Describe your first thoughts on how to solve this problem. \nThe idea behind this solution is to use a flexible data structure (a dictionary) to man
|
Aman_Raj_Sinha
|
NORMAL
|
2024-08-15T02:36:14.997172+00:00
|
2024-08-15T02:38:45.094445+00:00
| 119 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe idea behind this solution is to use a flexible data structure (a dictionary) to manage the count of different bills. The problem can be simplified by focusing on how much change is needed and ensuring that we can always meet this requirement using the bills we have in hand. The dictionary allows us to dynamically manage and query the number of $5 and $10 bills available, making it easy to check if we can provide the correct change for each transaction.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1.\tDictionary to Track Bills:\n\t\u2022\tWe use a Dictionary<int, int> where the keys represent the bill denominations (5 and 10), and the values represent the number of each bill currently available.\n2.\tProcessing Each Transaction:\n\t\u2022\tIf the customer pays with a 5 bill, simply increase the count of 5 bills in the dictionary.\n\t\u2022\tIf the customer pays with a 10 bill, we check if there\u2019s a 5 bill available (needed to provide 5 as change). If yes, decrement the count of 5 bills and increment the count of 10 bills.\n\t\u2022\tIf the customer pays with a 20 bill, try to give change using one 10 bill and one 5 bill (this is preferred to preserve smaller bills). If that isn\u2019t possible, check if three 5 bills can be given as change. If neither is possible, return false.\n3.\tReturn true if you successfully provide change for all customers.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n): The solution iterates through the bills array once, performing constant-time operations for each transaction. Since dictionary operations (like lookup and update) are O(1), the overall time complexity is linear with respect to the number of customers.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1): The space complexity is constant because the dictionary only stores two keys (for $5 and $10 bills), regardless of the input size. This small, fixed amount of additional space is used irrespective of the number of transactions.\n\n# Code\n```\npublic class Solution {\n public bool LemonadeChange(int[] bills) {\n // Dictionary to store the count of $5 and $10 bills\n Dictionary<int, int> billCount = new Dictionary<int, int> {\n {5, 0},\n {10, 0}\n };\n \n foreach (int bill in bills) {\n if (bill == 5) {\n billCount[5]++;\n } else if (bill == 10) {\n // To give $5 as change\n if (billCount[5] > 0) {\n billCount[5]--;\n billCount[10]++;\n } else {\n return false;\n }\n } else if (bill == 20) {\n // Prefer to give $10 + $5 as change\n if (billCount[10] > 0 && billCount[5] > 0) {\n billCount[10]--;\n billCount[5]--;\n } \n // Otherwise, give three $5 bills as change\n else if (billCount[5] >= 3) {\n billCount[5] -= 3;\n } else {\n return false;\n }\n }\n }\n \n return true;\n }\n}\n```
| 2 | 0 |
['C#']
| 0 |
lemonade-change
|
My Greedy Solution || T(n): O(n) || S(n) = O(1)
|
my-greedy-solution-tn-on-sn-o1-by-ramcha-fa7b
|
Approach\nTo accept a tenDollarBill, we need at least one fiveDollarBill to give the customer correct change.\nTo accept a twentyDollarBill, we need to have eit
|
ramcharan2410
|
NORMAL
|
2024-06-04T04:53:10.549040+00:00
|
2024-06-04T04:53:10.549067+00:00
| 810 | false |
# Approach\nTo accept a tenDollarBill, we need at least one fiveDollarBill to give the customer correct change.\nTo accept a twentyDollarBill, we need to have either (tenDollarBill+fiveDollarBill) or (3 fiveDollarBills) to give the customer correct change.\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\npublic:\n bool lemonadeChange(vector<int>& bills) {\n int fiveBillCount=0,tenBillCount=0;\n for(int bill:bills){\n if(bill==5) fiveBillCount++;\n else if(bill==10){\n if(fiveBillCount>=1){\n tenBillCount++;\n fiveBillCount--;\n }\n else return false;\n }\n else{\n if(tenBillCount>=1 and fiveBillCount>=1){\n tenBillCount--;\n fiveBillCount--;\n }\n else if(fiveBillCount>=3){\n fiveBillCount-=3;\n }\n else return false;\n }\n }\n return true;\n }\n};\n```
| 2 | 0 |
['Greedy', 'C++']
| 0 |
lemonade-change
|
Kotlin OOP Solution | O(n) Time, beats 100%
|
kotlin-oop-solution-on-time-beats-100-by-60bw
|
Approach\n\nGreedy\n\n# Complexity\n\n- Time complexity: O(n)\n- Space complexity: O(1)\n\n# Code\n\nkt\nclass Solution {\n fun lemonadeChange(bills: IntArra
|
naXa777
|
NORMAL
|
2024-02-13T01:18:01.002869+00:00
|
2024-02-13T01:18:01.002891+00:00
| 27 | false |
# Approach\n\nGreedy\n\n# Complexity\n\n- Time complexity: $$O(n)$$\n- Space complexity: $$O(1)$$\n\n# Code\n\n```kt\nclass Solution {\n fun lemonadeChange(bills: IntArray): Boolean {\n bank.reset()\n return bills.all { bill ->\n bank.change(bill)\n }\n }\n\n companion object {\n private val bank = Bank()\n }\n}\n\nprivate class Bank() {\n private var five: Int = 0\n private var ten: Int = 0\n\n fun change(bill: Int): Boolean {\n when (bill) {\n 5 -> {\n ++this.five\n }\n 10 -> {\n if (this.five == 0) return false\n --this.five\n ++this.ten\n }\n 20 -> {\n if (this.ten == 0) {\n if (this.five < 3) return false\n this.five -= 3\n } else {\n if (this.five == 0) return false\n --this.five\n --this.ten\n }\n }\n else -> {}\n }\n return true\n }\n\n fun reset() {\n this.five = 0\n this.ten = 0\n }\n}\n```\n\n# Screenshot\n\n\n
| 2 | 0 |
['Array', 'Greedy', 'Kotlin']
| 1 |
lemonade-change
|
Super Easy Java Code || Beats 100%
|
super-easy-java-code-beats-100-by-saurab-6tbr
|
Complexity\n- Time complexity:\nO(n)\n- Space complexity:\nO(1)\n# Code\n\nclass Solution {\n public boolean lemonadeChange(int[] bills) {\n int five
|
Saurabh_Mishra06
|
NORMAL
|
2024-01-17T17:17:07.906400+00:00
|
2024-01-17T17:17:07.906441+00:00
| 375 | false |
# Complexity\n- Time complexity:\nO(n)\n- Space complexity:\nO(1)\n# Code\n```\nclass Solution {\n public boolean lemonadeChange(int[] bills) {\n int five = 0;\n int ten = 0;\n\n for(int bill : bills){\n if(bill == 5){\n five++;\n }else if(bill == 10){\n ten++;\n five--;\n }else if(ten > 0) {\n ten--;\n five--;\n }else {\n five -= 3;\n }\n\n if(five < 0) return false;\n }\n return true;\n }\n}\n```
| 2 | 0 |
['Java']
| 0 |
lemonade-change
|
Easy C++ solution || Greedy approach
|
easy-c-solution-greedy-approach-by-bhara-1jbc
|
\n# Code\n\nclass Solution {\npublic:\n bool lemonadeChange(vector<int>& bills) {\n int n = bills.size(), change5 = 0, change10 = 0;\n for(int
|
bharathgowda29
|
NORMAL
|
2024-01-02T18:10:32.464560+00:00
|
2024-01-02T18:10:32.464592+00:00
| 1,045 | false |
\n# Code\n```\nclass Solution {\npublic:\n bool lemonadeChange(vector<int>& bills) {\n int n = bills.size(), change5 = 0, change10 = 0;\n for(int i=0; i<n; i++){\n if(bills[i] == 5){\n change5++;\n }\n else if(bills[i] == 10){\n if(change5 <= 0){\n return false;\n }\n change5--;\n change10++;\n }\n else if(bills[i] == 20){\n if(change10 > 0 && change5 > 0){\n change10--;\n change5--;\n }\n else if(change5 > 2){\n change5 -= 3;\n }\n else{\n return false;\n }\n }\n if(change5 < 0 && change10 < 0){\n return false;\n }\n }\n\n return true;\n }\n};\n```
| 2 | 0 |
['Array', 'Greedy', 'C++']
| 0 |
lemonade-change
|
Super Easy Solution - O(n) TC, O(1) SC
|
super-easy-solution-on-tc-o1-sc-by-am_it-u1t9
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
|
Am_it
|
NORMAL
|
2023-12-09T13:54:09.895822+00:00
|
2023-12-09T13:54:09.895844+00:00
| 12 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n bool lemonadeChange(vector<int>& bills) {\n // 0th index -> 5 rs, 1st index-> 10rs, 2nd index->20 rs\n vector<int> v = {0,0,0};\n\n int i=0;\n while(i<bills.size()){\n // if 5 rs ka note hai \n if(bills[i]==5){\n v[0]++;\n }\n else if(bills[i]==10){\n v[1]++;\n // 5 rs ka change dena pdega\n if(v[0]==0) return false;\n else v[0]--;\n }\n else if(bills[i]==20){\n v[2]++;\n // 15 ka change dena hai\n // ya toh ek 10 ka note and ek 5 ka note\n if(v[1]>0 and v[0]>0){\n v[1]--;\n v[0]--;\n }\n // ya 5 ke 3 notes se 15 ka change denge\n else if(v[0]>=3){\n v[0] = v[0] - 3;\n }\n // ni hai possible toh false\n else return false;\n }\n i++;\n }\n return true;\n }\n};\n```\nIf you like my soln please upvote this .\n\n\n\n
| 2 | 0 |
['C++']
| 0 |
lemonade-change
|
Lemonade Change solution in JS - Easy enough for Humans to understand rather than just machines
|
lemonade-change-solution-in-js-easy-enou-tm1l
|
Intuition\nTook more of a human approach for this solution. Didn\'t feel like writing a shorter code,\n\n>\u201CAny fool can write code that a computer can unde
|
BoweryKing
|
NORMAL
|
2023-12-02T21:31:57.521416+00:00
|
2023-12-02T21:31:57.521448+00:00
| 271 | false |
# Intuition\nTook more of a human approach for this solution. Didn\'t feel like writing a shorter code,\n\n>\u201CAny fool can write code that a computer can understand. Good programmers write code that humans can understand.\u201D \n> \n> ~ Martin Fowler, 2008.\n\n# Approach\nImagine having an actual lemonade stand. You take the bil from your customer, if the given bill is more than the price of the lemonade purchased then you return change, otherwise not.\n\nThe given code bellow is absolutely self explanatory as I promised a *Human Readable Code*. \n\n# Complexity\n- Time complexity: *O(n)*, where `n` is the length of the input array `bills`. The reason is the loop in the `lemonadeChange` function, which iterates through each bill in the array once.\n\n- Space complexity: *O(n)*, The `bills` Map in the Cashier class can grow in proportion to the number of `bills` processed in the `lemonadeChange` function.\n\n# Code\n```\n/**\n * Cashier class to handle bill transactions.\n */\nclass Cashier {\n /**\n * Constructor for the Cashier class.\n */\n constructor() {\n this.bills = new Map();\n this.PRICE_OF_LEMONADE = 5;\n this.valid_bills = [20, 10, 5];\n }\n\n /**\n * Check the balance of a specific bill.\n * @param {number} bill - The denomination of the bill.\n * @return {number} The number of bills of this denomination.\n */\n checkBalance(bill) {\n return this.bills.get(bill) || 0;\n }\n\n /**\n * Deposit a specific bill.\n * @param {number} bill - The denomination of the bill.\n * @param {number} [n=1] - The number of bills to deposit.\n */\n deposit(bill, n = 1) {\n var _previousBalance = this.checkBalance(bill);\n\n this.bills.set(bill, _previousBalance + n);\n }\n\n /**\n * Withdraw a specific bill.\n * @param {number} bill - The denomination of the bill.\n * @param {number} [n=1] - The number of bills to withdraw.\n */\n withdraw(bill, n = 1) {\n var _previousBalance = this.checkBalance(bill);\n\n this.bills.set(bill, _previousBalance - n);\n }\n\n /**\n * Return change for a given amount.\n * @param {number} amount - The amount to return change for.\n * @return {boolean} Whether the change can be returned or not.\n */\n return_change(amount) {\n var _change = amount,\n isReturnable = true;\n\n for (var i = 0; i < this.valid_bills.length; i++) {\n var _currentDenomination = this.valid_bills[i];\n\n while (\n _change >= _currentDenomination &&\n this.checkBalance(_currentDenomination)\n ) {\n this.withdraw(_currentDenomination);\n _change -= _currentDenomination;\n }\n }\n\n if (_change !== 0) {\n isReturnable = false;\n }\n\n return isReturnable;\n }\n}\n\n/**\n * @param {number[]} bills\n * @return {boolean}\n */\nvar lemonadeChange = function (bills) {\n // Empty bills array should not be processed\n if (bills.length === 0) return false;\n\n var cashier = new Cashier();\n\n for (var i = 0; i < bills.length; i++) {\n var _currentBill = bills[i];\n\n // whatever the bill is, we put it in the cash box\n // and then check whether change needs to be returned\n cashier.deposit(_currentBill);\n\n // we only have to return change if given bill is-\n // more than the price of a lemonade\n if (_currentBill > cashier.PRICE_OF_LEMONADE) {\n var _change = _currentBill - cashier.PRICE_OF_LEMONADE;\n\n if (!cashier.return_change(_change)) {\n // since cannot return change for given bill,\n // we remove the bill that we deposited earlier\n cashier.withdraw(_currentBill);\n\n return false;\n }\n }\n }\n\n return true;\n};\n```
| 2 | 0 |
['JavaScript']
| 2 |
lemonade-change
|
C++ || Simple if and else || With Explanation
|
c-simple-if-and-else-with-explanation-by-ocz7
|
\nclass Solution {\npublic:\n bool lemonadeChange(vector<int>& bills) {\n int five=0;\n int ten=0;\n for(int i=0;i<bills.size();i++){\n
|
Inam_28_06
|
NORMAL
|
2023-08-13T15:41:38.996642+00:00
|
2023-08-13T15:42:20.677076+00:00
| 106 | false |
```\nclass Solution {\npublic:\n bool lemonadeChange(vector<int>& bills) {\n int five=0;\n int ten=0;\n for(int i=0;i<bills.size();i++){\n if(bills[i]==5){ \n five++; //count number of five\n }\n else if(bills[i]==10){\n if(five<1){ // we require 5 for 10 if not exist then return false\n return false;\n }\n else{ \n five--; // if 5 five>0 then decrement it \n ten++; // add ten\n }\n }\n \n else if(bills[i]==20){ //final case\n //for 20 we require 15 bill for 20\n if(ten>0 && five>0){ //case 1 ten>0 and five>0 then it is equal to 15 \n ten--; //decrement it\n five--; //decrement it\n \n }\n else if(five>2){ //case 2 five>3 then it is also equal to 15\n five=five-3; // decrement it 3 5 which are used\n }\n else{\n return false; //remaining case return false\n }\n }\n \n }\n return true;\n }\n};\n```\n![Uploading file...]()\n
| 2 | 0 |
['Greedy', 'C', 'C++']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Explained - Sort & positive sum count || Very simple & Easy to Understand Solution
|
explained-sort-positive-sum-count-very-s-t1q3
|
Approach \nHere we need to check for the total prefix sum > 0.\nTo do this all the positive number should be at the begining\nso that the total possible sum mus
|
kreakEmp
|
NORMAL
|
2023-03-12T04:11:20.577970+00:00
|
2023-03-13T02:24:05.203578+00:00
| 4,645 | false |
# Approach \nHere we need to check for the total prefix sum > 0.\nTo do this all the positive number should be at the begining\nso that the total possible sum must be maximum and after that we need to remove the -ve numbers from the smaller ones. \n\nSo we need to just sort it and start adding the elements and counting it \nuntill we get the sum as -ve or zero.\n\n# Code\n```\n int maxScore(vector<int>& nums) {\n long long ans = 0, sum = 0;\n sort(nums.begin(), nums.end());\n for(int i = nums.size()-1; i >= 0; --i){\n sum += nums[i];\n if(sum > 0) ans++;\n else break;\n }\n return ans;\n }\n```
| 23 | 1 |
['C++']
| 5 |
rearrange-array-to-maximize-prefix-score
|
Python 3 || 2 lines || T/S: 90% / 24%
|
python-3-2-lines-ts-90-24-by-spaulding-xbg5
|
\nclass Solution:\n def maxScore(self, nums: List[int]) -> int:\n \n nums.sort(reverse=True)\n \n return sum(n > 0 for n in accum
|
Spaulding_
|
NORMAL
|
2023-03-12T05:14:08.373903+00:00
|
2024-06-23T01:50:11.810455+00:00
| 820 | false |
```\nclass Solution:\n def maxScore(self, nums: List[int]) -> int:\n \n nums.sort(reverse=True)\n \n return sum(n > 0 for n in accumulate(nums)) \n```\n[https://leetcode.com/problems/rearrange-array-to-maximize-prefix-score/submissions/1297203705/](https://leetcode.com/problems/rearrange-array-to-maximize-prefix-score/submissions/1297203705/)\n\nI could be wrong, but I think that time complexity is *O*(*N* log *N*) and space complexity is *O*(*N*).\n
| 14 | 0 |
['Python3']
| 2 |
rearrange-array-to-maximize-prefix-score
|
Prefix Sum of Sorted Array
|
prefix-sum-of-sorted-array-by-votrubac-xxye
|
Python 3\npython\nclass Solution:\n def maxScore(self, nums: List[int]) -> int:\n return sum(n > 0 for n in accumulate(sorted(nums, reverse=True)))\n\
|
votrubac
|
NORMAL
|
2023-03-12T04:01:22.265641+00:00
|
2023-03-12T09:25:36.370385+00:00
| 3,302 | false |
**Python 3**\n```python\nclass Solution:\n def maxScore(self, nums: List[int]) -> int:\n return sum(n > 0 for n in accumulate(sorted(nums, reverse=True)))\n```\n**C++**\n```cpp\nint maxScore(vector<int>& n) {\n sort(begin(n), end(n), greater<>());\n for (long long sum = 0, i = 0; i <= n.size(); sum += n[i++])\n if (i == n.size() || sum + n[i] <= 0)\n return i;\n return 0;\n}\n```
| 13 | 1 |
['C', 'Python3']
| 2 |
rearrange-array-to-maximize-prefix-score
|
2587: Solution with step by step explanation
|
2587-solution-with-step-by-step-explanat-jxwn
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. Define a function maxScore that takes a list of integers nums as input
|
Marlen09
|
NORMAL
|
2023-03-12T04:07:08.767875+00:00
|
2023-03-12T04:07:08.767907+00:00
| 1,732 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Define a function maxScore that takes a list of integers nums as input and returns an integer.\n2. Sort the input list nums in descending order.\n3. Initialize a list prefix_sums with a single element 0.\n4. Loop through the sorted list nums and compute the prefix sums, appending each sum to the list prefix_sums.\n5. Initialize a variable max_score to 0.\n6. Loop through the indices from 1 to the length of prefix_sums:\na. If the current prefix sum is greater than 0, update max_score to the current index.\n7. Return max_score.\n\n# Complexity\n- Time complexity:\nO(n log n)\n\n- Space complexity:\nO(n),\n\n# Code\n```\nclass Solution:\n def maxScore(self, nums: List[int]) -> int:\n nums.sort(reverse=True)\n prefix_sums = [0]\n for num in nums:\n prefix_sums.append(prefix_sums[-1] + num)\n max_score = 0\n for i in range(1, len(prefix_sums)):\n if prefix_sums[i] > 0:\n max_score = i\n return max_score\n\n```
| 9 | 0 |
['Python', 'Python3']
| 4 |
rearrange-array-to-maximize-prefix-score
|
C++ || EASY || SORTING
|
c-easy-sorting-by-jenish_09-7gec
|
\n\n# Complexity\n- Time complexity: O(n logn)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(1)\n Add your space complexity here, e.g. O(
|
jenish_09
|
NORMAL
|
2023-03-12T05:24:24.638538+00:00
|
2023-03-12T05:24:24.638573+00:00
| 1,194 | false |
\n\n# Complexity\n- Time complexity: $$O(n logn)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n int cnt=0;\n long long sum=0;\n for(int i=nums.size()-1;i>=0;i--)\n {\n sum+=nums[i];\n if(sum>0)\n cnt++;\n }\n return cnt;\n \n }\n};\n```
| 7 | 0 |
['C++']
| 1 |
rearrange-array-to-maximize-prefix-score
|
Simple java solution
|
simple-java-solution-by-siddhant_1602-ddz8
|
Complexity\n- Time complexity: O(n log n) \n\n- Space complexity: O(n) where n is the size of the array\n\n# Code\n\nclass Solution {\n public int maxScore(i
|
Siddhant_1602
|
NORMAL
|
2023-03-12T04:01:12.762381+00:00
|
2023-03-12T04:05:56.436354+00:00
| 2,015 | false |
# Complexity\n- Time complexity: $$O(n log n)$$ \n\n- Space complexity: $$O(n)$$ where n is the size of the array\n\n# Code\n```\nclass Solution {\n public int maxScore(int[] num) {\n Integer[] boxedArr = Arrays.stream(num).boxed().toArray(Integer[]::new);\n Arrays.sort(boxedArr, Comparator.reverseOrder());\n int[] nums = Arrays.stream(boxedArr).mapToInt(Integer::intValue).toArray();\n int sum=0;\n long p=0;\n for(int i:nums)\n {\n p+=(long)i;\n if(p>0)\n {\n sum++;\n }\n else\n {\n p=0;\n }\n }\n return sum;\n }\n}\n```
| 6 | 0 |
['Java']
| 3 |
rearrange-array-to-maximize-prefix-score
|
✅C++ || Sorting
|
c-sorting-by-chiikuu-aobs
|
Complexity\n- Time complexity: O(nlogn)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(1)\n Add your space complexity here, e.g. O(n) \n\n
|
CHIIKUU
|
NORMAL
|
2023-03-12T09:22:03.790446+00:00
|
2023-03-12T09:22:03.790489+00:00
| 428 | false |
# Complexity\n- Time complexity: **O(nlogn)**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: **O(1)**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int maxScore(vector<int>& v) {\n sort(v.begin(),v.end());\n reverse(v.begin(),v.end());\n long long a=0;\n long long c=0;\n for(int i=0;i<v.size();i++){\n a+=v[i];\n if(a>0)c++;\n }\n return c;\n }\n};\n```\n\n
| 5 | 0 |
['Sorting', 'C++']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Full Explanation | Sorting and Prefix Sum | Simple Solution
|
full-explanation-sorting-and-prefix-sum-h6m0s
|
\n# Approach\n Describe your approach to solving the problem. \nWe need to first sort the numbers in the array in descending order (from largest to smallest) so
|
nidhiii_
|
NORMAL
|
2023-03-12T04:07:18.515088+00:00
|
2023-03-12T04:57:22.793048+00:00
| 338 | false |
\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe need to first sort the numbers in the array in descending order (from largest to smallest) so that we can pick the biggest numbers first to maximize our score.\n\nThen create a new array that will store the cumulative sum of the numbers in the sorted array. This means that each element in the new array will be the sum of all the elements before it in the sorted array.\n\nThen loop through the sorted array, starting from the second element. For each element, it calculates the cumulative sum up to that element and stores it in the new array. If the sum is greater than 0, the code increments a counter variable.\n\nFinally, return the value of the counter variable, which represents the maximum score that can be obtained.\n\n# Complexity\n- Time complexity: O(nlogn) for sorting\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n sort(nums.begin(),nums.end(),greater<int>());\n vector<long long>prefix_sum(nums.size());\n \n prefix_sum[0]=nums[0];\n long long count=0;\n \n if(prefix_sum[0]>0) ++count;\n for(int i=1;i<nums.size();++i){\n prefix_sum[i] = prefix_sum[i-1]+nums[i];\n if(prefix_sum[i]>0) ++count;\n }\n \n return count;\n }\n};\n```
| 5 | 0 |
['C++']
| 1 |
rearrange-array-to-maximize-prefix-score
|
C++ || O(1) Space
|
c-o1-space-by-shrikanta8-rtpb
|
\n\n# Code\n\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n \n sort(nums.begin(),nums.end(),greater<int>());\n\n long
|
Shrikanta8
|
NORMAL
|
2023-03-12T04:04:04.097163+00:00
|
2023-03-12T04:40:22.060693+00:00
| 641 | false |
\n\n# Code\n```\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n \n sort(nums.begin(),nums.end(),greater<int>());\n\n long long sum=0,ans=0;\n int n=nums.size(),i;\n\n //positive values\n for(i=0;i<n;i++){\n if(nums[i]<=0)\n break;\n sum += nums[i];\n }\n ans = i;\n //non-positive values\n while(i<n ){\n sum += nums[i++];\n if(sum > 0){\n ans++;\n }\n }\n return ans;\n }\n};\n```
| 5 | 0 |
['C++']
| 1 |
rearrange-array-to-maximize-prefix-score
|
Image Explanation🏆-[Sort + Positive Sum] - Complete Intuition
|
image-explanation-sort-positive-sum-comp-vtka
|
Video Solution\nhttps://youtu.be/_RzI3dDSdZA\n\n# Approach & Intuition\n\n\n\n\n# Code\n\n#define ll long long int\n\nclass Solution {\npublic:\n int maxScor
|
aryan_0077
|
NORMAL
|
2023-03-12T04:17:09.966424+00:00
|
2023-03-12T06:40:01.835100+00:00
| 809 | false |
# Video Solution\nhttps://youtu.be/_RzI3dDSdZA\n\n# Approach & Intuition\n\n\n\n\n# Code\n```\n#define ll long long int\n\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n sort(nums.begin(), nums.end(), greater<int>());\n int n = nums.size();\n \n vector<ll> prefix(n);\n prefix[0] = nums[0];\n \n for(int i=1;i<n;i++){\n prefix[i] = prefix[i-1] + nums[i];\n }\n \n int ans = 0;\n for(int i=0;i<n;i++){\n if(prefix[i]>0ll)\n ans++;\n }\n \n return ans;\n }\n};\n```
| 4 | 1 |
['C++']
| 2 |
rearrange-array-to-maximize-prefix-score
|
✅ c++ greedy | sorting | easy code with logic ✅
|
c-greedy-sorting-easy-code-with-logic-by-xxp0
|
\n# Code\n\nclass Solution {\npublic:\n /*app 1: greedy way ==> rearrange numbers in descending order.\n T.c: O(N*logN)\n \n Intuition::\n \n
|
suraj_jha989
|
NORMAL
|
2023-03-12T04:04:33.354528+00:00
|
2023-03-12T04:05:07.713244+00:00
| 294 | false |
\n# Code\n```\nclass Solution {\npublic:\n /*app 1: greedy way ==> rearrange numbers in descending order.\n T.c: O(N*logN)\n \n Intuition::\n \n to get max. no. of +ve integers, we should have max. no. of positive numbers\n in the beginning of array and then the negative numbers.\n \n bcos, doing so ensures that we have maximum possible +ve prefix sum.\n It\'s like a greedy way of rearranging numbers.\n \n */\n int maxScore(vector<int>& nums) {\n int n=nums.size();\n \n vector<long long> prefix(n);\n \n //sort the array in desc order\n sort(nums.begin(),nums.end(),greater<int>());\n \n prefix[0]=nums[0];\n \n int score=0;\n if(prefix[0] > 0) score++;\n \n //count the no. of +ve numbers in prefix sum array\n for(int i=1; i<n; i++){\n prefix[i] = prefix[i-1] + nums[i];\n \n if(prefix[i] > 0) score++;\n }\n \n return score;\n }\n};\n```
| 4 | 0 |
['C++']
| 2 |
rearrange-array-to-maximize-prefix-score
|
Sort then Prefix
|
sort-then-prefix-by-krush_r_sonwane-x3x5
|
Complexity\nTime O(N * log N)\nSpace O(N) #for sorting\n\n# Code\n\nclass Solution(object):\n def maxScore(self, nums):\n nums.sort()\n res = p
|
krush_r_sonwane
|
NORMAL
|
2023-03-12T04:02:30.856553+00:00
|
2023-03-12T04:02:30.856601+00:00
| 325 | false |
# Complexity\nTime `O(N * log N)`\nSpace `O(N) #for sorting`\n\n# Code\n```\nclass Solution(object):\n def maxScore(self, nums):\n nums.sort()\n res = prefix = 0\n while nums:\n prefix += nums.pop()\n if prefix > 0: \n res += 1\n return res\n```\n**UpVote**, if you like it :)
| 4 | 0 |
['Sorting']
| 0 |
rearrange-array-to-maximize-prefix-score
|
C++ || SIMPLE || EASY TO UNDERSTAND
|
c-simple-easy-to-understand-by-ganeshkum-sh97
|
Code\n\nclass Solution {\npublic:\n bool static cmp(int &a,int &b){\n return a>b;\n }\n int maxScore(vector<int>& nums) {\n long long int
|
ganeshkumawat8740
|
NORMAL
|
2023-05-15T12:59:41.224037+00:00
|
2023-05-15T12:59:41.224086+00:00
| 887 | false |
# Code\n```\nclass Solution {\npublic:\n bool static cmp(int &a,int &b){\n return a>b;\n }\n int maxScore(vector<int>& nums) {\n long long int x = 0;\n int y = 0;\n sort(nums.begin(),nums.end(),cmp);\n if(nums[0]<=0)return 0;\n for(auto &i: nums){\n x += i;\n if(x<=0){\n return y;\n }\n y++;\n }\n return y;\n }\n};\n```
| 3 | 0 |
['Array', 'Greedy', 'Sorting', 'C++']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Easy python solution || runtime 100% memory 100%
|
easy-python-solution-runtime-100-memory-j0zk9
|
Code\n\nclass Solution(object):\n def maxScore(self, nums):\n """\n :type nums: List[int]\n :rtype: int\n """\n nums.sort(
|
Lalithkiran
|
NORMAL
|
2023-03-13T13:22:47.061247+00:00
|
2023-03-13T13:22:47.061278+00:00
| 292 | false |
# Code\n```\nclass Solution(object):\n def maxScore(self, nums):\n """\n :type nums: List[int]\n :rtype: int\n """\n nums.sort()\n sm=sum(nums)\n cnt=0\n for i in nums:\n if sm>0:cnt+=1\n sm-=i\n return cnt\n```
| 3 | 0 |
['Sorting', 'Prefix Sum', 'Python']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Simple JS
|
simple-js-by-uriha-21mw
|
ma# Code\n\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maxScore = function(nums) {\n nums.sort((a,b)=>{return b-a})\n let n = 0\n le
|
uriha
|
NORMAL
|
2023-03-12T11:42:35.128494+00:00
|
2023-03-12T11:42:35.128524+00:00
| 106 | false |
ma# Code\n```\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maxScore = function(nums) {\n nums.sort((a,b)=>{return b-a})\n let n = 0\n let c = nums[n]\n while(c > 0){\n n++\n c += nums[n]\n }\n return n\n};\n```
| 3 | 0 |
['Math', 'Sorting', 'JavaScript']
| 1 |
rearrange-array-to-maximize-prefix-score
|
Simple greedy + sorting c++ solution
|
simple-greedy-sorting-c-solution-by-anik-pc0t
|
Intuition\n Describe your first thoughts on how to solve this problem. \nIn this question we have to find maximum score.It mean we have to think greedily and we
|
aniketrajput25
|
NORMAL
|
2023-03-12T06:19:57.060608+00:00
|
2023-03-12T06:19:57.060641+00:00
| 87 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIn this question we have to find maximum score.It mean we have to think greedily and we can even allow to change order to get maximum score.Sorting in reverse order would be perfect choice to get optimal order.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Sort the vector in Decending order.\n2. Initialise sum and ans with value of 0.\n3. Traverse through the vector and start adding element to sum.\n4. If sum is greater then 0 the increase the count of ans by 1 else break the loop\n5. Return the ans\n\n# Complexity\n- Time complexity:O(N*log(N))\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) \n {\n sort(nums.rbegin(),nums.rend());\n long long sum=0;\n int ans=0;\n for(int i=0;i<nums.size();i++)\n {\n sum+=nums[i];\n if(sum>0) ans++;\n else break;\n }\n return ans;\n }\n};\n```
| 3 | 0 |
['Greedy', 'Sorting', 'C++']
| 0 |
rearrange-array-to-maximize-prefix-score
|
✅C++ || Beginner Friendly && EASY CODE || CLEAN CODE
|
c-beginner-friendly-easy-code-clean-code-6kv6
|
\n\nT->O(n logn) && S->O(1)\n\n\tclass Solution {\n\t\tpublic:\n\t\t\tint maxScore(vector& nums) {\n\t\t\t\tint n = nums.size();\n\t\t\t\tint ans = 0;\n\t\t\t\t
|
abhinav_0107
|
NORMAL
|
2023-03-12T04:09:20.310687+00:00
|
2023-03-12T04:09:20.310731+00:00
| 898 | false |
\n\n**T->O(n logn) && S->O(1)**\n\n\tclass Solution {\n\t\tpublic:\n\t\t\tint maxScore(vector<int>& nums) {\n\t\t\t\tint n = nums.size();\n\t\t\t\tint ans = 0;\n\t\t\t\tlong long sum = 0;\n\t\t\t\tsort(nums.begin(),nums.end(),greater<int>());\n\n\t\t\t\tfor(int i=0;i<n;i++){\n\t\t\t\t\tsum+=nums[i];\n\t\t\t\t\tif(sum > 0) ans++;\n\t\t\t\t}\n\n\t\t\t\treturn ans;\n\t\t\t}\n\t\t};
| 3 | 0 |
['Array', 'C', 'Sorting', 'Prefix Sum', 'C++']
| 3 |
rearrange-array-to-maximize-prefix-score
|
Java | Sorting | Simple code
|
java-sorting-simple-code-by-judgementdey-m6im
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
|
judgementdey
|
NORMAL
|
2023-03-12T04:06:47.301669+00:00
|
2023-03-12T19:02:15.254429+00:00
| 89 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n*log(n))$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int maxScore(int[] nums) {\n var n = nums.length;\n Arrays.sort(nums);\n \n var sum = 0L;\n var i = n-1;\n for (; i >= 0; i--) {\n sum += nums[i];\n if (sum <= 0) break;\n }\n return n-1-i;\n }\n}\n```
| 3 | 0 |
['Sorting', 'Java']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Short || Clean || Java
|
short-clean-java-by-himanshubhoir-dbcz
|
java []\nclass Solution {\n public int maxScore(int[] nums) {\n long sum = 0;\n int i=nums.length-1;\n Arrays.sort(nums);\n if(nu
|
HimanshuBhoir
|
NORMAL
|
2023-03-12T04:05:35.057407+00:00
|
2023-03-12T04:05:35.057444+00:00
| 1,415 | false |
```java []\nclass Solution {\n public int maxScore(int[] nums) {\n long sum = 0;\n int i=nums.length-1;\n Arrays.sort(nums);\n if(nums[nums.length-1] == 0) return 0;\n while(i >= 0){\n sum += nums[i--];\n if(sum <= 0) return nums.length-i-2;\n }\n return nums.length;\n }\n}\n```
| 3 | 0 |
['Java']
| 2 |
rearrange-array-to-maximize-prefix-score
|
Easy Java Solution
|
easy-java-solution-by-1asthakhushi1-4hcf
|
\n# Code\n\nclass Solution {\n public int maxScore(int[] nums) {\n Arrays.sort(nums);\n <!-- sorting array is descending order by swapping -->\
|
1asthakhushi1
|
NORMAL
|
2023-03-12T04:02:31.751967+00:00
|
2024-02-27T06:27:54.468022+00:00
| 672 | false |
\n# Code\n```\nclass Solution {\n public int maxScore(int[] nums) {\n Arrays.sort(nums);\n <!-- sorting array is descending order by swapping -->\n for(int i=0;i<nums.length/2;i++){\n int ref=nums[i];\n nums[i]=nums[nums.length-i-1];\n nums[nums.length-i-1]=ref;\n }\n long sum=0;\n int count=0;\n for(int i=0;i<nums.length;i++){\n sum+=nums[i];\n if(sum>0)\n count++;\n }\n return count;\n }\n}\n```
| 3 | 0 |
['Sorting', 'Java']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Simple Java 5 lines solution
|
simple-java-5-lines-solution-by-vinaygar-tey0
|
Intuition\n Describe your first thoughts on how to solve this problem. \n Sort the array in ascending order and start from the right. \n keep adding the numbers
|
vinaygarg25
|
NORMAL
|
2023-12-20T05:57:22.795375+00:00
|
2023-12-20T05:57:22.795446+00:00
| 115 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n* Sort the array in ascending order and start from the right. \n* keep adding the numbers from right and if the sum is positive, increase the count by 1. \n* As soon as sum reaches 0 or negative, break out of the loop and return the value of count; \n\n\n\n# Complexity\n- Time complexity: O(NlogN) - for sorting the array\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int maxScore(int[] nums) {\n Arrays.sort(nums);\n long sum = 0;\n int count = 0; \n for(int i=nums.length-1; i>=0; i--){\n sum += nums[i];\n if(sum > 0) count++;\n else break; \n }\n return count; \n }\n}\n```
| 2 | 0 |
['Java']
| 0 |
rearrange-array-to-maximize-prefix-score
|
C++ Solution
|
c-solution-by-pranto1209-fmo3
|
Approach\n Describe your approach to solving the problem. \n Sorting\n\n# Complexity\n- Time complexity:\n Add your time complexity here, e.g. O(n) \n O(n
|
pranto1209
|
NORMAL
|
2023-07-24T05:54:23.137349+00:00
|
2023-07-24T05:54:23.137367+00:00
| 104 | false |
# Approach\n<!-- Describe your approach to solving the problem. -->\n Sorting\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n O(n * log(n))\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n O(1)\n\n# Code\n```\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n sort(nums.rbegin(), nums.rend());\n long long sum = 0;\n int ans = 0;\n for (int i = 0; i < nums.size(); i++) {\n sum += nums[i];\n if (sum > 0) ans++;\n }\n return ans;\n }\n};\n```
| 2 | 0 |
['C++']
| 0 |
rearrange-array-to-maximize-prefix-score
|
✅Easy Java solution||Intuition||Sort||Beginner Friendly🔥
|
easy-java-solutionintuitionsortbeginner-cen1b
|
Intuition\n Describe your first thoughts on how to solve this problem. \nFirst of all sort the array and then Get the prefix sums of the array and then just cou
|
deepVashisth
|
NORMAL
|
2023-03-15T13:45:02.867879+00:00
|
2023-03-15T13:47:20.275193+00:00
| 60 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**First of all sort the array and then Get the prefix sums of the array and then just count the total number of positive integer in the prefix array**\n\n# Code\n```\nclass Solution {\n public int maxScore(int[] nums) {\n Arrays.sort(nums);\n int count = 0;\n long sum = 0;\n for(int i = nums.length - 1; i >= 0; i --){\n sum += nums[i];\n if(sum > 0){\n count++;\n }\n }\n return count;\n }\n}\n```\n**If you really found my solution helpful please upvote it, as it motivates me to post such kind of codes and help the coding community, if you have some queries or some improvements please feel free to comment and share your views.**
| 2 | 0 |
['Java']
| 0 |
rearrange-array-to-maximize-prefix-score
|
PREFIX SUM OF SORTED ARRAY || C++ || EASY TO UNDERSTAND
|
prefix-sum-of-sorted-array-c-easy-to-und-ci9e
|
\nclass Solution {\npublic:\n bool static cmp(int &a,int &b){\n return a>b;\n }\n int maxScore(vector<int>& nums) {\n sort(nums.begin(),n
|
yash___sharma_
|
NORMAL
|
2023-03-13T16:13:47.728772+00:00
|
2023-03-13T16:13:47.728809+00:00
| 295 | false |
```\nclass Solution {\npublic:\n bool static cmp(int &a,int &b){\n return a>b;\n }\n int maxScore(vector<int>& nums) {\n sort(nums.begin(),nums.end(),cmp);\n if(nums[0]<=0)return 0;\n long long int ans = 0;\n int i = 0,n=nums.size();\n for(i = 0; i < n; i++){\n ans += nums[i];\n if(ans<=0)break;\n }\n return min(i,n);\n }\n};\n```
| 2 | 0 |
['Array', 'Greedy', 'C', 'Sorting', 'Prefix Sum', 'C++']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Python Elegant & Short | itertools | 1-line
|
python-elegant-short-itertools-1-line-by-n1kr
|
\nfrom itertools import accumulate\nfrom typing import List\n\n\nclass Solution:\n def maxScore(self, nums: List[int]) -> int:\n return sum(p > 0 for
|
Kyrylo-Ktl
|
NORMAL
|
2023-03-13T09:43:16.001077+00:00
|
2023-03-13T09:43:16.001121+00:00
| 147 | false |
```\nfrom itertools import accumulate\nfrom typing import List\n\n\nclass Solution:\n def maxScore(self, nums: List[int]) -> int:\n return sum(p > 0 for p in accumulate(sorted(nums, reverse=True)))\n\n```
| 2 | 0 |
['Python3']
| 0 |
rearrange-array-to-maximize-prefix-score
|
✅ Sort + Pre Sum || Faster 100% || 2 lines
|
sort-pre-sum-faster-100-2-lines-by-t86-6ng7
|
c++\nclass Solution {\npublic:\n int maxScore(vector<int>& nums, int res = 0, long long pre = 0) {\n sort(nums.begin(), nums.end(), greater<int>());\n
|
t86
|
NORMAL
|
2023-03-12T11:52:08.358513+00:00
|
2023-03-24T19:49:17.504522+00:00
| 179 | false |
```c++\nclass Solution {\npublic:\n int maxScore(vector<int>& nums, int res = 0, long long pre = 0) {\n sort(nums.begin(), nums.end(), greater<int>());\n for (auto num: nums) pre += num, res += pre > 0;\n return res;\n }\n};\n```\n\n**For more solutions, check out this \uD83C\uDFC6 [GITHUB REPOSITORY](https://github.com/MuhtasimTanmoy/playground) with over 1500+ solutions.**
| 2 | 0 |
[]
| 0 |
rearrange-array-to-maximize-prefix-score
|
Beginner friendly||easy to understand||c++||100%beats
|
beginner-friendlyeasy-to-understandc100b-f3rw
|
Intuition\nAs it is clearly mentioned in the question we have to arrange the given array in such a way that no of positive elements in the prefix of the array
|
klearner_051
|
NORMAL
|
2023-03-12T07:13:56.818059+00:00
|
2023-03-12T07:13:56.818103+00:00
| 85 | false |
# Intuition\nAs it is clearly mentioned in the question we have to arrange the given array in such a way that no of positive elements in the prefix of the array is maximum.\n\n# Approach\nSort the array(so that all elements having highest value are at the end) and maintain the sum variable and a count variable c and keep on calculation the sum till it is greater than 0,return the count.\n\n\n# Complexity\n- Time complexity:\nO(n*logn)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n//Greatest element at the end\n sort(nums.begin(),nums.end());\n int c=0;\n//To avoid integer overflow\n long long int sum=0;\n \nfor(int i=nums.size()-1;i>=0;i--){\nsum+=nums[i];\n if(sum>0){\nc++;\n }\n else{\nbreak;\n }\n\n}\n return c;\n \n \n }\n};\n```
| 2 | 0 |
['Sorting', 'C++']
| 3 |
rearrange-array-to-maximize-prefix-score
|
Go, C# || Sort Descending
|
go-c-sort-descending-by-wild-boar-t0h3
|
Approach\n - Sort nums is descending.\n - Sum prefix\n - If prefix > 0, increase res, else return.\n\n# Complexity\n- Time complexity: O(nlog(n))\n- Space compl
|
Wild-Boar
|
NORMAL
|
2023-03-12T04:08:09.781240+00:00
|
2023-03-15T06:49:59.727776+00:00
| 66 | false |
# Approach\n - Sort **nums** is descending.\n - Sum **prefix**\n - If **prefix** > 0, increase **res**, else return.\n\n# Complexity\n- Time complexity: O(nlog(n))\n- Space complexity: O(1)\n\n```Go []\nfunc maxScore(nums []int) int {\n\tvar res int = 0\n\tvar prefix int64 = 0\n\tsort.SliceStable(nums, func(i, j int) bool {\n\t\treturn nums[i] > nums[j]\n\t})\n\tfor _, num := range nums {\n\t\tprefix += int64(num)\n\t\tif prefix > 0 {\n\t\t\tres++\n\t\t} else {\n\t\t\tbreak\n\t\t}\n\t}\n\treturn res\n}\n```\n```C# []\npublic class Solution {\n public int MaxScore(int[] nums)\n {\n int res = 0;\n long predix = 0;\n Array.Sort(nums, (a, b) => b - a);\n foreach (int num in nums)\n {\n predix += num;\n if (predix > 0)\n res++;\n else\n return res; ;\n }\n return res;\n }\n}\n```
| 2 | 0 |
['Go', 'C#']
| 0 |
rearrange-array-to-maximize-prefix-score
|
easy short efficient clean code
|
easy-short-efficient-clean-code-by-maver-0d2f
|
\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n sort(begin(nums), end(nums));\n long long ans=0, sum=0;\n for(auto it=
|
maverick09
|
NORMAL
|
2023-03-12T04:03:36.995054+00:00
|
2023-03-12T04:16:40.622559+00:00
| 143 | false |
```\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n sort(begin(nums), end(nums));\n long long ans=0, sum=0;\n for(auto it=crbegin(nums);it!=crend(nums);++it){ // iterate from right(+ve) to left (-ve)\n sum = sum + *it;\n if(sum<1){\n break;\n }\n ++ans;\n }\n return ans;\n }\n};\n```
| 2 | 0 |
['Greedy', 'C', 'Sorting']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Kadanes algo Simple
|
kadanes-algo-simple-by-amanagrawal20156-jfcy
|
Intuition: KADANE\u2019S Algorithm \nAs the question state we can arrange in any order. So Sort the given array and find all the Prefixsum which is greater than
|
amanagrawal20156
|
NORMAL
|
2023-03-12T04:03:19.327413+00:00
|
2023-03-12T04:03:19.327454+00:00
| 92 | false |
Intuition: KADANE\u2019S Algorithm \nAs the question state we can arrange in any order. So Sort the given array and find all the Prefixsum which is greater than 0 and then increase count and when ever we encounter PrefixSum<0 we have to make it 0.\nAs we can see in the explanation also 0 is not counted in the examples. So we have to discard and take all the Prefixsum >0 and based on this our count will be increased.\nThe time complexity is O(nlogn) bcoz of sorting. Overall time complexity due to our for loop is O(N).\nThank You.\n\nPlease Upvote if you like the approach.\n\n```\nclass Solution {\n public int maxScore(int[] nums) {\n \n long sum=0;\n int count=0;\n Arrays.sort(nums);\n for(int i=nums.length-1;i>=0;i--){\n sum+=nums[i];\n \n if(sum>0){\n count++;\n }else{\n sum=0;\n }\n }\n return count;\n }\n}\n```
| 2 | 0 |
['Array', 'Sorting', 'Java']
| 0 |
rearrange-array-to-maximize-prefix-score
|
C++ | Sorting | Prefix Sum | Easy
|
c-sorting-prefix-sum-easy-by-trinity_06-igwp
|
ApproachRunning sum of sorted array until it becomes negative.Complexity
Time complexity: O(nlogn)
Space complexity: O(1)
Code
|
trinity_06
|
NORMAL
|
2025-02-13T06:44:55.400080+00:00
|
2025-02-13T06:44:55.400080+00:00
| 108 | false |
# Approach
Running sum of sorted array until it becomes negative.
# Complexity
- Time complexity: $$O(nlogn)$$
- Space complexity: $$O(1)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
int maxScore(vector<int>& nums) {
sort(nums.begin(),nums.end());
long runningSum = 0;
int count=0;
for(int i=nums.size()-1;i>=0;i--){
runningSum+=(long)nums[i];
if(runningSum<=0) break;
count++;
}
return count;
}
};
```
| 1 | 0 |
['Greedy', 'Sorting', 'Prefix Sum', 'C++']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Beginner friendly||easy to understand||c++||100%beats
|
beginner-friendlyeasy-to-understandc100b-5hdx
|
Intuition\nAs it is clearly mentioned in the question we have to arrange the given array in such a way that no of positive elements in the prefix of the array i
|
Yash_Akabari
|
NORMAL
|
2024-09-04T12:50:05.853154+00:00
|
2024-09-04T12:50:05.853187+00:00
| 23 | false |
# Intuition\nAs it is clearly mentioned in the question we have to arrange the given array in such a way that no of positive elements in the prefix of the array is maximum.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nSort the array(so that all elements having highest value are at the end) and maintain the sum variable and a count variable c and keep on calculation the sum till it is greater than 0,return the count.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n- O(n*logn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n- O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n //greater element at the end\n sort(nums.begin(),nums.end());\n int count = 0;\n //to avoid integer overflow\n long long int sum = 0;\n for(int i = nums.size()-1;i>=0;i--){\n sum+=nums[i];\n if(sum > 0){\n count++;\n }\n else{\n break;\n }\n }\n return count;\n }\n};\n```
| 1 | 0 |
['Array', 'Greedy', 'Sorting', 'Prefix Sum', 'C++']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Easy sol^n
|
easy-soln-by-singhgolu933600-dqwg
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
|
singhgolu933600
|
NORMAL
|
2024-05-16T15:43:26.739411+00:00
|
2024-05-16T15:43:26.739447+00:00
| 2 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n bool compare(int a,int b)\n{\n if(a>b)\n return true;\n else\n return false; \n}\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n sort(nums.begin(),nums.end(),compare);\n long long sum=0;\n int c=0;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]>0)\n {\n sum+=(long long)nums[i];\n c++;\n }\n else\n {\n if((sum-abs((long long)nums[i]))>0)\n {\n c++;\n sum=sum-abs((long long)nums[i]);\n }\n else\n break;\n }\n }\n return c;\n }\n};\n```
| 1 | 0 |
['Array', 'Greedy', 'Sorting', 'Prefix Sum', 'C++']
| 0 |
rearrange-array-to-maximize-prefix-score
|
easy c++ o(n) just count the positive sum
|
easy-c-on-just-count-the-positive-sum-by-37pz
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
|
murugapemal1
|
NORMAL
|
2023-04-05T10:56:19.753155+00:00
|
2023-04-05T10:56:19.753219+00:00
| 14 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: o(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n vector<int> neg;\n long int sum=0;\n int count=0;\n for(auto x:nums){\n if(x>0){\n sum=sum+x;\n count++;\n }\n else{\n neg.push_back(x);\n }\n }\n sort(neg.begin(),neg.end());\n reverse(neg.begin(),neg.end());\n for(auto x:neg){\n sum=sum+x;\n if(sum > 0){\n count++;\n }\n }\n return count;\n }\n};\n```
| 1 | 0 |
['Sorting', 'Prefix Sum', 'C++']
| 0 |
rearrange-array-to-maximize-prefix-score
|
📌📌 C++ || Sorting || Prefix Sum || Faster || Easy To Understand 🤷♂️🤷♂️
|
c-sorting-prefix-sum-faster-easy-to-unde-5u4f
|
Using Sorting && Prefix Sum\n\n Time Complexity :- O(NlogN)\n\n Space Complexity :- O(1)\n\n\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n
|
__KR_SHANU_IITG
|
NORMAL
|
2023-03-21T04:51:21.271729+00:00
|
2023-03-21T04:51:21.271758+00:00
| 53 | false |
* ***Using Sorting && Prefix Sum***\n\n* ***Time Complexity :- O(NlogN)***\n\n* ***Space Complexity :- O(1)***\n\n```\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n \n int n = nums.size();\n \n long long score = 0;\n \n int ans = 0;\n \n // sort the array in ascending order\n \n sort(nums.begin(), nums.end());\n \n // traverse over the nums from right side\n \n for(int i = n - 1; i >= 0; i--)\n {\n score += nums[i];\n \n if(score > 0)\n {\n ans++;\n }\n }\n \n return ans;\n }\n};\n```
| 1 | 0 |
['Greedy', 'C', 'Sorting', 'Prefix Sum', 'C++']
| 0 |
rearrange-array-to-maximize-prefix-score
|
✅C++ || Beginner Friendly || EASY CODE || SORTING
|
c-beginner-friendly-easy-code-sorting-by-d7ta
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
|
aman12345421
|
NORMAL
|
2023-03-18T17:10:15.837756+00:00
|
2023-03-18T17:11:50.995775+00:00
| 43 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(N)\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n long int sum=0;\n int n=nums.size(),count=0;\n sort(nums.rbegin(),nums.rend());\n for(int i=0;i<n;i++)\n {\n sum += nums[i];\n if(sum>0)\n {\n count++;\n }\n }\n return count;\n }\n};\n```
| 1 | 0 |
['C++']
| 0 |
rearrange-array-to-maximize-prefix-score
|
✅ Swift: Easy to Understand and Simple Solution
|
swift-easy-to-understand-and-simple-solu-oebt
|
\n\n# Code\n\nclass Solution {\n func maxScore(_ nums: [Int]) -> Int {\n let sorted = nums.sorted(by: >)\n var ans = 0\n var sum = 0\n
|
sohagkumarbiswas
|
NORMAL
|
2023-03-17T04:16:41.783726+00:00
|
2023-03-17T04:16:41.783769+00:00
| 28 | false |
\n\n# Code\n```\nclass Solution {\n func maxScore(_ nums: [Int]) -> Int {\n let sorted = nums.sorted(by: >)\n var ans = 0\n var sum = 0\n \n for i in 0..<sorted.count{\n sum += sorted[i]\n if sum > 0{\n ans += 1\n }\n }\n \n return ans\n }\n}\n\n```
| 1 | 0 |
['Swift']
| 0 |
rearrange-array-to-maximize-prefix-score
|
cpp sort and count || easy solution || no runtime error
|
cpp-sort-and-count-easy-solution-no-runt-6nkp
|
\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n reverse(nums.begin(),nums.end());\n i
|
khushisingh6661
|
NORMAL
|
2023-03-14T14:53:50.221357+00:00
|
2023-03-14T14:53:50.221402+00:00
| 15 | false |
```\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n reverse(nums.begin(),nums.end());\n int c=0;\n long long int ans=0; //use long long to avoid runtime error\n for(int i=0;i<nums.size();i++)\n {\n ans+=nums[i];\n if(ans>0)\n {\n c++;\n }\n }\n return c;\n \n }\n};\n```
| 1 | 0 |
['C++']
| 1 |
rearrange-array-to-maximize-prefix-score
|
greedy || sorting || very easy to understand || must see ||
|
greedy-sorting-very-easy-to-understand-m-25kx
|
Code\n\nstatic int fast_io = []() \n{ \n std::ios::sync_with_stdio(false); \n cin.tie(nullptr); \n cout.tie(nullptr); \n return 0; \n}();\n\n#ifdef
|
akshat0610
|
NORMAL
|
2023-03-14T14:15:33.867718+00:00
|
2023-03-14T14:15:33.867761+00:00
| 21 | false |
# Code\n```\nstatic int fast_io = []() \n{ \n std::ios::sync_with_stdio(false); \n cin.tie(nullptr); \n cout.tie(nullptr); \n return 0; \n}();\n\n#ifdef LOCAL\n freopen("input.txt", "r" , stdin);\n freopen("output.txt", "w", stdout);\n#endif\n\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) \n {\n //to count the numbe of positive integers in the nums\n int count = 0;\n\n //making the prefix vector to hold the final ans\n vector<long long int>prefix(nums.size()); \n\n //sorting the nums in greater<int>()\n sort(nums.begin(),nums.end(),greater<int>());\n\n if(nums[0] <= 0)\n return 0;\n\n count++;\n prefix[0] = nums[0];\n\n for(int i=1;i<nums.size();i++)\n {\n prefix[i] = prefix[i-1] + nums[i];\n if(prefix[i] > 0) count++;\n }\n return count;\n }\n};\n```
| 1 | 0 |
['Array', 'Greedy', 'Sorting', 'Prefix Sum', 'C++']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Simple JAVA Solution || 10 lines
|
simple-java-solution-10-lines-by-samyak_-buno
|
Approach\nWe can follow the Greedy approach, and sort the array. Then we will find the sum of elements till it becomes negative and the index before that will b
|
samyak_uttam
|
NORMAL
|
2023-03-13T12:30:53.630771+00:00
|
2023-03-13T12:30:53.630805+00:00
| 309 | false |
# Approach\nWe can follow the Greedy approach, and sort the array. Then we will find the sum of elements till it becomes negative and the index before that will be our answer.\n\n# Complexity\n- Time complexity: O(n logn)\n- Space complexity: O(1)\n\n# Code\n```\npublic int maxScore(int[] nums) {\n Arrays.sort(nums);\n long sum = 0;\n for(int i = nums.length - 1; i >= 0; i--) {\n sum += nums[i];\n if(sum <= 0)\n return nums.length - i - 1;\n }\n return nums.length;\n}\n```\nPlease upvote, if you found it useful.
| 1 | 0 |
['Greedy', 'Sorting', 'Java']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Sorting
|
sorting-by-energyperformer-rfkb
|
\n# Approach\n Describe your approach to solving the problem. \nWe just need to sort the array in Decsesing order after that \nWe will add all elements to the s
|
energyperformer
|
NORMAL
|
2023-03-12T18:18:37.722350+00:00
|
2023-03-12T18:18:37.722391+00:00
| 13 | false |
\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe just need to sort the array in Decsesing order after that \nWe will add all elements to the sum now we just need to check \nWhen the sum becomes 0 or less than zero after that there is no need to check beccause it can never become maximum soln \nwe return count\n\n\n# Code\n```\nclass Solution:\n def maxScore(self, nums: List[int]) -> int:\n nums.sort(reverse=True)\n sum=count=0\n for i in nums:\n sum+=i\n if(sum<=0):\n return count\n else:\n count+=1\n\n return count\n```
| 1 | 0 |
['Python3']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Python 1 line - Easy
|
python-1-line-easy-by-true-detective-nyyk
|
Intuition\n Describe your first thoughts on how to solve this problem. \nwe must first put the positive numbers then zeros then negative numbers to get the most
|
true-detective
|
NORMAL
|
2023-03-12T12:28:56.896369+00:00
|
2023-03-12T12:29:31.448181+00:00
| 310 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nwe must first put the positive numbers then zeros then negative numbers to get the most positives numbers in the accumulation\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nfirst reverse sort the nums\nthen accumulate\nthen count the number of positive ones\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(nlog(n))$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n\n# Code\n```\nclass Solution:\n def maxScore(self, nums: List[int]) -> int:\n return sum(x > 0 for x in accumulate(sorted(nums, reverse=True)))\n```
| 1 | 0 |
['Python3']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Sort | Easy C++ Solution
|
sort-easy-c-solution-by-haribhakt-yhzm
|
Intuition\nFor maximum no of prefixes to be positive :-\n- Large positive numbers should be at beginning(if any).\n- If Positive numbers are absent , then no po
|
HariBhakt
|
NORMAL
|
2023-03-12T12:13:47.024317+00:00
|
2023-03-12T12:13:47.024357+00:00
| 26 | false |
# Intuition\nFor maximum no of prefixes to be positive :-\n- Large positive numbers should be at beginning(if any).\n- If Positive numbers are absent , then no positive prefix exist.\n\n# Approach\n- Check if any postive number is present in vector, if not return 0.\n- Sort the vector in descending order and find the prefix sum of the vector.Increase the variable if prefix sum is positive.\n\n# Complexity\n- Time complexity: $$O(nlogn)$$ \n\n- Space complexity: $$O(n)$$ \n\n# Code\n```\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n int n = nums.size();\n int cnt = 0;\n for(int i=0;i<n;i++){\n if(nums[i] > 0) cnt++;\n }\n if(!cnt) return 0;\n sort(nums.begin(),nums.end(),greater<int>());\n vector<long long> prefix;\n long long sum = 0;\n for(int i=0;i<n;i++){\n sum += nums[i];\n prefix.push_back(sum);\n }\n cnt = 0;\n for(int i=0;i<n;i++){\n if(prefix[i] > 0) cnt++;\n }\n return cnt;\n }\n};\n```
| 1 | 0 |
['C++']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Easy PYTHON3 solution || O(nlogn)
|
easy-python3-solution-onlogn-by-uttudewa-0fts
|
Simple use the sort function or use the 2 pointer approach to bring all the positive elements of the array in the front and pushing all the negative elementsof
|
uttudewan100
|
NORMAL
|
2023-03-12T10:54:59.140964+00:00
|
2023-03-12T11:02:13.273347+00:00
| 92 | false |
Simple use the sort function or use the 2 pointer approach to bring all the positive elements of the array in the front and pushing all the negative elementsof the array to the back.\n\n```\ndef maxScore(self, nums: List[int]) -> int:\n \n nums.sort(reverse =True)\n \n prefix = [nums[0]]\n \n for i in range(1,len(nums)):\n prefix.append(prefix[-1] + nums[i])\n \n count = 0\n \n for i in prefix:\n if i > 0:\n count +=1\n \n return count\n```\n\nediting the solution a bit and decreasing one for loop:\n```\n\t\tnums.sort(reverse =True)\n \n\t\tprefix = [nums[0]]\n count = 0\n count +=1 if prefix[-1] > 0 else 0\n \n for i in range(1,len(nums)):\n prefix.append(prefix[-1] + nums[i])\n if prefix[-1] > 0:\n count +=1\n \n return count\n```
| 1 | 0 |
['Two Pointers', 'Sorting', 'Prefix Sum', 'Python', 'Python3']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Swift Solution in sorted array O(nlogn)
|
swift-solution-in-sorted-array-onlogn-by-wqlg
|
Code\n\nclass Solution {\n func maxScore(_ nums: [Int]) -> Int {\n let nums = nums.sorted(by: >)\n var subsum = 0\n var res = 0\n\n
|
AndreevIVdev
|
NORMAL
|
2023-03-12T09:26:01.015431+00:00
|
2023-03-12T09:26:01.015477+00:00
| 24 | false |
# Code\n```\nclass Solution {\n func maxScore(_ nums: [Int]) -> Int {\n let nums = nums.sorted(by: >)\n var subsum = 0\n var res = 0\n\n for num in nums {\n subsum += num\n if subsum > 0 {\n res += 1\n } else {\n break\n }\n }\n\n return res\n }\n}\n```
| 1 | 0 |
['Swift']
| 0 |
rearrange-array-to-maximize-prefix-score
|
[Python] Count the number of negative numbers and zeros; Explained.
|
python-count-the-number-of-negative-numb-jjg7
|
We can count the number of negative numbers and zeros in the list.\n\nIf the sum of all the positive numbers is larger than 0 (i.e., we have positive prefix sum
|
wangw1025
|
NORMAL
|
2023-03-12T06:09:33.840144+00:00
|
2023-03-12T06:10:17.140570+00:00
| 236 | false |
We can count the number of negative numbers and zeros in the list.\n\nIf the sum of all the positive numbers is larger than 0 (i.e., we have positive prefix sum), we can first append zeros to the arranged list starting with all the positive numbers. After that, we append the negative numbers from the smallest so that we can get the longest prefix sum list.\n\n```\nclass Solution:\n def maxScore(self, nums: List[int]) -> int:\n negative_nums = []\n score, psum, zeros = 0, 0, 0\n for num in nums:\n if num > 0:\n score += 1\n psum += num\n elif num == 0:\n zeros += 1\n else:\n heapq.heappush(negative_nums, -num)\n \n if psum > 0:\n score += zeros\n \n while negative_nums:\n n = heapq.heappop(negative_nums)\n psum = psum - n\n if psum > 0:\n score += 1\n else:\n break\n return score\n```
| 1 | 0 |
['Heap (Priority Queue)', 'Python3']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Best Java Approach
|
best-java-approach-by-k_surya_teja-sn07
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
|
k_surya_teja
|
NORMAL
|
2023-03-12T05:38:14.773058+00:00
|
2023-03-12T05:38:14.773087+00:00
| 10 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ --> O(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n0(1)\n\n# Code\n```\nclass Solution {\n public int maxScore(int[] nums) {\n Arrays.sort(nums);\n int n = nums.length;\n long sum = 0;\n int ans = 0;\n for(int i=n-1;i>=0;i--){\n sum+=nums[i];\n if(sum>0){\n ans++;\n }\n else{\n break;\n }\n }\n return ans;\n }\n}\n```
| 1 | 0 |
['Java']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Simple code|| sorting
|
simple-code-sorting-by-r_aghav-0wan
|
Connect with me on LinkedIn https://www.linkedin.com/in/raghav-upadhyay-80336b229/\n\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n
|
R_aghav
|
NORMAL
|
2023-03-12T05:08:48.200496+00:00
|
2023-03-12T05:08:48.200538+00:00
| 9 | false |
**Connect with me on LinkedIn https://www.linkedin.com/in/raghav-upadhyay-80336b229/**\n```\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n sort(nums.begin(),nums.end(),greater<int>());\n long long val=0,tot=0;\n for(auto i:nums){\n val+=i;\n if(val>0) tot++;\n else break;\n }\n return tot;\n }\n};\n```\n# Don\'t forgot to Upvote\u2B06\uFE0F
| 1 | 0 |
['C++']
| 0 |
rearrange-array-to-maximize-prefix-score
|
easy java solution
|
easy-java-solution-by-sandip93291-vc49
|
\n\n# Approach\nsort the array and start from last index add that value in sum if sum is greater than 0 then count++ else break\n Describe your approach to solv
|
sandip93291
|
NORMAL
|
2023-03-12T04:51:50.519887+00:00
|
2023-03-14T06:14:26.149454+00:00
| 206 | false |
\n\n# Approach\nsort the array and start from last index add that value in sum if sum is greater than 0 then count++ else break\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: NlogN+N\n NlogN - > sorting Array\n N -> for loop\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int maxScore(int[] nums) {\n \n int ans=0;\n long sum=0;\n Arrays.sort(nums);\n \n for(int i=nums.length-1;i>=0;i--)\n {\n sum+=nums[i];\n if(sum>0)\n {\n ans++;\n }\n else{\n break;\n }\n \n }\n return ans;\n }\n}\n```\n\n\uD83D\uDC46 vote
| 1 | 0 |
['Java']
| 1 |
rearrange-array-to-maximize-prefix-score
|
C++ Easy Solution || Easy to understand ✅✅
|
c-easy-solution-easy-to-understand-by-sh-5kjk
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
|
Shubhamjain287
|
NORMAL
|
2023-03-12T04:48:19.371689+00:00
|
2023-03-12T04:48:19.371731+00:00
| 26 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n \n sort(begin(nums),end(nums),greater<>());\n \n long long sum = 0, res = 0;\n \n for(int i=0; i<nums.size(); i++){\n sum += nums[i];\n if(sum > 0)\n res++;\n else\n break;\n }\n \n return res;\n }\n};\n```
| 1 | 0 |
['C++']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Fastest and easiest java solution
|
fastest-and-easiest-java-solution-by-cod-cew7
|
If you like then upvote it and if you don\'t understand then comment it.\nTime complexity = O(nlogn) for sorting\nSpace complexity = O(1) because we didn\'t use
|
codeHunter01
|
NORMAL
|
2023-03-12T04:41:27.265324+00:00
|
2023-03-12T17:19:15.896830+00:00
| 35 | false |
If you like then upvote it and if you don\'t understand then comment it.\nTime complexity = O(nlogn) for sorting\nSpace complexity = O(1) because we didn\'t use any extra space.\n\n```\n \nclass Solution {\n void swap(int[] nums, int i, int j)\n {\n int temp = nums[i];\n nums[i]= nums[j];\n nums[j]=temp;\n }\n public int maxScore(int[] nums) {\n Arrays.sort(nums);\n int n = nums.length;\n int i=0,j=n-1;\n //Reversing the sorted array\n while(i<j)\n {\n swap(nums,i,j);\n i++;\n j--;\n }\n \n int count=0;\n // Making prefixSum long for managing overflow situation\n long prefixSum = 0;\n for(i=0;i<nums.length;i++)\n {\n prefixSum += nums[i];\n if(prefixSum>0)\n count++;\n\n }\n return count;\n \n }\n}\n```
| 1 | 0 |
['Java']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Best Approach using sorting | JAVA | TC: O(nlogn) SC: O(1)
|
best-approach-using-sorting-java-tc-onlo-9p2l
|
Intuition\n Describe your first thoughts on how to solve this problem. \nGroup all the positive numbers together and negative numbers.\nSorting helps to do that
|
CHANDRA_MOULIESH_N
|
NORMAL
|
2023-03-12T04:27:41.263079+00:00
|
2023-03-12T04:31:54.015037+00:00
| 11 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nGroup all the positive numbers together and negative numbers.\nSorting helps to do that.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nLet\'s say the given arrray is [-1 2 3 -2 -3 -1]. You can group all the positive and negative numbers together without sorting it (using two pointer). But, it might gives minimum prefix sum for some cases. \n\nEg:Grouping +ve and -ve numbers in the [-1 2 3 -2 -3 -1]\n-> *+ve numbers on left and -ve numbers on right side. \n **[2 3 -1 -2 -3 -1]**, Here the array is not sorted. If you take prefix sum, then the resultant array will looks like **[2 5 4 2 -1 -2]**. The number of +ve elements are ***4****.\n\nCan we increase the positive elements count from 4?\n\n-> Sort the [-1 2 3 -2 -3 -1], Sorted array= [-3 -2 -1 -1 2 3]. \n now take the suffix sum of the sorted array, you will get **[-2 1 3 4 5 3]**, the +ve element count is increased from 4 to 5. The O/P is 5.\n\nInstead of taking suffix sum, use the carry forward technique.\n \n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ --> O(nlogn)+O(n) => O(nlogn)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\n\n# Code\n```\nclass Solution {\n public int maxScore(int[] nums) {\n \n Arrays.sort(nums);\n\n int n=nums.length;\n \n int count=0;\n long sum=0;\n\n for(int i=n-1; i>=0; i--){\n sum = sum+nums[i];\n if(sum>0) count++;\n else break;\n }\n \n return count;\n \n }\n}\n```
| 0 | 0 |
['Array', 'Suffix Array', 'Sorting', 'Counting', 'Java']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Simple c++ soln || sort fn || prefix sum
|
simple-c-soln-sort-fn-prefix-sum-by-akas-2lju
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nusing sort function and
|
akashkr11668
|
NORMAL
|
2023-03-12T04:17:23.120845+00:00
|
2023-03-12T04:17:23.120877+00:00
| 26 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nusing sort function and iterating aproach \n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(nlogn)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\n\n# Code\n```\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n sort(nums.rbegin(), nums.rend());\n long long sum = 0;\n int score = 0;\n for (int i = 0; i < nums.size(); i++) {\n sum += nums[i];\n if (sum > 0) {\n score++;\n }\n }\n return score;\n }\n};\n\n```
| 1 | 0 |
['Greedy', 'Sorting', 'Prefix Sum', 'C++']
| 0 |
rearrange-array-to-maximize-prefix-score
|
4 line easy Java solution
|
4-line-easy-java-solution-by-yaduveers02-365j
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nFirst sort array and then doing prefix sum from end of array, as i have s
|
yaduveers02
|
NORMAL
|
2023-03-12T04:15:37.981549+00:00
|
2023-03-12T04:19:43.119765+00:00
| 167 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nFirst sort array and then doing prefix sum from end of array, as i have sorted array in ascending order. While taking sum also count number of prefix sum which are greater than 0.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int maxScore(int[] nums) {\n <!-- sorting array -->\n Arrays.sort(nums);\n <!-- counting and taking sum from end of list-->\n int count=0;\n long sum=0;\n for(int i=nums.length-1; i>=0; i--){\n sum+=nums[i];\n if(sum>0) count++;\n }\n return count;\n }\n}\n```
| 1 | 0 |
['Java']
| 1 |
rearrange-array-to-maximize-prefix-score
|
Kotlin Greedy
|
kotlin-greedy-by-kotlinc-cs6s
|
\n class Solution {\n fun maxScore(nums: IntArray): Int {\n nums.sortDescending()\n\n var sum = 0L\n var res = 0\n for (v in nums) {\n
|
kotlinc
|
NORMAL
|
2023-03-12T04:14:50.302239+00:00
|
2023-03-12T04:14:50.302273+00:00
| 21 | false |
```\n class Solution {\n fun maxScore(nums: IntArray): Int {\n nums.sortDescending()\n\n var sum = 0L\n var res = 0\n for (v in nums) {\n sum += v\n \n if (sum > 0L)\n res++\n }\n return res\n }\n }\n\n```
| 1 | 0 |
['Kotlin']
| 1 |
rearrange-array-to-maximize-prefix-score
|
Simple python3 O(nlog(n)) solution
|
simple-python3-onlogn-solution-by-sbpark-2e31
|
\n# Code\n\nclass Solution:\n def maxScore(self, nums: List[int]) -> int:\n nums.sort(reverse=True)\n s = 0\n for i, n in enumerate(nums
|
sbpark0611
|
NORMAL
|
2023-03-12T04:14:41.254699+00:00
|
2023-03-12T05:41:58.906820+00:00
| 228 | false |
\n# Code\n```\nclass Solution:\n def maxScore(self, nums: List[int]) -> int:\n nums.sort(reverse=True)\n s = 0\n for i, n in enumerate(nums):\n s += n\n if s <= 0:\n return i\n return len(nums)\n \n```\n\n# Complexity\n- Time complexity: O(nlog(n))\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n
| 1 | 0 |
['Python3']
| 1 |
rearrange-array-to-maximize-prefix-score
|
EAsiest Solution || C++
|
easiest-solution-c-by-asthanaman_89-7bpw
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
|
Asthanaman_89
|
NORMAL
|
2023-03-12T04:04:47.283918+00:00
|
2023-03-12T04:04:47.283952+00:00
| 31 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n sort(nums.begin(),nums.end(),greater<int>());\n int n=nums.size();\n vector<long long>pre(n);\n int ans=0;\n pre[0]=nums[0];\n if(pre[0]>0) ans++;\n for(int i=1;i<n;i++){\n pre[i]=pre[i-1]+nums[i];\n if(pre[i]>0) ans++;\n }\n return ans;\n }\n};\n```
| 1 | 0 |
['C++']
| 0 |
rearrange-array-to-maximize-prefix-score
|
Sort Descending, and create Prefix. Check positive count.
|
sort-descending-and-create-prefix-check-6khdt
|
\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n sort(nums.begin(), nums.end(), greater<int>());\n long long positiveSum = 0,
|
parth511
|
NORMAL
|
2023-03-12T04:03:21.509601+00:00
|
2023-03-12T04:04:17.877670+00:00
| 18 | false |
```\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n sort(nums.begin(), nums.end(), greater<int>());\n long long positiveSum = 0, ans = 0, n = nums.size();\n vector<long long> prefSum(n);\n prefSum[0] = nums[0];\n for(int i = 1; i < n; ++i) prefSum[i] = prefSum[i - 1] + nums[i];\n for(int i = 0; i < n; ++i){\n if(prefSum[i] <= 0) return i;\n ans++;\n }\n return ans;\n }\n};\n```\n\n\nCan also instead use single variable to keep track of prefix Sum, will reduce the space complexity to O(1)
| 1 | 0 |
['Sorting', 'Simulation', 'Prefix Sum', 'C++']
| 0 |
rearrange-array-to-maximize-prefix-score
|
c++ solution | sorting | prefix | java
|
c-solution-sorting-prefix-java-by-mandli-8dqe
|
Code\n\nclass Solution {\npublic:\n static bool cmp(int a,int b){\n return a>b;\n }\n int maxScore(vector<int>& nums) {\n sort(nums.begin(
|
mandliyarajendra11
|
NORMAL
|
2023-03-12T04:03:01.175135+00:00
|
2023-03-12T04:03:01.175162+00:00
| 121 | false |
# Code\n```\nclass Solution {\npublic:\n static bool cmp(int a,int b){\n return a>b;\n }\n int maxScore(vector<int>& nums) {\n sort(nums.begin(),nums.end(),cmp);\n \n if(nums[nums.size()-1]>0)return nums.size();\n \n int ans=0;\n long long int prefix=0;\n for(auto i:nums){\n prefix+=i;\n if(prefix>0)ans++;\n } \n \n return ans;\n }\n};\n```
| 1 | 0 |
['Greedy', 'Prefix Sum', 'C++', 'Java']
| 0 |
rearrange-array-to-maximize-prefix-score
|
🚀✅ || Steps explained || Beginner friendly || Clean code
|
steps-explained-beginner-friendly-clean-em4io
|
Intuition\n Describe your first thoughts on how to solve this problem. \nAs given in the question, we need to find no of positives after performing prefix sum.\
|
aeroabrar_31
|
NORMAL
|
2023-03-12T04:02:31.422624+00:00
|
2023-03-13T10:02:53.369727+00:00
| 146 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAs given in the question, we need to find no of positives after performing prefix sum.\nWe can also do this without using prefix array but, as we are performing prefix sum they are the chances of integer overflow so we need to declare prefix array with long datatype.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n1. Sort the Array nums\n2. Reverse the Array nums\n3. Prefix sum\n4. Count no. of positives\n\n# Complexity\n- Time complexity: $$O(N*logN)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n\n- Space complexity: $$O(N)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n\n\n\n# Code\n```\nclass Solution {\n public int maxScore(int[] nums) {\n \n Arrays.sort(nums);// 1. Sort array\n\n int n=nums.length;\n int lastindex=n-1;\n for(int i=0;i<n/2;i++)//2. Reverse array\n {\n int t= nums[i];\n nums[i]=nums[lastindex];\n nums[lastindex]=t;\n lastindex--;\n }\n \n long[] prefarray=new long[nums.length];// declaring with long datatype\n prefarray[0]=nums[0];\n \n for(int i=1;i<nums.length;i++)//3. performing prefix sum\n {\n prefarray[i]=prefarray[i-1]+nums[i];\n }\n int count=0;\n \n //System.out.println(Arrays.toString(prefarray));\n \n for(long i:prefarray)//4. counting no. of positives\n if(i>0)\n count++;\n\n return count;\n \n }\n}\n```\n
| 1 | 0 |
['Prefix Sum', 'Java']
| 2 |
rearrange-array-to-maximize-prefix-score
|
Java Accepted Solution | Sorting O(NlogN)
|
java-accepted-solution-sorting-onlogn-by-i7r8
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
|
manishk4514
|
NORMAL
|
2023-03-12T04:02:31.191236+00:00
|
2023-03-12T04:02:31.191292+00:00
| 421 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int maxScore(int[] nums) {\n Arrays.sort(nums);\n int n = nums.length;\n long[] prefix = new long[n];\n long sum = 0;\n int ans = 0;\n for(int i = n - 1; i >= 0; i--){\n sum += nums[i];\n prefix[i] = sum;\n if(prefix[i] > 0) ans++;\n }\n return ans;\n }\n}\n```
| 1 | 0 |
['Java']
| 1 |
rearrange-array-to-maximize-prefix-score
|
simple c++ solution
|
simple-c-solution-by-yashpadiyar4-wgp9
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
|
yashpadiyar4
|
NORMAL
|
2023-03-12T04:02:04.478481+00:00
|
2023-03-12T04:02:38.847532+00:00
| 25 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int mod=1e9+7;\n int maxScore(vector<int>& nums1) {\n \n int cnt=0;\n int n=nums1.size();\n sort(nums1.begin(),nums1.end());\n reverse(nums1.begin(),nums1.end());\n vector<long long>nums(n+1);\n nums[0]=nums1[0];\n for(long long i=1;i<n;i++){\n nums[i]=(nums[i-1]+nums1[i]);\n }\n for(long long i=0;i<n;i++){\n if(nums[i]>0)cnt++;\n }\n return cnt;\n \n \n \n }\n};\n```
| 1 | 0 |
['C++']
| 0 |
rearrange-array-to-maximize-prefix-score
|
✅ Easiest Java Solution 🔥Step By Step || Deatailed Expalined ✅
|
easiest-java-solution-step-by-step-deata-66db
|
Intuition\nSince question is to count the maximum number of score you can achieve . \nAnd the score of nums is the number of positive integers in the array pref
|
KumarNishantGunjan
|
NORMAL
|
2023-03-12T04:02:00.926627+00:00
|
2023-03-12T04:02:00.926670+00:00
| 89 | false |
# Intuition\nSince question is to count the maximum number of score you can achieve . \nAnd the score of nums is the number of positive integers in the array prefix and prefix is the array containing the prefix sums of nums after rearranging it.\n In other words, prefix[i] is the sum of the elements from 0 to i in nums after rearranging it.\n\n In order to maximize the score we need to keep all the positive element continuously. **We can do it by sorting the given array.**\n Here no need to take extra space for prefix sum, since we just have to count the number of positive sum we can store it in variable and carry through the whole array.\n\n**One more catch** is that we need to take sum variable long because of the constraint given ***(-106 <= nums[i] <= 106)*** . If we will store sum in integer varaible it might give you wrong sum due to integer overflow.\n Then iterate throught the array, add arr[i] to sum, If at the current index sum>0 add 1 to answer.\n\n# Approach\n> Step 1: Sort the given array.\n\n**You can sort it in descending order as well but I have sorted int ascending order.**\n\n> Step 2: Intialize a variable sum(long) = 0;\n\n> Step 3: Intialize a variable max(int) = 0, in which we will store answer.\n\n> step 4: Traverse the array from right to left (if array is sorted in ascending order) and left to right (if array is sorted in descending order), add current index element to sum and check if it is greater than 0, if it is add 1 to answer.\n\n> step 5: Return answer.\n# Complexity\n- Time complexity: $$O(n)$$ \n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$ \n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int maxScore(int[] nums) {\n\n Arrays.sort(nums);\n long sum=0;\n int ans=0;\n int n=nums.length-1;\n \n for(int i=n;i>=0;i--){\n sum+=nums[i];\n if(sum>0)ans++;\n }\n\n return ans;\n }\n}\n```\n``` []\nThanks for viewing the solution.\nHope it helps, if it does, please leave a comment below \uD83D\uDE4F\nAnd do consider UPVOTING \u2B06\uFE0F Because it motivates me,\nwriting such solution.\n```\n \n
| 1 | 0 |
['Array', 'Sorting', 'Prefix Sum', 'Java']
| 1 |
rearrange-array-to-maximize-prefix-score
|
C++ || Sort then count
|
c-sort-then-count-by-up1512001-u39k
|
\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n sort(nums.begin(),nums.end(),[](int a,int b){\n return a>b; \n });\
|
up1512001
|
NORMAL
|
2023-03-12T04:01:45.974053+00:00
|
2023-03-12T04:01:45.974101+00:00
| 76 | false |
```\nclass Solution {\npublic:\n int maxScore(vector<int>& nums) {\n sort(nums.begin(),nums.end(),[](int a,int b){\n return a>b; \n });\n long long ans=0,cnt=0;\n for(int i=0;i<nums.size();i++){\n ans += nums[i];\n if(ans > 0) cnt+=1;\n }\n return cnt;\n }\n};\n```
| 1 | 0 |
['C', 'Sorting', 'C++']
| 0 |
rearrange-array-to-maximize-prefix-score
|
💯✅JAVA | SUPER EASY SOLUTION
|
java-super-easy-solution-by-dipesh_12-apzb
|
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
|
dipesh_12
|
NORMAL
|
2023-03-12T04:01:28.381844+00:00
|
2023-03-12T04:01:28.381875+00:00
| 153 | false |
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nimport java.util.*;\n\nclass Solution {\n public int maxScore(int[] nums) {\n desc(nums);\n int n=nums.length;\n long[]pre=new long[n];\n int maxScore=0;\n pre[0]=nums[0];\n if(pre[0]>0) maxScore++;\n for(int i=1;i<n;i++){\n pre[i]=pre[i-1]+nums[i];\n if(pre[i]>0){\n maxScore++;\n }\n }\n return maxScore;\n }\n \n public void desc(int[]nums){\n Arrays.sort(nums);\n int n=nums.length;\n for(int i=0;i<n/2;i++){\n int temp=nums[i];\n nums[i]=nums[n-i-1];\n nums[n-1-i]=temp;\n }\n }\n}\n```
| 1 | 0 |
['Java']
| 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.