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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
find-the-child-who-has-the-ball-after-k-seconds | java sol | java-sol-by-rrrautela-s0a2 | Code | rrrautela | NORMAL | 2025-02-10T03:51:59.575359+00:00 | 2025-02-10T03:51:59.575359+00:00 | 7 | false |
# Code
```java []
class Solution {
public int numberOfChild(int n, int k) {
int x = k;
if(k>=n){
x = k % (2*n - 2);
if(x>=n){
x = n-1 - (x%(n-1));
// x = x - x%(n-1) * 2;
}
}
return x;
}
}
``` | 0 | 0 | ['Java'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | 3178. Find the Child Who Has the Ball After K Seconds | 3178-find-the-child-who-has-the-ball-aft-cleo | IntuitionHard one to solve through optimal solution!ApproachFound internal logic of changing direction!Complexity
Time complexity: O(1)
Space complexity: O(1) | SPD-LEGEND | NORMAL | 2025-01-27T16:15:26.535991+00:00 | 2025-01-27T16:15:26.535991+00:00 | 6 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Hard one to solve through optimal solution!
# Approach
<!-- Describe your approach to solving the problem. -->
Found internal logic of changing direction!
# Complexity
- Time complexity: O(1)
<!-- Add your time complexity here, e.g. $$O(n)$... | 0 | 0 | ['Java'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | Array | Simple solution with 100% beaten | array-simple-solution-with-100-beaten-by-4n0v | IntuitionTo solve this problem, we can simulate the movement of the ball. Initially, the ball starts at child 0, and it moves in the right direction. When the b | Takaaki_Morofushi | NORMAL | 2025-01-27T03:39:37.917322+00:00 | 2025-01-27T03:39:37.917322+00:00 | 3 | false | # Intuition
To solve this problem, we can simulate the movement of the ball. Initially, the ball starts at child 0, and it moves in the right direction. When the ball reaches either end, it reverses direction. We need to compute the final child after `k` seconds of this movement.
# Approach
1. Initialize the ball's po... | 0 | 0 | ['C++'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | begineer friendly || 100% beat | begineer-friendly-100-beat-by-gyaneshkr-lg5h | IntuitionApproachComplexity
Time complexity:
O(n)
Space complexity:
O(1)
Code | gyaneshkr | NORMAL | 2025-01-26T16:43:48.351999+00:00 | 2025-01-26T16:43:48.351999+00:00 | 7 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
O(n)
- Space complexity:
O(1)
# Code
```java []
class Solution {
public int numberOfChild(int n, int k) {
int dir=1; // 1->ri... | 0 | 0 | ['Java'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | numberOfChild: beautiful O(1) solution. BEAT 100%. ✅ | numberofchild-beautiful-o1-solution-beat-56vx | IntuitionTo determine which child will have the ball after k seconds, we need to consider the direction changes when the ball reaches the ends of the line. The | rTIvQYSHLp | NORMAL | 2025-01-23T20:14:14.307652+00:00 | 2025-01-23T20:14:14.307652+00:00 | 3 | false | # Intuition
To determine which child will have the ball after `k` seconds, we need to consider the direction changes when the ball reaches the ends of the line. The ball will move back and forth between the children based on the number of passes.
# Approach
1. Check if the quotient of \(k\) divided by \(n-1\) is even ... | 0 | 0 | ['Math', 'C'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | FORMULA BASED FULL EXPLAINATION | formula-based-full-explaination-by-prach-0l2x | IntuitionFor n students from 0 to n-1 , then from last to 1st notice a cycle !Hence ball goes in cycle be just get the effective shift in its initial Pos and de | PrachiKumari | NORMAL | 2025-01-18T11:19:33.320687+00:00 | 2025-01-18T11:19:33.320687+00:00 | 4 | false | # Intuition
For n students from 0 to n-1 , then from last to 1st notice a cycle !
Hence ball goes in cycle be just get the effective shift in its initial Pos and determine its Directions of motion either` L TO R `or `R TO L` .
# Approach
- Total cyclic shifts 0 to n-1 and back to 0th
(Take is as n vertices and n-1 ed... | 0 | 0 | ['Java'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | FORMULA BASED FULL EXPLAINATION | formula-based-full-explaination-by-prach-06xh | IntuitionFor n students from 0 to n-1 , then from last to 1st notice a cycle !Hence ball goes in cycle be just get the effective shift in its initial Pos and de | PrachiKumari | NORMAL | 2025-01-18T11:19:30.721910+00:00 | 2025-01-18T11:19:30.721910+00:00 | 3 | false | # Intuition
For n students from 0 to n-1 , then from last to 1st notice a cycle !
Hence ball goes in cycle be just get the effective shift in its initial Pos and determine its Directions of motion either` L TO R `or `R TO L` .
# Approach
- Total cyclic shifts 0 to n-1 and back to 0th
(Take is as n vertices and n-1 ed... | 0 | 0 | ['Java'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | FORMULA BASED FULL EXPLAINATION | formula-based-full-explaination-by-prach-joaq | IntuitionFor n students from 0 to n-1 , then from last to 1st notice a cycle !Hence ball goes in cycle be just get the effective shift in its initial Pos and de | PrachiKumari | NORMAL | 2025-01-18T11:19:29.238727+00:00 | 2025-01-18T11:19:29.238727+00:00 | 0 | false | # Intuition
For n students from 0 to n-1 , then from last to 1st notice a cycle !
Hence ball goes in cycle be just get the effective shift in its initial Pos and determine its Directions of motion either` L TO R `or `R TO L` .
# Approach
- Total cyclic shifts 0 to n-1 and back to 0th
(Take is as n vertices and n-1 ed... | 0 | 0 | ['Java'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | Easy Solution | easy-solution-by-choudhariaditya-vg2b | Code | choudhariaditya | NORMAL | 2025-01-14T17:53:31.605804+00:00 | 2025-01-14T17:53:31.605804+00:00 | 6 | false |
# Code
```java []
class Solution {
public int numberOfChild(int n, int k) {
int i = 0;
while (k > 0) {
while (k > 0 && i < n - 1) {
i++;
k--;
}
if (k == 0) {
return i;
}
while (k > 0 && i >... | 0 | 0 | ['Math', 'Simulation', 'Java'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | reduce by round/single side move | reduce-by-roundsingle-side-move-by-linda-8xg5 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | linda2024 | NORMAL | 2025-01-10T22:42:45.831434+00:00 | 2025-01-10T22:42:45.831434+00:00 | 8 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['C#'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | 2 line | 2-line-by-sanzenin_aria-rit3 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | sanzenin_aria | NORMAL | 2025-01-08T13:06:19.396368+00:00 | 2025-01-08T13:06:19.396368+00:00 | 6 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['Python3'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | 3178. Find the Child Who Has the Ball After K Seconds | 3178-find-the-child-who-has-the-ball-aft-0zyw | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | G8xd0QPqTy | NORMAL | 2025-01-07T07:41:38.293212+00:00 | 2025-01-07T07:41:38.293212+00:00 | 3 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['C++'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | Java Best Solution 0ms | java-best-solution-0ms-by-trevorkmcintyr-82wr | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | trevorkmcintyre | NORMAL | 2025-01-07T05:01:33.551283+00:00 | 2025-01-07T05:01:33.551283+00:00 | 3 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['Java'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | Simplest Approach: Beats 100% | simplest-approach-beats-100-by-kajal_2-icd6 | IntuitionApproachComplexity
Time complexity: O(k)
Space complexity: O(1)
Code | Kajal_2 | NORMAL | 2025-01-03T10:03:02.697490+00:00 | 2025-01-03T10:03:02.697490+00:00 | 3 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity: O(k)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->... | 0 | 0 | ['C++'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | 3178. Find the Child Who Has the Ball After K Seconds | 3178-find-the-child-who-has-the-ball-aft-6v84 | Time complexity: O(1)Space complexity: O(1)Code | Adarshjha | NORMAL | 2024-12-31T11:17:56.733855+00:00 | 2024-12-31T11:17:56.733855+00:00 | 3 | false |
# Time complexity: O(1)
# Space complexity: O(1)
# Code
```c []
int numberOfChild(int n, int k) {
int quo, rem, n2 = n - 1;
quo = k / n2;
rem = k % n2;
if(quo % 2){
return (n2-rem);
}
else{
return rem;
}
}
``` | 0 | 0 | ['C'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | Find the Child Who Has the Ball After K Seconds - Simplified Solution | find-the-child-who-has-the-ball-after-k-14kgo | Problem OverviewWe have n children standing in a line, and a ball is passed between them every second. The ball starts with child 0 and moves to the right. When | JAHAGANAPATHI_SUGUMAR | NORMAL | 2024-12-29T13:27:15.317656+00:00 | 2024-12-29T13:27:15.317656+00:00 | 3 | false | # Problem Overview
We have `n` children standing in a line, and a ball is passed between them every second. The ball starts with child `0` and moves to the right. When the ball reaches either end of the line, the passing direction reverses. The task is to determine which child has the ball after `k` seconds.
# Int... | 0 | 0 | ['Math', 'Simulation', 'Java'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | One Line | one-line-by-khaled-alomari-i948 | null | khaled-alomari | NORMAL | 2024-12-25T22:02:00.371742+00:00 | 2024-12-25T22:02:00.371742+00:00 | 13 | false | # Complexity
- Time complexity:
$$O(1)$$
- Space complexity:
$$O(1)$$
# Code
```javascript []
const numberOfChild = (n, k) => (k %= n * 2 - 2) && k > n - 1 && 2 * n - 2 - k || k;
```
```typescript []
const numberOfChild = (n: number, k: number): number => (k %= n * 2 - 2) && k > n - 1 && 2 * n - 2 - k || k;
``` | 0 | 0 | ['Math', 'TypeScript', 'JavaScript'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | Simple implementation in cpp without any math | simple-implementation-in-cpp-without-any-sec7 | IntuitionApproachComplexity
Time complexity:
O(k)
Space complexity:
O(1)
Code | aditya821 | NORMAL | 2024-12-23T17:05:04.713879+00:00 | 2024-12-23T17:05:04.713879+00:00 | 2 | false | # Intuition
# Approach
# Complexity
- Time complexity:
O(k)
- Space complexity:
O(1)
# Code
```cpp []
class Solution {
public:
int numberOfChild(int n, int k) {
bool t=true;
int i=-1,cnt=-1;
while(cnt!=k){
if(i==n-1&&t==true){
t=!t;
}
if(i==0... | 0 | 0 | ['C++'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | simple solution | simple-solution-by-owenwu4-2uqy | Intuitionsimulate the process - this time, starting at 0, using a boolean flag to determine the upper and lower boundApproachComplexity
Time complexity:
Space | owenwu4 | NORMAL | 2024-12-22T15:03:59.372616+00:00 | 2024-12-22T15:03:59.372616+00:00 | 6 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
simulate the process - this time, starting at 0, using a boolean flag to determine the upper and lower bound
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity ... | 0 | 0 | ['Python3'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | solution | solution-by-ester45764-w9at | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | ester45764 | NORMAL | 2024-12-20T18:38:14.946865+00:00 | 2024-12-20T18:38:14.946865+00:00 | 3 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['Python3'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | Revert Approach Solution | revert-approach-solution-by-sohan3das-rbyh | IntuitionI was thinking that how can i revert when i reach either of the end then i got this approach.Approach -> Revert ApproachComplexity
Time complexity: O(K | sohan3das | NORMAL | 2024-12-18T04:28:46.026454+00:00 | 2024-12-18T04:28:46.026454+00:00 | 5 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI was thinking that how can i revert when i reach either of the end then i got this approach.\n# Approach -> Revert Approach\n<!-- Describe your approach to solving the problem. -->\n\n\n# Complexity\n- Time complexity: O(K)\n<!-- Add you... | 0 | 0 | ['Java'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | ✅🐍 Python simulation 🐍✅ | python-simulation-by-ninise-fwc9 | Code | Ninise | NORMAL | 2024-12-16T18:18:24.665677+00:00 | 2024-12-16T18:18:24.665677+00:00 | 5 | false | \n# Code\n```python3 []\nclass Solution:\n def numberOfChild(self, n: int, k: int) -> int:\n i = 0\n direction = True\n while k > 0:\n if direction and i == n - 1:\n direction = False\n i -= 1\n elif not direction and i == 0:\n d... | 0 | 0 | ['Simulation', 'Python3'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | Simple Maths Solution C++ Beats 100% | simple-maths-solution-c-beats-100-by-tan-w7mx | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | tanishqj2005 | NORMAL | 2024-12-14T09:01:52.967243+00:00 | 2024-12-14T09:01:52.967243+00:00 | 4 | 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)$$ --... | 0 | 0 | ['C++'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | Java Solution || Beats 100% | java-solution-beats-100-by-yashkesharwan-lr8l | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | yashkesharwani559 | NORMAL | 2024-12-13T16:02:11.192141+00:00 | 2024-12-13T16:02:11.192141+00:00 | 3 | 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)$$ --... | 0 | 0 | ['Java'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | 2 line code cpp || 100% time complexity | 2-line-code-cpp-100-time-complexity-by-p-kfs1 | 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 | Prashant0100 | NORMAL | 2024-12-08T07:08:38.768767+00:00 | 2024-12-08T07:08:38.768797+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)$$ --... | 0 | 0 | ['C++'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | One Line Solution: Time : O(1) & Space : O(1) | one-line-solution-time-o1-space-o1-by-bh-nflu | Intuition\nThe problem can be thought of as simulating the movement of the ball between the children while alternating directions when it reaches either end. In | bhardwajsameer7 | NORMAL | 2024-12-05T20:07:00.606041+00:00 | 2024-12-05T20:07:00.606073+00:00 | 1 | false | # Intuition\nThe problem can be thought of as simulating the movement of the ball between the children while alternating directions when it reaches either end. Instead of simulating every step explicitly, the key is recognizing the repetitive nature of the sequence.\n\nThe ball oscillates between the children in a pred... | 0 | 0 | ['Java'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | Video Explanation Solution easy to understand | Programming with Pirai | Half Moon Coder. | video-explanation-solution-easy-to-under-zifu | Video : https://youtu.be/lc5IuVczpNI\n\n# Code\npython3 []\nclass Solution:\n def numberOfChild(self, n: int, k: int) -> int:\n pos=0\n directi | Piraisudan_02 | NORMAL | 2024-12-05T16:24:39.761824+00:00 | 2024-12-05T16:24:39.761848+00:00 | 2 | false | # Video : https://youtu.be/lc5IuVczpNI\n\n# Code\n```python3 []\nclass Solution:\n def numberOfChild(self, n: int, k: int) -> int:\n pos=0\n direction=1\n for i in range(k):\n if direction==1:\n if pos==n-1:\n direction=-1\n pos+=direct... | 0 | 0 | ['Math', 'Simulation', 'Python3'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | 1ms solution. Pattern based logic | 1ms-solution-pattern-based-logic-by-that-ycmu | Complexity\n- Time complexity:\nO(1)\n\n- Space complexity:\nO(1)\n\n# Code\njava []\n/**\n n=5, k=6\n 0->0\n 1->1\n 2->2\n 3 | ThatNinjaGuySpeaks | NORMAL | 2024-12-01T16:04:45.229672+00:00 | 2024-12-01T16:04:45.229714+00:00 | 1 | false | # Complexity\n- Time complexity:\nO(1)\n\n- Space complexity:\nO(1)\n\n# Code\n```java []\n/**\n n=5, k=6\n 0->0\n 1->1\n 2->2\n 3->3\n 4->4\n \n 5->3\n 6->2\n 7->1\n 8->0\n\n 9->1\n 10->2\n 11->3\n 12->4\n\n ... | 0 | 0 | ['Java'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | C++ Beats 100% | c-beats-100-by-muhammad-hussnain-gqnn | 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 | muhammad-hussnain | NORMAL | 2024-11-27T13:56:27.848289+00:00 | 2024-11-27T13:56:27.848318+00:00 | 1 | 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)$$ --... | 0 | 0 | ['Math', 'C++'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | Easy Python beats 100% - 5 lines only | easy-python-beats-100-5-lines-only-by-pu-xgl5 | Intuition\nThere is no need for traversing the array. If k is equal to or less than n, its clear that we will end up on child k.\n\nOnce k hits n and the ball s | Puddle321 | NORMAL | 2024-11-25T21:39:44.125215+00:00 | 2024-11-25T21:39:44.125261+00:00 | 3 | false | # Intuition\nThere is no need for traversing the array. If k is equal to or less than n, its clear that we will end up on child k.\n\nOnce k hits n and the ball starts going backwards, the child that ends up with the ball is child number (n-1) - (k % (n-1)). This is because we count backwards from the last child (child... | 0 | 0 | ['Python'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | Simple solution -> 0 ms | simple-solution-0-ms-by-developersuserna-x79f | 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 | DevelopersUsername | NORMAL | 2024-11-25T17:56:11.954622+00:00 | 2024-11-25T17:56:11.954662+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:\nO(1)\n\n- Space complexity:\nO(1)\n\n# Code\n```java []\nclass Solution {\n public int numberOfChild(int n, int k) {\n\n ... | 0 | 0 | ['Java'] | 0 |
find-the-child-who-has-the-ball-after-k-seconds | JavaScript Solution | javascript-solution-by-shriharimutalik96-czj0 | 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 | shriharimutalik96 | NORMAL | 2024-11-24T17:46:52.199683+00:00 | 2024-11-24T17:46:52.199711+00:00 | 5 | 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(1)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $... | 0 | 0 | ['JavaScript'] | 0 |
minimum-add-to-make-parentheses-valid | [C++/Java/Python] Straight Forward One Pass | cjavapython-straight-forward-one-pass-by-m7ix | Intuition:\nTo make a string valid,\nwe can add some ( on the left,\nand add some ) on the right.\nWe need to find the number of each.\n\n\n## Explanation:\nlef | lee215 | NORMAL | 2018-10-14T03:18:46.443556+00:00 | 2019-09-16T14:47:46.973592+00:00 | 42,412 | false | ## **Intuition**:\nTo make a string valid,\nwe can add some `(` on the left,\nand add some `)` on the right.\nWe need to find the number of each.\n<br>\n\n## **Explanation**:\n`left` records the number of `(` we need to add on the left of `S`.\n`right` records the number of `)` we need to add on the right of `S`,\nwhic... | 334 | 11 | [] | 43 |
minimum-add-to-make-parentheses-valid | [Java/Python 3] two one pass codes - space O(n) and O(1), respectively | javapython-3-two-one-pass-codes-space-on-omrv | Intuition:\n\nWe only need to count those unmatched parentheses. \nKey Observation:\n\nAfter removal of matched parentheses, the remainings must be ))...)(...(( | rock | NORMAL | 2018-10-14T03:03:02.786478+00:00 | 2024-10-09T08:37:14.008269+00:00 | 10,335 | false | **Intuition:**\n\nWe only need to count those unmatched parentheses. \n**Key Observation:**\n\nAfter removal of matched parentheses, the remainings must be `))...)(...(`(both numbers of open and close patheses can be `0`). Otherwise, there would be at least one pair of `()`.\n\n**Algorithm Description:**\n\nLoop throug... | 82 | 2 | [] | 21 |
minimum-add-to-make-parentheses-valid | Beats 100% || Easy Self Explanatory Code || 3 Approaches | beats-100-easy-self-explanatory-code-3-a-afqw | 3 Approaches:\n\n\n# Approach 1\nTime O(N) Space O(N)\ncpp []\nclass Solution {\npublic:\n int minAddToMakeValid(string s) {\n stack<char> S;\n | Garv_Virmani | NORMAL | 2024-10-09T03:10:29.239281+00:00 | 2024-10-09T03:26:52.807369+00:00 | 14,524 | false | # 3 Approaches:\n\n\n# Approach 1\n**Time O(N) Space O(N)**\n```cpp []\nclass Solution {\npublic:\n int minAddToMakeValid(string s) {\n stack<char> S;\n int c = 0;\n for (char i : s)... | 76 | 4 | ['String', 'Stack', 'Greedy', 'C', 'Python', 'C++', 'Java', 'Rust', 'Ruby', 'JavaScript'] | 5 |
minimum-add-to-make-parentheses-valid | Java 4ms 100% straight-forward counting | java-4ms-100-straight-forward-counting-b-quh8 | Keep track of the number of unmatched open and closed parentheses.\n\n```\npublic int minAddToMakeValid(String S) {\n\tint unmatchedOpen = 0;\n\tint unmatchedCl | jebennett099 | NORMAL | 2019-01-30T06:57:08.508588+00:00 | 2019-01-30T06:57:08.508633+00:00 | 4,089 | false | Keep track of the number of unmatched open and closed parentheses.\n\n```\npublic int minAddToMakeValid(String S) {\n\tint unmatchedOpen = 0;\n\tint unmatchedClose = 0;\n \n\tfor (char c : S.toCharArray()) {\n\t\tif (c == \'(\') {\n unmatchedOpen++;\n } else if (unmatchedOpen > 0)\n\t\t\tunmatchedO... | 54 | 0 | [] | 5 |
minimum-add-to-make-parentheses-valid | Super simple approach, beats 100%, O(1) space and O(n) time, YouTube Video added | super-simple-approach-beats-100-o1-space-mm78 | Youtube Solution https://youtu.be/UojbHmdbj7Q\n# Intuition and approach\n keep it very very simple guys , just like below :- \n i have variable open = 0, mism | vinod_aka_veenu | NORMAL | 2024-10-09T01:47:07.779889+00:00 | 2024-10-09T05:38:03.826314+00:00 | 15,621 | false | # Youtube Solution https://youtu.be/UojbHmdbj7Q\n# Intuition and approach\n keep it very very simple guys , just like below :- \n i have variable **open = 0, mismatch = 0**\n now traverse in the string \nif **char = \'(\'** ==> open++;\nelse ===> **if(open>0)==> open--** ; (because we have to decrease open count as ... | 34 | 11 | ['Array', 'String Matching', 'Python', 'C++', 'Java', 'C#'] | 15 |
minimum-add-to-make-parentheses-valid | C++ 0ms faster than 100% | c-0ms-faster-than-100-by-random_cpp-ji1t | Whenever we get open bracket we will increase count by 1 and when we get close bracket we will decrease count by 1. But If our count becomes less than 0 than we | random_cpp | NORMAL | 2021-05-18T06:31:00.049470+00:00 | 2021-05-18T06:31:00.049515+00:00 | 3,027 | false | Whenever we get open bracket we will increase count by 1 and when we get close bracket we will decrease count by 1. But If our count becomes less than 0 than we have to add open bracket before current position so just absolute value of count and make count 0. Now at last we will add count to ans as we have to close all... | 34 | 0 | [] | 9 |
minimum-add-to-make-parentheses-valid | python two lines code | python-two-lines-code-by-abcd28s-9nti | \nclass Solution(object):\n def minAddToMakeValid(self, S):\n """\n :type S: str\n :rtype: int\n """\n \n while "() | abcd28s | NORMAL | 2018-11-11T20:46:48.272614+00:00 | 2018-11-11T20:46:48.272654+00:00 | 3,012 | false | ```\nclass Solution(object):\n def minAddToMakeValid(self, S):\n """\n :type S: str\n :rtype: int\n """\n \n while "()" in S:\n S = S.replace("()", "")\n \n return len(S)\n``` | 31 | 1 | [] | 8 |
minimum-add-to-make-parentheses-valid | 2 solutions | Easy to Understand | Faster | Simple | Python Solution | 2-solutions-easy-to-understand-faster-si-yzfz | \n def without_stack(self, S):\n opening = count = 0\n for i in S:\n if i == \'(\': \n opening += 1\n else | mrmagician | NORMAL | 2020-04-10T04:10:47.963438+00:00 | 2020-04-10T04:10:47.963493+00:00 | 3,268 | false | ```\n def without_stack(self, S):\n opening = count = 0\n for i in S:\n if i == \'(\': \n opening += 1\n else:\n if opening: opening -= 1\n else: count += 1\n return count + opening\n \n def using_stack(self, S):\n ... | 30 | 1 | ['Stack', 'Python', 'Python3'] | 3 |
minimum-add-to-make-parentheses-valid | Easy Solution (No Stack)🧠 Python | Java | C++ | C | C# | GO | Rust O(N), O(1) | easy-solution-no-stack-python-java-c-c-c-5dv8 | Intuition\n- Objective: Determine the minimum number of parentheses to add to make a string valid.\n\n- Goal: Keep two counters: one for open parentheses and on | violet_6 | NORMAL | 2024-10-09T00:14:15.864287+00:00 | 2024-10-09T00:30:05.001195+00:00 | 6,980 | false | # Intuition\n- **Objective**: Determine the minimum number of parentheses to add to make a string valid.\n\n- **Goal**: Keep two counters: one for open parentheses and one for the number of unmatched closing parentheses.\n\n# Approach\n1. Traverse the string character by character.\n2. Keep track of:\n- Open parenthese... | 26 | 2 | ['C', 'C++', 'Java', 'Go', 'Python3', 'Rust', 'C#'] | 8 |
minimum-add-to-make-parentheses-valid | Greedy string again||Beats 100% | greedy-string-againbeats-100-by-anwenden-3eth | Intuition\n Describe your first thoughts on how to solve this problem. \nGreedy string modifies the solution for yesterday\'s daily question\n1963. Minimum Num | anwendeng | NORMAL | 2024-10-09T01:57:52.993443+00:00 | 2024-10-09T08:27:46.821705+00:00 | 790 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nGreedy string modifies the solution for yesterday\'s daily question\n[1963. Minimum Number of Swaps to Make the String Balanced](https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/solutions/5884212/greedy-s... | 17 | 1 | ['String', 'Greedy', 'C++'] | 7 |
minimum-add-to-make-parentheses-valid | Python classic valid paranthesis solution | python-classic-valid-paranthesis-solutio-s7hr | \nclass Solution:\n def minAddToMakeValid(self, S):\n r, l = 0, []\n for s in S:\n if s == "(":\n l.append(s)\n | cenkay | NORMAL | 2018-10-14T07:40:38.573424+00:00 | 2018-10-18T16:04:18.198447+00:00 | 2,922 | false | ```\nclass Solution:\n def minAddToMakeValid(self, S):\n r, l = 0, []\n for s in S:\n if s == "(":\n l.append(s)\n elif l:\n l.pop()\n else:\n r += 1 \n return r + len(l)\n```\nor for fun =>\n```\nclass Solution:\n ... | 17 | 0 | [] | 3 |
minimum-add-to-make-parentheses-valid | [JAVA] [0MS] [100%] [STACK] | java-0ms-100-stack-by-trevor-akshay-5gwr | ```\nclass Solution {\n public int minAddToMakeValid(String S) {\n int res = 0;\n Stack stack = new Stack<>();\n for(char c : S.toCharArr | trevor-akshay | NORMAL | 2021-02-06T05:31:53.266341+00:00 | 2021-02-06T05:31:53.266377+00:00 | 2,000 | false | ```\nclass Solution {\n public int minAddToMakeValid(String S) {\n int res = 0;\n Stack<Character> stack = new Stack<>();\n for(char c : S.toCharArray()){\n if(c == \'(\'){\n stack.push(c);\n }\n else{\n if(stack.empty())\n ... | 16 | 0 | ['Stack', 'Java'] | 2 |
minimum-add-to-make-parentheses-valid | [Java] Easy to understand Stack Solution (8ms) | java-easy-to-understand-stack-solution-8-tzfp | Each time we come across an open paren '(', we add it to the stack. If we come across a closing paren ')' we check if the stack is empty and if not we pop off | ernie-h | NORMAL | 2018-10-18T04:18:06.733506+00:00 | 2018-10-21T11:55:15.996692+00:00 | 2,092 | false | Each time we come across an open paren '(', we add it to the stack.
If we come across a closing paren ')' we check if the stack is empty and if not we pop off the open paren.
Else if the stack is empty, we increment the counter for an amend needed.
In the case the stack is not empty when we complete the loop, we co... | 16 | 1 | [] | 5 |
minimum-add-to-make-parentheses-valid | 🏗️ Two Approaches [SC: O(n) vs O(1)]: Stack vs. Greedy 🚀 | two-approaches-sc-on-vs-o1-stack-vs-gree-fq2l | Approach\n1. # First Approach:\n We traverse the string, and for each character:\n If it\'s an opening parenthesis (, we push it onto the stack.\n If i | AlgoArtisan | NORMAL | 2024-10-09T02:52:37.098389+00:00 | 2024-10-09T02:59:23.322369+00:00 | 932 | false | # Approach\n1. # First Approach:\n We traverse the string, and for each character:\n If it\'s an opening parenthesis (, we push it onto the stack.\n If it\'s a closing parenthesis ), we check if the top of the stack is an opening parenthesis (. If so, we pop the stack (since they balance each other). Otherwise... | 15 | 2 | ['String', 'Stack', 'Greedy', 'C++'] | 5 |
minimum-add-to-make-parentheses-valid | C++ Simple, Short and Easy Solution, O(n) time O(1) space, faster than 100% | c-simple-short-and-easy-solution-on-time-2see | \nclass Solution {\npublic:\n int minAddToMakeValid(string S) {\n int res = 0, n = S.size(), balance = 0;\n for (int i = 0; i < n; i++) {\n | yehudisk | NORMAL | 2021-02-19T10:02:40.267814+00:00 | 2021-02-19T10:02:40.267854+00:00 | 1,431 | false | ```\nclass Solution {\npublic:\n int minAddToMakeValid(string S) {\n int res = 0, n = S.size(), balance = 0;\n for (int i = 0; i < n; i++) {\n balance += S[i] == \'(\' ? 1 : -1;\n if (balance == -1) {\n res++;\n balance++;\n }\n }\n ... | 13 | 0 | ['C'] | 1 |
minimum-add-to-make-parentheses-valid | JavaScript Clean One Pass | javascript-clean-one-pass-by-control_the-n3zy | javascript\nvar minAddToMakeValid = function(S) {\n let open = 0, close = 0;\n \n for(let c of S) {\n if(c === \'(\') open++;\n else if(! | control_the_narrative | NORMAL | 2020-08-06T14:06:34.201741+00:00 | 2020-08-06T14:06:34.201771+00:00 | 1,090 | false | ```javascript\nvar minAddToMakeValid = function(S) {\n let open = 0, close = 0;\n \n for(let c of S) {\n if(c === \'(\') open++;\n else if(!open) close++;\n else open--;\n }\n return open + close;\n};\n``` | 13 | 1 | ['JavaScript'] | 1 |
minimum-add-to-make-parentheses-valid | Java simple solution 100% faster | java-simple-solution-100-faster-by-feyse-p4l9 | \nclass Solution {\n public int minAddToMakeValid(String S) {\n int openingNeeded = 0;\n int closingNeeded = 0;\n \n for(int i = | feyselmubarek | NORMAL | 2020-01-25T14:05:01.862774+00:00 | 2020-02-05T13:13:28.248113+00:00 | 869 | false | ```\nclass Solution {\n public int minAddToMakeValid(String S) {\n int openingNeeded = 0;\n int closingNeeded = 0;\n \n for(int i = 0; i < S.length(); i++){\n if(S.charAt(i) == \'(\'){\n closingNeeded++;\n }else{\n if(closingNeeded == 0)... | 12 | 0 | ['Java'] | 1 |
minimum-add-to-make-parentheses-valid | C++|| For Beginners..!!! || Stack | c-for-beginners-stack-by-ananya2023-slrk | \nclass Solution {\npublic:\n int minAddToMakeValid(string s) {\n stack<char> st;\n int count=0;\n for(int i=0;i<s.size();i++){\n | ananya2023 | NORMAL | 2021-09-06T12:13:33.087288+00:00 | 2021-09-06T12:13:33.087323+00:00 | 1,969 | false | ```\nclass Solution {\npublic:\n int minAddToMakeValid(string s) {\n stack<char> st;\n int count=0;\n for(int i=0;i<s.size();i++){\n if(s[i]==\')\'){\n if(st.empty())\n count++;\n else if(st.top()==\'(\')\n st.pop()... | 11 | 0 | ['Stack', 'C', 'C++'] | 3 |
minimum-add-to-make-parentheses-valid | C++ 5 lines, O(n) | c-5-lines-on-by-votrubac-wgik | We can flip the question and ask how many "extra" parentheses the string has that makes it invalid. Then we can add (or remove) the same number of parentheses t | votrubac | NORMAL | 2018-10-14T03:45:06.431514+00:00 | 2018-10-15T06:48:23.729117+00:00 | 1,784 | false | We can flip the question and ask how many "extra" parentheses the string has that makes it invalid. Then we can add (or remove) the same number of parentheses to make it valid.\n\nWe increment the number of ```open``` parentheses when we see ```\'(\'```, and decrement otherwise. Every time that number is negative, tha... | 11 | 0 | [] | 3 |
minimum-add-to-make-parentheses-valid | C++(Using Stack- 100% faster) | cusing-stack-100-faster-by-amit_singhh-1k32 | Please Upvote if you like the solution!\n\n class Solution {\n public:\n int minAddToMakeValid(string s) \n {\n stack st;//create a Stack\n | amit_singhh | NORMAL | 2022-03-26T18:30:45.458983+00:00 | 2022-03-26T18:30:45.459027+00:00 | 728 | false | **Please Upvote if you like the solution!**\n\n class Solution {\n public:\n int minAddToMakeValid(string s) \n {\n stack<char> st;//create a Stack\n \n for(int i=0;i<s.size();i++)\n {\n if(s[i]==\'(\')//whenever their is open bracket insert on stack\n s... | 10 | 0 | ['String', 'Stack', 'C', 'C++'] | 0 |
minimum-add-to-make-parentheses-valid | Python 3-line mathematically optimal solution with explanation, one-pass O(n) time O(1) space | python-3-line-mathematically-optimal-sol-nakx | 3-liner:\npython\ndef minAddToMakeValid(self, S):\n a = b = 0\n for c in S: a, b = (a, b + 1) if c == \'(\' else (min(a, b - 1), b - 1)\n return b - a | zemer | NORMAL | 2018-10-14T07:01:08.771935+00:00 | 2018-10-20T12:21:51.389989+00:00 | 659 | false | 3-liner:\n```python\ndef minAddToMakeValid(self, S):\n a = b = 0\n for c in S: a, b = (a, b + 1) if c == \'(\' else (min(a, b - 1), b - 1)\n return b - a * 2\n```\nSome 1-liners with functional programming, just for fun:\n```python\ndef minAddToMakeValid(self, S):\n return (lambda a,b:b-a*2)(*reduce(lambda ... | 10 | 0 | [] | 1 |
minimum-add-to-make-parentheses-valid | Let's Form Couples ❤️🔥 Part - II | Very Easy Explanation + Full Concept + illustrations | lets-form-couples-part-ii-very-easy-expl-htr6 | Youtube Explanation\nSoon to be on Channel : https://www.youtube.com/@Intuit_and_Code\nEdited:\nhttps://youtu.be/ZRr2UiHIBBA?si=kUm0zzURb2UkN91H\n\n> I would re | Rarma | NORMAL | 2024-10-09T07:30:33.964101+00:00 | 2024-10-09T08:56:14.597267+00:00 | 1,048 | false | # Youtube Explanation\nSoon to be on Channel : https://www.youtube.com/@Intuit_and_Code\nEdited:\nhttps://youtu.be/ZRr2UiHIBBA?si=kUm0zzURb2UkN91H\n\n> I would recommend to read this article first : **[Link](https://leetcode.com/problems/minimum-number-of-swaps-to-make-the-string-balanced/solutions/5886919/let-s-form-c... | 9 | 0 | ['Math', 'String', 'Stack', 'Greedy', 'String Matching', 'Python', 'C++', 'Java', 'Python3'] | 3 |
minimum-add-to-make-parentheses-valid | Python | Greedy Pattern | python-greedy-pattern-by-khosiyat-y0rp | see the Successfully Accepted Submission\n\n# Code\npython3 []\nclass Solution:\n def minAddToMakeValid(self, s: str) -> int:\n # Initialize balance v | Khosiyat | NORMAL | 2024-10-09T03:30:03.123427+00:00 | 2024-10-09T03:30:03.123466+00:00 | 1,011 | false | [see the Successfully Accepted Submission](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/submissions/1416537063/?envType=daily-question&envId=2024-10-09)\n\n# Code\n```python3 []\nclass Solution:\n def minAddToMakeValid(self, s: str) -> int:\n # Initialize balance variables for unmatched... | 9 | 0 | ['Python3'] | 4 |
minimum-add-to-make-parentheses-valid | Python3 || 10 lines, track ()s || T/S: 99% / 54% | python3-10-lines-track-s-ts-99-54-by-spa-m31j | Here\'s the intuition:\n\nFirst thoughts go right to using a stack, but we only need to determine the number the mis-balanced parenthesis pairs and return an in | Spaulding_ | NORMAL | 2024-10-09T00:18:25.677651+00:00 | 2024-10-09T04:38:00.379898+00:00 | 83 | false | Here\'s the intuition:\n\nFirst thoughts go right to using a stack, but we only need to determine the number the mis-balanced parenthesis pairs and return an integer. We can accomplish this task with simple counting.\n```python3 []\nclass Solution:\n def minAddToMakeValid(self, s: str) -> int:\n\n moves = tal... | 9 | 0 | ['C++', 'Java', 'Python3'] | 0 |
minimum-add-to-make-parentheses-valid | simple cpp solution | simple-cpp-solution-by-prithviraj26-6x3h | 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 | prithviraj26 | NORMAL | 2023-02-10T04:59:51.774207+00:00 | 2023-02-10T04:59:51.774236+00:00 | 609 | 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)$$ -->\no(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n... | 9 | 0 | ['String', 'Stack', 'C++'] | 0 |
minimum-add-to-make-parentheses-valid | Explaining One Pass | explaining-one-pass-by-gracemeng-viv6 | It is intuitive to count ( and ) and balance them on the fly.\n\nCases like )( should be disallowed, regardless of equal counts of ( and ) .\n\nThat is, count o | gracemeng | NORMAL | 2021-06-15T19:01:18.485141+00:00 | 2021-06-15T19:01:18.485172+00:00 | 426 | false | It is intuitive to count `(` and `)` and balance them on the fly.\n\nCases like `)(` should be disallowed, regardless of equal counts of `(` and `)` .\n\nThat is, count of `)` should `<=` count of `(` all the time.\n\nReferring to the code, when `right_expected == 0` and we hit `)`, we ought to add a `(` in the front i... | 9 | 0 | [] | 1 |
minimum-add-to-make-parentheses-valid | BASIC IF ELSE STATEMENT ✅✅69_BEATS_100%✅✅🔥Easy Solution🔥With Explanation🔥 | basic-if-else-statement-69_beats_100easy-629h | Intuition\n Describe your first thoughts on how to solve this problem. \nwe need to count how many illegal paranthesis are there \na pair of paranthesis is lega | srinivas_bodduru | NORMAL | 2024-10-09T00:19:47.859025+00:00 | 2024-10-09T00:19:47.859058+00:00 | 1,352 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nwe need to count how many illegal paranthesis are there \na pair of paranthesis is legal only if it is like this ()\nmeans ) followed by (\nso we keep track of brackets encountered in a stack and we compare legal vs illegal \n\n# Approach... | 8 | 0 | ['String', 'Stack', 'Greedy', 'C', 'Python', 'C++', 'Java', 'Python3', 'JavaScript'] | 4 |
minimum-add-to-make-parentheses-valid | [Java] Stack Solution || With Explanation | java-stack-solution-with-explanation-by-9md8w | UPVOTE IF YOU LIKE THE EXPLANATION !!\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n Stack Data Structure\n# Approach\n Descr | Mjashwanth | NORMAL | 2023-04-24T15:42:47.280575+00:00 | 2023-04-24T15:43:40.325446+00:00 | 993 | false | # UPVOTE IF YOU LIKE THE EXPLANATION !!\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n Stack Data Structure\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Stack consists of **\'(\'** and **\')\'** symbols only.\n- In case of **\'(\'** you have to push i... | 8 | 0 | ['Stack', 'Java'] | 0 |
minimum-add-to-make-parentheses-valid | ✅C++ 100% faster & 90% memory efficient || easy single loop solution | c-100-faster-90-memory-efficient-easy-si-2516 | Please upvote if you find this helpful \uD83D\uDE4F\n\n# Code\n\nclass Solution {\npublic:\n int minAddToMakeValid(string s) {\n int ans = 0;\n | underground_coder | NORMAL | 2023-01-03T04:44:54.147237+00:00 | 2023-01-03T04:44:54.147283+00:00 | 870 | false | # Please upvote if you find this helpful \uD83D\uDE4F\n\n# Code\n```\nclass Solution {\npublic:\n int minAddToMakeValid(string s) {\n int ans = 0;\n int open = 0;\n int close = 0;\n\n for(int j=0;j<s.length();j++){\n\n if((open - close) == 0 && s[j] == \')\'){\n ... | 8 | 0 | ['C++'] | 0 |
minimum-add-to-make-parentheses-valid | [Java] 100% [O(N) TC ][O(1) SC] || With Explanation | java-100-on-tc-o1-sc-with-explanation-by-i7pj | Please upvote if you like the solution !!\n# Intuition\n Describe your first thoughts on how to solve this problem. \n- If the symbol is \'(\' just increase the | Mjashwanth | NORMAL | 2023-04-25T01:18:53.584107+00:00 | 2023-04-25T01:18:53.584129+00:00 | 302 | false | # Please upvote if you like the solution !!\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- If the symbol is **\'(\'** just increase the count of **open** variable by 1.\n- If the symbol is **\')\'** check whether it\'s pair there or not. \n- If **open** variable is greater than zer... | 7 | 0 | ['Java'] | 0 |
minimum-add-to-make-parentheses-valid | 2 pointer building up from Stack C++ solution | 2-pointer-building-up-from-stack-c-solut-lsvf | (1) Stack\n\nclass Solution {\npublic:\n int minAddToMakeValid(string S) {\n stack<char> stack;\n \n for(char s : S){\n if(s= | ankurharitosh | NORMAL | 2020-07-23T14:35:33.964808+00:00 | 2020-07-23T14:35:33.964850+00:00 | 406 | false | (1) Stack\n```\nclass Solution {\npublic:\n int minAddToMakeValid(string S) {\n stack<char> stack;\n \n for(char s : S){\n if(s==\'(\')\n stack.push(s);\n else{\n if(!stack.empty() && stack.top()==\'(\')\n stack.pop();\n ... | 7 | 0 | ['Two Pointers', 'Stack', 'C', 'C++'] | 0 |
minimum-add-to-make-parentheses-valid | python simple beats 100% (as of 22nd October, 2018) | python-simple-beats-100-as-of-22nd-octob-q8ve | The idea is to keep a separate counter for invalid left and right parenthesis. Whenever a new left parenthesis is encountered, the left counter is incremented b | keshavsarraf | NORMAL | 2018-10-22T11:24:19.546599+00:00 | 2018-10-25T01:38:16.622250+00:00 | 547 | false | The idea is to keep a separate counter for invalid left and right parenthesis. Whenever a new left parenthesis is encountered, the left counter is incremented by 1 (assuming it to be an invalid parenthesis). When a right parenthesis is encountered, the left counter is decremented by 1 (since we found a pair, we have on... | 7 | 0 | [] | 0 |
minimum-add-to-make-parentheses-valid | Easy Python solution+ comments (12ms , 99% faster) | easy-python-solution-comments-12ms-99-fa-043y | \nclass Solution(object):\n def minAddToMakeValid(self, S):\n """\n :type S: str\n :rtype: int\n """\n stack =[]\n | pakilamak | NORMAL | 2020-08-07T07:03:18.873877+00:00 | 2020-08-07T07:11:42.513909+00:00 | 395 | false | ```\nclass Solution(object):\n def minAddToMakeValid(self, S):\n """\n :type S: str\n :rtype: int\n """\n stack =[]\n \n for i in range(len(S)):\n \n # if character is "(", then append to the stack\n if S[i] == "(":\n st... | 6 | 0 | ['Python'] | 1 |
minimum-add-to-make-parentheses-valid | Python | Beats 99% | Very easy to understand | python-beats-99-very-easy-to-understand-ysuw3 | Explaination:\nWhenever \'(\' comes, push it to stack.\nWhenever \')\' comes and stack is not empty, pop from stack.\nIf \')\' comes and stack is empty, add to | manasswami | NORMAL | 2020-04-16T10:20:13.250682+00:00 | 2020-04-16T10:20:13.250776+00:00 | 294 | false | **Explaination:**\nWhenever \'(\' comes, push it to stack.\nWhenever \')\' comes and stack is not empty, pop from stack.\nIf \')\' comes and stack is empty, add to count (count+=1)\n**Code:**\n```class Solution(object):\n def minAddToMakeValid(self, S):\n """\n :type S: str\n :rtype: int\n ... | 6 | 0 | [] | 3 |
minimum-add-to-make-parentheses-valid | 💡 C++ | O(n) | Space O(n) [Stack] -> O(1) [No Stack] | With Full Explanation ✏️ | c-on-space-on-stack-o1-no-stack-with-ful-5z2f | Intuition\nThe problem asks us to make a string of parentheses valid by adding the minimum number of parentheses. A valid string means every open parenthesis ( | Tusharr2004 | NORMAL | 2024-10-09T09:50:22.335325+00:00 | 2024-10-09T09:59:40.144334+00:00 | 18 | false | # Intuition\nThe problem asks us to make a string of parentheses valid by adding the minimum number of parentheses. A valid string means every open parenthesis `(` must have a corresponding close parenthesis `)`. The goal is to find how many parentheses need to be added to make the string valid.\n\nMy first thought is ... | 5 | 1 | ['String', 'Stack', 'Greedy', 'C++'] | 1 |
minimum-add-to-make-parentheses-valid | Greedy Handling of Unbalanced Parentheses | Java | C++ | Video Solution | greedy-handling-of-unbalanced-parenthese-2ske | Intuition, approach, and complexity discussed in video solution in detail.\nhttps://youtu.be/aHp2Q6y0_Nw\n# Code\njava []\nclass Solution {\n public int minA | Lazy_Potato_ | NORMAL | 2024-10-09T05:36:46.958819+00:00 | 2024-10-09T05:36:46.958856+00:00 | 206 | false | # Intuition, approach, and complexity discussed in video solution in detail.\nhttps://youtu.be/aHp2Q6y0_Nw\n# Code\n```java []\nclass Solution {\n public int minAddToMakeValid(String s) {\n int openReq = 0;\n int openCnt = 0;\n for(var chr : s.toCharArray()){\n if(chr == \'(\')openCnt++;\n ... | 5 | 0 | ['String', 'Stack', 'Greedy', 'C++', 'Java'] | 2 |
minimum-add-to-make-parentheses-valid | 💡| O(n) | C++ 0ms Beats 100.00% | Easy Solution🧠| Py3 23ms Beats 98.69% | Java 0ms Beats 100.00% | | on-c-0ms-beats-10000-easy-solution-py3-2-lzsb | \n#\n---\n\n> # Intuition\nTo determine the minimum number of insertions required to make the parentheses string valid, we need to track the number of unmatched | user4612MW | NORMAL | 2024-10-09T03:23:13.864355+00:00 | 2024-10-09T03:50:42.064038+00:00 | 585 | false | \n#\n---\n\n> # Intuition\nTo determine the minimum number of insertions required to make the parentheses string valid, we need to track the number of unmatched opening and closing parentheses. As we traverse the string, we increment counts based on whether the current character is an opening or closing parenthesis. Wh... | 5 | 0 | ['C++', 'Java', 'Python3'] | 4 |
minimum-add-to-make-parentheses-valid | "💯 Beats 100% | No Stack Needed 🚫🧳 | Simple C++ Solution 🚀" | beats-100-no-stack-needed-simple-c-solut-j68e | Intuition\nThe problem asks for the minimum number of parentheses that need to be added to make a given string of parentheses valid. A string of parentheses is | geek_nik | NORMAL | 2024-10-09T02:12:50.584144+00:00 | 2024-10-09T02:12:50.584189+00:00 | 116 | false | # Intuition\nThe problem asks for the minimum number of parentheses that need to be added to make a given string of parentheses valid. A string of parentheses is valid when every open parenthesis $$($$ has a matching close parenthesis $$)$$. The idea is to keep track of unmatched open and close parentheses while iterat... | 5 | 0 | ['String', 'Stack', 'Greedy', 'C++'] | 1 |
minimum-add-to-make-parentheses-valid | My solution beats 100% . simple solution in c++ | my-solution-beats-100-simple-solution-in-cnle | Intuition \uD83C\uDFAF\n Describe your first thoughts on how to solve this problem. \nThis problem focuses on finding the minimum number of parentheses addition | letsdoit5806 | NORMAL | 2024-10-09T02:06:03.104667+00:00 | 2024-10-09T02:06:03.104700+00:00 | 184 | false | # Intuition \uD83C\uDFAF\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis problem focuses on finding the minimum number of parentheses additions needed to make a string of parentheses (( and )) valid. A valid string of parentheses is one where every opening parenthesis (() has a corresponding ... | 5 | 0 | ['Stack', 'C++'] | 1 |
minimum-add-to-make-parentheses-valid | ✅Efficient Easy to understand🔥Beginner Friendly🔥|| Stack|| | efficient-easy-to-understandbeginner-fri-9ds6 | Problem Understanding\n Describe your first thoughts on how to solve this problem. \nThe problem requires finding the minimum number of parentheses needed to be | anand_shukla1312 | NORMAL | 2024-10-09T01:09:11.943675+00:00 | 2024-10-09T01:09:11.943701+00:00 | 93 | false | # Problem Understanding\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires finding the minimum number of parentheses needed to be added to make a given string of parentheses valid. \n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Stack-based Validation*... | 5 | 0 | ['Stack', 'Python3'] | 0 |
minimum-add-to-make-parentheses-valid | Easy C++ Code, beats 100% time and 90% space complexity, 10 lines code | easy-c-code-beats-100-time-and-90-space-8wt6e | \n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nIterating once and counting all | Kyouma45 | NORMAL | 2023-05-31T11:56:05.656000+00:00 | 2023-05-31T11:56:05.656040+00:00 | 35 | false | \n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nIterating once and counting all the lose paranthesis in the string.\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space compl... | 5 | 0 | ['C++'] | 0 |
minimum-add-to-make-parentheses-valid | Python | Easy Solution✅ | python-easy-solution-by-gmanayath-ogif | Solution 1:\n\ndef minAddToMakeValid(self, s: str) -> int:\n while "()" in s:\n s= s.replace("()","",1)\n return len(s)\n\nSolution 2:\ | gmanayath | NORMAL | 2022-08-28T07:45:11.292317+00:00 | 2022-12-22T16:46:56.687595+00:00 | 790 | false | Solution 1:\n```\ndef minAddToMakeValid(self, s: str) -> int:\n while "()" in s:\n s= s.replace("()","",1)\n return len(s)\n```\nSolution 2:\n```\ndef minAddToMakeValid(self, s: str) -> int:\n output=[]\n for i in range(len(s)):\n if s[i] == \'(\':\n outp... | 5 | 0 | ['Python', 'Python3'] | 1 |
minimum-add-to-make-parentheses-valid | Javascript stack | javascript-stack-by-fbecker11-q8o7 | \nvar minAddToMakeValid = function(S) {\n const stack = []\n for(let s of S){\n if(s === \')\' && stack[stack.length-1] === \'(\')\n stack.pop()\n | fbecker11 | NORMAL | 2020-11-17T19:23:04.982168+00:00 | 2020-11-17T19:23:04.982200+00:00 | 506 | false | ```\nvar minAddToMakeValid = function(S) {\n const stack = []\n for(let s of S){\n if(s === \')\' && stack[stack.length-1] === \'(\')\n stack.pop()\n else\n stack.push(s)\n }\n return stack.length\n};\n``` | 5 | 1 | ['JavaScript'] | 0 |
minimum-add-to-make-parentheses-valid | 😍💯2 Lines of Codeeeee with 1 ms✅,Beats 90.31% in Memory📝 The Easiest Approach Ever🚀 | 2-lines-of-codeeeee-with-1-msbeats-9031-k11z7 | Appproach\nAaaaaaaaaaaaa.. I mean There\'s not much to explain. I\'m simply using contains() to check if any substring "()" is present in the main string. If I | ArcuLus | NORMAL | 2024-10-09T15:07:45.477961+00:00 | 2024-10-09T15:07:45.477984+00:00 | 18 | false | # Appproach\nAaaaaaaaaaaaa.. I mean There\'s not much to explain. I\'m simply using contains() to check if any substring "()" is present in the main string. If I find it, I remove it.\n\nFor example, let\'s say you have a string like "(((((())))))\')". In this case, everything is balanced except for the last \')\'.\n\n... | 4 | 0 | ['String', 'Stack', 'Greedy', 'Java'] | 1 |
minimum-add-to-make-parentheses-valid | Very easy......🔥🔥🔥🔥 | very-easy-by-pritambanik-z08r | 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 | PritamBanik | NORMAL | 2024-10-09T14:58:19.795406+00:00 | 2024-10-09T14:58:19.795441+00:00 | 42 | 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: 100%\u2705\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. ... | 4 | 0 | ['String', 'Stack', 'Greedy', 'C'] | 0 |
minimum-add-to-make-parentheses-valid | Simple 3 lines Code (Easy Approach) | simple-3-lines-code-easy-approach-by-tan-t2gb | \n# Approach\n1. Keep count of open and close bracket. \n2. if \'(\' encounter increesed open and else if \')\' encounter and open is greater than 0 means 1 ope | Tanmay_bhure | NORMAL | 2024-10-09T13:56:26.205198+00:00 | 2024-10-09T13:56:26.205238+00:00 | 133 | false | \n# Approach\n1. Keep count of open and close bracket. \n2. if \'(\' encounter increesed open and else if \')\' encounter and open is greater than 0 means 1 open open bracket is already happens so decreesed open else increese close\n3. return open + close\n\n# Complexity\n- Time complexity:\nO(n);\n\n- Space complexity... | 4 | 0 | ['String', 'Greedy', 'Python', 'C++', 'Java'] | 0 |
minimum-add-to-make-parentheses-valid | Simple Java Code ☠️ | simple-java-code-by-abhinandannaik1717-84z2 | Code\njava []\nclass Solution {\n public int minAddToMakeValid(String s) {\n int n = s.length();\n Stack<Character> stack = new Stack<>();\n | abhinandannaik1717 | NORMAL | 2024-10-09T13:21:45.968721+00:00 | 2024-10-09T13:21:45.968764+00:00 | 106 | false | # Code\n```java []\nclass Solution {\n public int minAddToMakeValid(String s) {\n int n = s.length();\n Stack<Character> stack = new Stack<>();\n for(int i=0;i<n;i++){\n if(s.charAt(i) == \'(\'){\n stack.push(\'(\');\n }\n else{\n if... | 4 | 0 | ['String', 'Stack', 'Java'] | 0 |
minimum-add-to-make-parentheses-valid | C++ || Easy Stack Approach✅✅ | c-easy-stack-approach-by-arunk_leetcode-7pq1 | Intuition\nThe goal is to determine how many parentheses must be added to make a given string valid. A valid parentheses string is one where every opening paren | arunk_leetcode | NORMAL | 2024-10-09T04:38:19.842561+00:00 | 2024-10-09T04:38:19.842606+00:00 | 170 | false | # Intuition\nThe goal is to determine how many parentheses must be added to make a given string valid. A valid parentheses string is one where every opening parenthesis `\'(\'` has a corresponding closing parenthesis `\')\'`. \n\nTo achieve this, we can use a stack to track unmatched opening parentheses and a counter t... | 4 | 0 | ['Stack', 'C++'] | 1 |
minimum-add-to-make-parentheses-valid | Very Easy with O(1) Space 100% beat | very-easy-with-o1-space-100-beat-by-vira-hebv | \n\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 | viratkohli_ | NORMAL | 2024-10-09T02:39:09.895274+00:00 | 2024-10-09T02:39:09.895322+00:00 | 20 | false | \n\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```cpp []\ncla... | 4 | 0 | ['String', 'Greedy', 'C++'] | 0 |
minimum-add-to-make-parentheses-valid | JAVA | 100% Beats | Linear Approach with O(1) Space | java-100-beats-linear-approach-with-o1-s-krep | \n# Code\njava []\nclass Solution {\n public int minAddToMakeValid(String s) {\n int set = 0;\n int res = 0;\n for (char c : s.toCharArr | SudhikshaGhanathe | NORMAL | 2024-09-08T08:32:12.667547+00:00 | 2024-09-08T08:32:12.667571+00:00 | 227 | false | \n# Code\n```java []\nclass Solution {\n public int minAddToMakeValid(String s) {\n int set = 0;\n int res = 0;\n for (char c : s.toCharArray()) {\n switch (c) {\n case \'(\' -> set += 1;\n case \')\' -> set -= 1;\n }\n if (set == -1... | 4 | 0 | ['Java'] | 1 |
minimum-add-to-make-parentheses-valid | Java Simple Brute Force Solution Using Stack | java-simple-brute-force-solution-using-s-4btp | Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem is about finding the minimum number of parentheses that need to be added to | Chaitanya_91 | NORMAL | 2024-08-15T17:25:14.372129+00:00 | 2024-08-15T17:25:14.372166+00:00 | 191 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is about finding the minimum number of parentheses that need to be added to make a string of parentheses valid. A valid string means that every opening parenthesis \'(\' has a corresponding closing parenthesis \')\'.\n\n# Appr... | 4 | 0 | ['Java'] | 0 |
minimum-add-to-make-parentheses-valid | SIMPLE STACK C++ COMMENTED SOLUTION | simple-stack-c-commented-solution-by-jef-1u5z | 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 | Jeffrin2005 | NORMAL | 2024-07-26T12:37:55.292658+00:00 | 2024-07-26T12:41:12.626915+00:00 | 164 | 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(n)\n<!-- Add your space complexity here, e.g. $$O... | 4 | 0 | ['C++'] | 1 |
minimum-add-to-make-parentheses-valid | 100% beats, easy-peasy code. | 100-beats-easy-peasy-code-by-a_krisssh-nchs | \n#### I don\'t know why this problem is tagged as Medium. Easy-peasy code in O(N) time complexity and O(1) space complexity.\n\n\n# Code\n\nclass Solution {\np | a_krisssh | NORMAL | 2024-07-05T16:45:34.482992+00:00 | 2024-07-05T16:45:34.483021+00:00 | 321 | false | \n#### I don\'t know why this problem is tagged as Medium. Easy-peasy code in O(N) time complexity and O(1) space complexity.\n\n\n# Code\n```\nclass Solution {\npublic:\n int minAddToMakeValid(string s) {\n int open = 0, close = 0;\n\n for(char ch: s){\n if(ch == \'(\') open++;\n ... | 4 | 0 | ['String', 'Greedy', 'C++'] | 0 |
minimum-add-to-make-parentheses-valid | ✔️✔️💯Brute Intuitive solution💯✅🔥 with and without stack beast time and Space | brute-intuitive-solution-with-and-withou-at2d | \n# Code\njava []\nclass Solution {\n public int minAddToMakeValid(String s) {\n\n int requiredAdditions = 0;\n Stack<Character> st = new Stack | Dixon_N | NORMAL | 2024-06-21T18:49:49.342356+00:00 | 2024-06-21T19:16:26.677586+00:00 | 209 | false | \n# Code\n```java []\nclass Solution {\n public int minAddToMakeValid(String s) {\n\n int requiredAdditions = 0;\n Stack<Character> st = new Stack<>();\n for (char c : s.toCharArray()) {\n if (c == \'(\') {\n st.push(c);\n } else if(st.isEmpty())\n ... | 4 | 0 | ['Java'] | 3 |
minimum-add-to-make-parentheses-valid | PYTHON || OPTIMAL SOLUTION || 100% EFFICIENT || EASY TO UNDERSTAND || | python-optimal-solution-100-efficient-ea-tdk7 | if it\'s help, please up \u2B06 vote! \u2764\uFE0F\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \n- The intuition behind this pro | Adit_gaur | NORMAL | 2024-05-24T04:04:12.580876+00:00 | 2024-05-24T04:04:12.580897+00:00 | 88 | false | # if it\'s help, please up \u2B06 vote! \u2764\uFE0F\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- The intuition behind this problem is to determine the minimum number of parentheses (opening or closing) that need to be added to a given string to make it a valid parentheses expr... | 4 | 0 | ['String', 'Stack', 'Greedy', 'Python', 'Python3'] | 2 |
minimum-add-to-make-parentheses-valid | Java Easy Solution for Beginners | java-easy-solution-for-beginners-by-raja-gt11 | Approach\n1. Define a method minAddToMakeValid that takes a string s representing parentheses.\n2. Initialize a stack (Stack<Character> st) to keep track of ope | RajarshiMitra | NORMAL | 2024-05-13T05:39:39.890789+00:00 | 2024-06-16T07:03:45.945257+00:00 | 22 | false | # Approach\n1. Define a method `minAddToMakeValid` that takes a string `s` representing parentheses.\n2. Initialize a stack (`Stack<Character> st`) to keep track of opening parentheses.\n3. Initialize a counter (`count`) to count the number of additional parentheses needed.\n4. Iterate through each character in the str... | 4 | 0 | ['Java'] | 0 |
minimum-add-to-make-parentheses-valid | 🧠[C++/Java/Python]: One Pass Easy Solution | cjavapython-one-pass-easy-solution-by-hr-1fzc | Approach\n> 1. Initialize an empty stack st to keep track of unmatched opening parentheses.\n> 2. Iterate through each character i in the input string s.\n\n> 3 | hrishi13 | NORMAL | 2023-09-02T17:12:04.072681+00:00 | 2023-09-02T17:12:04.072721+00:00 | 373 | false | ## Approach\n> 1. Initialize an empty stack st to keep track of unmatched opening parentheses.\n> 2. Iterate through each character i in the input string s.\n\n> 3. For each character i:\n> * If the stack is empty, push the character onto the stack because there\'s nothing to match it with.\n> * If the stack is not emp... | 4 | 0 | ['Python', 'C++', 'Java', 'Python3'] | 0 |
minimum-add-to-make-parentheses-valid | 100% beats || c++ || stack | 100-beats-c-stack-by-ganeshkumawat8740-lr79 | Code\n\nclass Solution {\npublic:\n int minAddToMakeValid(string s) {\n int ans = 0;\n vector<char> v;\n for(auto &i: s){\n i | ganeshkumawat8740 | NORMAL | 2023-06-22T03:39:35.471740+00:00 | 2023-06-22T03:39:35.471761+00:00 | 669 | false | # Code\n```\nclass Solution {\npublic:\n int minAddToMakeValid(string s) {\n int ans = 0;\n vector<char> v;\n for(auto &i: s){\n if(i==\'(\'){\n v.push_back(i);\n }else{\n if(v.empty())ans++;\n else{\n v.pop_ba... | 4 | 0 | ['String', 'Stack', 'Greedy', 'C++'] | 0 |
minimum-add-to-make-parentheses-valid | Simple Java Solution Using Stack | simple-java-solution-using-stack-by-vand-xj03 | \nclass Solution {\n public int minAddToMakeValid(String s) {\n \n Stack<Character> stk = new Stack();\n for(int i=0;i<s.length();i++){\n | vandittalwadia | NORMAL | 2023-02-03T07:54:44.899910+00:00 | 2023-02-03T07:59:38.023790+00:00 | 423 | false | ```\nclass Solution {\n public int minAddToMakeValid(String s) {\n \n Stack<Character> stk = new Stack();\n for(int i=0;i<s.length();i++){\n \n if(!stk.isEmpty() && s.charAt(i)==\')\' && stk.peek()==\'(\'){\n stk.pop();\n }\n else{\n ... | 4 | 0 | ['Stack', 'Java'] | 0 |
minimum-add-to-make-parentheses-valid | C++ Solution in O(n) time | c-solution-in-on-time-by-lordmavles-3qmc | \nclass Solution {\npublic:\n int minAddToMakeValid(string s) {\n int cc = 0, cur = 0;\n for(auto i: s){\n if(i == \'(\') ++cur;\n | lordmavles | NORMAL | 2022-07-21T09:34:14.012961+00:00 | 2022-07-21T09:34:14.013003+00:00 | 80 | false | ```\nclass Solution {\npublic:\n int minAddToMakeValid(string s) {\n int cc = 0, cur = 0;\n for(auto i: s){\n if(i == \'(\') ++cur;\n else --cur;\n if(cur < 0) ++cc, ++cur;\n }\n cc += cur;\n return cc;\n }\n};\n``` | 4 | 0 | [] | 0 |
minimum-add-to-make-parentheses-valid | C++ || simple stack solution || faster than 100% | c-simple-stack-solution-faster-than-100-6ldr9 | \nclass Solution {\npublic:\n int minAddToMakeValid(string s) {\n int ans = s.length();\n stack<char> st;\n for(int i=0;i<s.length();i++ | ritamsadhu | NORMAL | 2022-03-06T21:09:02.798091+00:00 | 2022-03-06T21:09:45.045881+00:00 | 113 | false | ```\nclass Solution {\npublic:\n int minAddToMakeValid(string s) {\n int ans = s.length();\n stack<char> st;\n for(int i=0;i<s.length();i++)\n {\n if(s[i]==\')\')\n {\n if(!st.empty() && st.top()==\'(\')\n {\n st.pop()... | 4 | 0 | ['Stack'] | 0 |
minimum-add-to-make-parentheses-valid | Easy for Beginner in C++ || Stack | easy-for-beginner-in-c-stack-by-luciferr-exjw | class Solution {\npublic:\n\n\n int minAddToMakeValid(string s){\n stack st;\n int count=0;\n for(int i=0;i<s.length();i++){\n | luciferraturi | NORMAL | 2021-09-20T16:46:39.102306+00:00 | 2021-09-22T18:38:36.903629+00:00 | 65 | false | class Solution {\npublic:\n\n\n int minAddToMakeValid(string s){\n stack<int> st;\n int count=0;\n for(int i=0;i<s.length();i++){\n if(s[i]==\'(\'){\n st.push(i);\n }\n else if(s[i]==\')\'){\n if(st.empty()){\n cou... | 4 | 0 | [] | 0 |
minimum-add-to-make-parentheses-valid | [Python3] Simple Solution | python3-simple-solution-by-voidcupboard-p4ma | \nclass Solution:\n def minAddToMakeValid(self, s: str) -> int:\n count = 0\n \n x = y = 0\n \n for i in s:\n i | VoidCupboard | NORMAL | 2021-05-26T01:50:57.147014+00:00 | 2021-05-26T01:50:57.147052+00:00 | 209 | false | ```\nclass Solution:\n def minAddToMakeValid(self, s: str) -> int:\n count = 0\n \n x = y = 0\n \n for i in s:\n if(i == \'(\'):\n x += 1\n else:\n x -= 1\n \n if(x < 0):\n count += 1\n ... | 4 | 1 | ['Python3'] | 1 |
minimum-add-to-make-parentheses-valid | 0-ms |100% faster | C++ Solution | 0-ms-100-faster-c-solution-by-emiya100-syps | ****\n```\nclass Solution {\npublic:\n int minAddToMakeValid(string S) {\n int i,n=S.size(),l=0,r=0,c=0;\n for(i=0;i<n;i++)\n {\n | emiya100 | NORMAL | 2021-01-08T09:47:28.416677+00:00 | 2021-01-08T09:47:28.416727+00:00 | 141 | false | ****\n```\nclass Solution {\npublic:\n int minAddToMakeValid(string S) {\n int i,n=S.size(),l=0,r=0,c=0;\n for(i=0;i<n;i++)\n {\n if(S[i]==\'(\')\n {\n l++;\n }\n else\n {\n if(l==0)\n {\n ... | 4 | 0 | [] | 0 |
minimum-add-to-make-parentheses-valid | Python 3 using stack simple solution with explanation | python-3-using-stack-simple-solution-wit-h4pk | \nclass Solution:\n def minAddToMakeValid(self, S: str) -> int:\n _stack=[] #initilized a new stack\n for bracke | mintukrish | NORMAL | 2020-10-18T08:09:52.301325+00:00 | 2020-10-18T08:09:52.301358+00:00 | 291 | false | ```\nclass Solution:\n def minAddToMakeValid(self, S: str) -> int:\n _stack=[] #initilized a new stack\n for bracket in S: # looping through all the brackets in S, storing each in bracket variable\n if(len(_stack)==0): # ... | 4 | 0 | ['Stack', 'Python3'] | 0 |
minimum-add-to-make-parentheses-valid | Straightforward C++ (0 ms), O(N), with explanation | straightforward-c-0-ms-on-with-explanati-r3s4 | The idea is to keep track of number of unused \'(\' in addition to keep track of extra \')\'\nSo lets say we use open and cnt variables for this goal, respecti | ashkanxy | NORMAL | 2018-12-23T18:41:30.988846+00:00 | 2018-12-23T18:41:30.988890+00:00 | 707 | false | The idea is to keep track of number of unused \'(\' in addition to keep track of extra \')\'\nSo lets say we use open and cnt variables for this goal, respectively.\nMove forward through the string. \n* Once you reach a \'(\' , increament the open variable.\n* Once you reach the \')\', \n\t* If the open variable is eq... | 4 | 0 | [] | 0 |
minimum-add-to-make-parentheses-valid | Java clear one-pass stack solution with expalantion | java-clear-one-pass-stack-solution-with-d0ho3 | We need to add as many parentheses as left after finding and removing all valid pairs.\nFor example:\n()))((()())(\n\nlet\'s delete all valid pairs:\n()))((()() | olsh | NORMAL | 2018-10-14T18:20:47.521172+00:00 | 2018-10-14T18:20:47.521214+00:00 | 77 | false | We need to add as many parentheses as left after finding and removing all valid pairs.\nFor example:\n()))((()())(\n\nlet\'s delete all valid pairs:\n()))((()())( -> \n**()** ))((()())( ->\n))(( **()** ())( ->\n))(( **()** )( ->\n))( **()** ( ->\n))((\n\nWe can add 4 more parentheses to make it valid (one for each pare... | 4 | 0 | [] | 0 |
minimum-add-to-make-parentheses-valid | Python Solution | python-solution-by-dipanjan0013-cpiu | IntuitionStack :)ApproachJust go through the string. If "(" comes, add it to the stack. In case of ")", if the stack is empty or the previous element is also ") | dipanjan0013 | NORMAL | 2025-04-06T19:08:31.626286+00:00 | 2025-04-06T20:18:21.745354+00:00 | 43 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Stack :)
# Approach
<!-- Describe your approach to solving the problem. -->
Just go through the string. If "(" comes, add it to the stack. In case of ")", if the stack is empty or the previous element is also ")", add it to the stack. And i... | 3 | 0 | ['Python3'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.