solve more problems
swadhinbiswas committed 1 years ago
7c666b2
1 parent 1428e25
commit 7c666b2
7 files changed
+123 -2
Showing 7 changed files+123-2
▼169. Majority Element.py
+32-0
| @@ -0,0 +1,32 @@ | ||
1 | +# Using BruteForce | |
2 | +from typing import List | |
3 | +from collections import defaultdict | |
4 | +class Solution: | |
5 | + #7ms | |
6 | + def majorityElement(self, nums: List[int]) -> int: | |
7 | + seen={} | |
8 | + for number in nums: | |
9 | + if number not in seen: | |
10 | + seen[number]=nums.count(number) | |
11 | + | |
12 | + return max(seen,key=seen.get) | |
13 | + | |
14 | + #12ms | |
15 | + def majorityElement(self, nums: List[int]) -> int: | |
16 | + count_map = defaultdict(int) | |
17 | + for num in nums: | |
18 | + count_map[num] += 1 | |
19 | + return max(count_map, key=count_map.get) | |
20 | + | |
21 | + | |
22 | + | |
23 | + | |
24 | + | |
25 | +nums=[1,1,1,2,2,3] | |
26 | + | |
27 | +x=Solution().majorityElement(nums) | |
28 | +print(x) | |
29 | + | |
30 | + | |
31 | + | |
32 | + | |
0 | 33 | |
▼9. Palindrome Number.py
+27-0
| @@ -0,0 +1,27 @@ | ||
1 | +class Solution(object): | |
2 | + def isPalindrome(self, x: int) -> bool: | |
3 | + invertnumber:int =0 | |
4 | + y=x | |
5 | + while x>0: | |
6 | + last=x%10 | |
7 | + invertnumber=(invertnumber*10)+last | |
8 | + x=x//10 | |
9 | + return invertnumber==y | |
10 | + | |
11 | + | |
12 | + | |
13 | + | |
14 | + | |
15 | + | |
16 | + | |
17 | +# Aooroch 2 | |
18 | + | |
19 | + | |
20 | +class Solutions(object): | |
21 | + def isPalindrome(self, x: int) -> bool: | |
22 | + if x<0: | |
23 | + return False | |
24 | + | |
25 | + if x!=0 and x%10==0: | |
26 | + return False | |
27 | + half_number= | |
0 | 28 | |
▼BrouteForceforMaxSUbarray.py
+7-2
| @@ -1,8 +1,8 @@ | ||
1 | -import math | |
1 | + | |
2 | 2 | |
3 | 3 | class Solution(object): |
4 | 4 | def maxSubarray(self,array): |
5 | - max_sum = -math.inf | |
5 | + max_sum = -1 | |
6 | 6 | for start in range(len(array)): |
7 | 7 | arraysum=0 |
8 | 8 | for end in range(start,len(array)): |
| @@ -12,3 +12,8 @@ class Solution(object): | ||
12 | 12 | return max_sum |
13 | 13 | |
14 | 14 | |
15 | + | |
16 | + | |
17 | +x=Solution().maxSubarray([3,-4,5,4,-1,12,-8]) | |
18 | +print(x) | |
19 | + | |
15 | 20 | |
▼DSA/pairsum.py
+26-0
| @@ -0,0 +1,26 @@ | ||
1 | +from typing import List | |
2 | + | |
3 | +class Pair(object): | |
4 | + def pairsum(self,numer:List[int],target:int)->List[int]: | |
5 | + for i in range(len(number)): | |
6 | + for j in range(i+1,number): | |
7 | + if numbr[i]+number[j]==target: | |
8 | + return i,j | |
9 | + | |
10 | + | |
11 | + def pairsumalternet(self,number:List[int],target:int)->List[int]: | |
12 | + seen={} | |
13 | + for i, num in enumerate(number): | |
14 | + complement=target-num | |
15 | + if complement in seen : | |
16 | + return [seen[complement],i] | |
17 | + seen[num]=i | |
18 | + return [] | |
19 | + | |
20 | + | |
21 | + | |
22 | + | |
23 | + | |
24 | + | |
25 | +x=Pair().pairsumalternet([1,2,3,4,5],9) | |
26 | +print(x) | |
0 | 27 | |
▼DSA/test.ipynb
+12-0
| @@ -61,6 +61,18 @@ | ||
61 | 61 | "source": [ |
62 | 62 | "print(-12>>1)" |
63 | 63 | ] |
64 | + }, | |
65 | + { | |
66 | + "cell_type": "markdown", | |
67 | + "metadata": {}, | |
68 | + "source": [] | |
69 | + }, | |
70 | + { | |
71 | + "cell_type": "code", | |
72 | + "execution_count": null, | |
73 | + "metadata": {}, | |
74 | + "outputs": [], | |
75 | + "source": [] | |
64 | 76 | } |
65 | 77 | ], |
66 | 78 | "metadata": { |
67 | 79 | |
▼MaxSumusing(Khadangel).py
+19-0
| @@ -0,0 +1,19 @@ | ||
1 | +import math | |
2 | +from typing import List | |
3 | +class Solution(object): | |
4 | + def maxSubArray(self,numbers:List[int])-> int: | |
5 | + currentsum,maxsum = 0,-math.inf | |
6 | + | |
7 | + for start in range(len(numbers)): | |
8 | + currentsum+=numbers[start] | |
9 | + maxsum=max(currentsum,maxsum) | |
10 | + if currentsum<0: | |
11 | + currentsum=0 | |
12 | + return maxsum | |
13 | + | |
14 | + | |
15 | + | |
16 | +solve=Solution().maxSubArray([-2,1,-3,4,-1,2,1,-5,4]) | |
17 | + | |
18 | +print(solve) | |
19 | + | |
0 | 20 | |
▼test.cs
+0-0
0 | 0 |