You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You're given a string 'S' consisting of "{", "}", "(", ")", "[" and "]" .
435
+
436
+
Return true if the given string 'S' is balanced, else return false.
437
+
438
+
For example:
439
+
'S' = "{}()".
440
+
441
+
There is always an opening brace before a closing brace i.e. '{' before '}', '(' before ').
442
+
So the 'S' is Balanced.
443
+
444
+
```py
445
+
defisValidParenthesis(s: str) -> bool:
446
+
# Write your code here
447
+
stack=[]
448
+
for c in s:
449
+
if c=='('or c=='['or c=='{':
450
+
stack.append(c)
451
+
elif c==')':
452
+
if stack and stack[-1]=='(':
453
+
stack.pop()
454
+
else:
455
+
returnFalse
456
+
elif c==']':
457
+
if stack and stack[-1]=='[':
458
+
stack.pop()
459
+
else:
460
+
returnFalse
461
+
elif c=='}':
462
+
if stack and stack[-1]=='{':
463
+
stack.pop()
464
+
else:
465
+
returnFalse
466
+
returnnot stack
467
+
```
468
+
469
+
### Day 17: Implement Upper Bound
470
+
471
+
You are given a sorted array ‘arr’ containing ‘n’ integers and an integer ‘x’.Implement the ‘upper bound’ function to find the index of the upper bound of 'x' in the array.
472
+
473
+
Note:
474
+
475
+
1. The upper bound in a sorted array is the index of the first value that is greater than a given value.
476
+
2. If the greater value does not exist then the answer is 'n', Where 'n' is the size of the array.
477
+
3. Try to write a solution that runs in log(n) time complexity.
478
+
479
+
Example:
480
+
481
+
Input : ‘arr’ = {2,4,6,7} and ‘x’ = 5,
482
+
483
+
Output: 2
484
+
485
+
Explanation: The upper bound of 5 is 6 in the given array, which is at index 2 (0-indexed).
Explanation: Starting from the element in the first row and the first column, traverse from left to right (1 3 5 7), then top to bottom (20 60), then right to left (34 30 23), then bottom to up (10) and then left to right (11 16).
You are given a matrix 'MATRIX' of dimension 'N' x 'M'. Your task is to make all the elements of row 'i' and column 'j' equal to 0 if any element in the ith row or jth column of the matrix is 0.
first_col_have_zero=any(matrix[i][0]==0for i inrange(n))
557
+
558
+
for i inrange(1,n):
559
+
for j inrange(1,m):
560
+
if matrix[i][j]==0:
561
+
matrix[0][j]=0
562
+
matrix[i][0]=0
563
+
for i inrange(1,n):
564
+
for j inrange(1,m):
565
+
if matrix[0][j]==0or matrix[i][0]==0:
566
+
matrix[i][j]=0
567
+
if first_row_have_zero:
568
+
for j inrange(m):
569
+
matrix[0][j]=0
570
+
if first_col_have_zero:
571
+
for i inrange(n):
572
+
matrix[i][0]=0
573
+
```
574
+
575
+
### Day 20: Pascal's Triangle
576
+
577
+
You are given an integer N. Your task is to return a 2-D ArrayList containing the pascal’s triangle till the row N.
578
+
579
+
A Pascal's triangle is a triangular array constructed by summing adjacent elements in preceding rows. Pascal's triangle contains the values of the binomial coefficient.
580
+
581
+
```py
582
+
defprintPascal(n:int):
583
+
# Write your code here.
584
+
# Return a list of lists.
585
+
res=[[1]]
586
+
for i inrange(1,n):
587
+
res.append([1]+[res[i-1][j]+res[i-1][j-1] for j inrange(1,i)]+[1])
588
+
return res
589
+
```
590
+
591
+
### Day 21: Number of Inversions
592
+
593
+
There is an integer array ‘A’ of size ‘N’.
594
+
595
+
Number of inversions in an array can be defined as the number of pairs of ‘i’, ‘j’ such that ‘i’ < ‘j’ and ‘A[i]’ > ‘A[j]’.
596
+
597
+
You must return the number of inversions in the array.
598
+
599
+
For example,
600
+
Input:
601
+
A = [5, 3, 2, 1, 4], N = 5
602
+
Output:
603
+
7
604
+
Explanation:
605
+
The pairs satisfying the condition for inversion are (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), and (3, 4).
606
+
The number of inversions in the array is 7.
607
+
608
+
```py
609
+
from typing import*
610
+
611
+
classSolution:
612
+
defmergeSort(self,arr,low,high):
613
+
count_inversion=0
614
+
if low<high:
615
+
mid=(low+high)//2
616
+
count_inversion+=self.mergeSort(arr,low,mid)
617
+
count_inversion+=self.mergeSort(arr,mid+1,high)
618
+
count_inversion+=self.merge(arr,low,mid,high)
619
+
return count_inversion
620
+
defmerge(self,arr,low,mid,high):
621
+
count_inversion=0
622
+
temp=[0]*(high-low+1)
623
+
i=low
624
+
j=mid+1
625
+
k=0
626
+
while i<=mid and j<=high:
627
+
if arr[i]<=arr[j]:
628
+
temp[k]=arr[i]
629
+
k+=1
630
+
i+=1
631
+
else:
632
+
temp[k]=arr[j]
633
+
k+=1
634
+
j+=1
635
+
count_inversion+=mid-i+1#se agrega la inversion
636
+
while i<=mid:
637
+
temp[k]=arr[i]
638
+
k+=1
639
+
i+=1
640
+
while j<=high:
641
+
temp[k]=arr[j]
642
+
k+=1
643
+
j+=1
644
+
for i inrange(low,high+1):
645
+
arr[i]=temp[i-low]
646
+
return count_inversion
647
+
defnumberOfInversions(a : List[int], n : int) -> int:
0 commit comments