-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ef737f
commit 96f63c3
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#User function Template for python3 | ||
|
||
class Solution: | ||
def kTop(self, arr, N, K): | ||
final_ans=[] | ||
top = [0 for i in range(K + 1)] | ||
freq = {i:0 for i in range(K + 1)} | ||
|
||
for m in range(N): | ||
if a[m] in freq.keys(): | ||
freq[a[m]] += 1 | ||
else: | ||
freq[a[m]] = 1 | ||
|
||
top[k] = a[m] | ||
i = top.index(a[m]) | ||
i -= 1 | ||
|
||
while i >= 0: | ||
if (freq[top[i]] < freq[top[i + 1]]): | ||
t = top[i] | ||
top[i] = top[i + 1] | ||
top[i + 1] = t | ||
|
||
elif ((freq[top[i]] == freq[top[i + 1]]) and (top[i] > top[i + 1])): | ||
t = top[i] | ||
top[i] = top[i + 1] | ||
top[i + 1] = t | ||
else: | ||
break | ||
i -= 1 | ||
|
||
i = 0 | ||
ans = [] | ||
while i < K and top[i] != 0: | ||
ans.append(top[i]) | ||
i += 1 | ||
|
||
final_ans += [ans] | ||
return final_ans | ||
|
||
#{ | ||
# Driver Code Starts | ||
|
||
|
||
t=int(input()) | ||
for _ in range(0,t): | ||
n,k=list(map(int,input().split())) | ||
a=list(map(int,input().split())) | ||
ob = Solution() | ||
ans=ob.kTop(a,n,k) | ||
for line in ans: | ||
print(*line) | ||
|
||
|
||
|
||
# } Driver Code Ends |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Time complexity - O(n * k) | ||
Space complexity - O(n) | ||
|
||
Was not possible for me to return the output as said in Java (ArrayList<Integer>); could had done if it was 2d ArrayList |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n^2) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Solution | ||
{ | ||
public int minFallingPathSum(int[][] A) | ||
{ | ||
final int n = A.length; | ||
|
||
for (int i = 1; i < n; ++i) | ||
for (int j = 0; j < n; ++j) | ||
{ | ||
int min = Integer.MAX_VALUE; | ||
|
||
for (int k = Math.max(0, j - 1); k < Math.min(n, j + 2); ++k) | ||
min = Math.min(min, A[i - 1][k]); | ||
|
||
A[i][j] += min; | ||
} | ||
|
||
return Arrays.stream(A[n - 1]).min().getAsInt(); | ||
} | ||
} |