-
-
Notifications
You must be signed in to change notification settings - Fork 257
Student rank at Hackerrank contest #412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Keshav-babu
wants to merge
2
commits into
chandrikadeb7:main
Choose a base branch
from
Keshav-babu:Keshav_task1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,3 +1,34 @@ | ||
| Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a container, such that the container contains the most water. | ||
| Given an array of n non-negative integers a1, a2, ..., an , where each element represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of a line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a container, such that the container contains the most water. | ||
|
|
||
| Note: You may not slant the container. | ||
|
|
||
| Example:<br> | ||
| a) Input: n=9<br> | ||
| lines = [1,8,6,2,5,4,8,3,7]<br> | ||
| Output: 49<br> | ||
| Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water the container can contain is 49 which lies between the index(1,8) i.e, between the line 8 at 1st position and line 7 at 8th position. | ||
|
|
||
|
|
||
| b) Input: n=5<br> | ||
| lines = [4,3,2,1,4]<br> | ||
| Output: 16<br> | ||
| Explanation: The container containing most water lies between the index 0 and 4 i.e, between the line 4 at 0th position and line 4 at 4th position. | ||
|
|
||
|
|
||
| Approach: Two Pointer Approach | ||
|
|
||
| The max area is calculated by the following formula: | ||
| Area= (j - i) * min(lines[i], lines[j])<br> | ||
|
|
||
| We should choose (i, j) so that Area is max. Note that i, j go through the range (1, n) and j > i. | ||
| The simple way is to take all possibilities of (i, j) and compare all obtained Area. The time complexity will be O(n)^2. | ||
|
|
||
| But, What we gonna do is to choose all possibilities of (i, j) in a wise way. If: | ||
| lines[i] < lines[j] we will check the next (i+1, j) (or move i to the right) | ||
| lines[i] >= lines[j] we will check the next (i, j-1) (or move j to the left) | ||
| Here is the explaination for that: | ||
| When lines[i] < lines[j] , we don't need to calculate all (i, j-1), (i, j-2), .... Why? because these max areas will be smaller than our Area at (i, j). | ||
|
|
||
| NOTE: The Time complexity of this solution is O(n). | ||
|
|
||
|
|
||
This file contains hidden or 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,35 @@ | ||
| var arr=[['rancho',45],['chatur',32],['raju',30],['farhan',28],['virus',32],['joy',45]] | ||
| var N=arr.length; | ||
| for(var i=0;i<N;i++){ | ||
| var min=i | ||
| for(var j=i+1;j<N;j++){ | ||
| if(arr[min][1]<=arr[j][1]){ | ||
| min=j | ||
| } | ||
| } | ||
| var temp=arr[i] | ||
| arr[i]=arr[min] | ||
| arr[min]=temp | ||
|
|
||
| } | ||
| var rank=1 | ||
| var a=1 | ||
| for(var i=0;i<N;i++){ | ||
| if(i>0){ | ||
| if(arr[i-1][1]==arr[i][1]){ | ||
| console.log(rank+" "+arr[i][0]) | ||
| a++ | ||
| } | ||
| else{ | ||
| rank=rank+a | ||
| console.log(rank+" "+arr[i][0]) | ||
| a=1 | ||
| } | ||
|
|
||
|
|
||
| }else{ | ||
| console.log(rank+" "+arr[i][0]) | ||
|
|
||
|
|
||
| } | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do add a newline at EOF |
||
This file contains hidden or 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,9 @@ | ||
| You are given name and marks of N different students in a hackerrank contest. Your task is to write a program that makes leaderboard of the students under following conditions: | ||
|
|
||
| - If two students get same marks they get same rank | ||
|
|
||
| - The student placed next to the same marks students will get the rank skipping the intermediate ranks. | ||
|
|
||
| Refer to the sample test case for better understanding | ||
|
|
||
| Note : You cannot use built-in sort function. Using that can lead to disqualification. Write your own sorting algorithm |
This file contains hidden or 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,23 @@ | ||
| //Given a squarematrixof sizeN, turn it by 90 degrees in an anti-clockwise direction, as shown in sample input. | ||
| // Output | ||
| // You have to display therotated matrix. | ||
|
|
||
| function rotateBy90(N, matrix){ | ||
| //write code here | ||
| for(var i=R-1;i>=0;i--){ | ||
| var out=[] | ||
| for(var j=0;j<R;j++){ | ||
| out.push(matrix[j][i]) | ||
| } | ||
| console.log(out.join(" ")) | ||
| } | ||
| } | ||
| var matrix=[ | ||
| [1,2,3,4], | ||
| [5,6,7,8], | ||
| [1,2,3,4], | ||
| [5,6,7,8] | ||
| ] | ||
| var len=matrix.length | ||
| var out=rotateBy90(len,matrix) | ||
| console.log(out) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here as well, newline at EOF |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have a corresponding solution file for this?