-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
feat: add solutions to lc problem: No.1762 #1716
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
Merged
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
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
29 changes: 14 additions & 15 deletions
solution/1700-1799/1762.Buildings With an Ocean View/Solution.cpp
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,16 +1,15 @@ | ||
| class Solution { | ||
| public: | ||
| vector<int> findBuildings(vector<int>& heights) { | ||
| int mx = 0; | ||
| vector<int> ans; | ||
| for (int i = heights.size() - 1; ~i; --i) { | ||
| int v = heights[i]; | ||
| if (mx < v) { | ||
| ans.push_back(i); | ||
| mx = v; | ||
| } | ||
| } | ||
| reverse(ans.begin(), ans.end()); | ||
| return ans; | ||
| } | ||
| class Solution { | ||
| public: | ||
| vector<int> findBuildings(vector<int>& heights) { | ||
| vector<int> ans; | ||
| int mx = 0; | ||
| for (int i = heights.size() - 1; ~i; --i) { | ||
| if (heights[i] > mx) { | ||
| ans.push_back(i); | ||
| mx = heights[i]; | ||
| } | ||
| } | ||
| reverse(ans.begin(), ans.end()); | ||
| return ans; | ||
| } | ||
| }; |
8 changes: 3 additions & 5 deletions
solution/1700-1799/1762.Buildings With an Ocean View/Solution.go
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,15 +1,13 @@ | ||
| func findBuildings(heights []int) []int { | ||
| func findBuildings(heights []int) (ans []int) { | ||
| mx := 0 | ||
| ans := []int{} | ||
| for i := len(heights) - 1; i >= 0; i-- { | ||
| v := heights[i] | ||
| if mx < v { | ||
| if v := heights[i]; v > mx { | ||
| ans = append(ans, i) | ||
| mx = v | ||
| } | ||
| } | ||
| for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 { | ||
| ans[i], ans[j] = ans[j], ans[i] | ||
| } | ||
| return ans | ||
| return | ||
| } |
27 changes: 14 additions & 13 deletions
solution/1700-1799/1762.Buildings With an Ocean View/Solution.java
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,14 +1,15 @@ | ||
| class Solution { | ||
| public int[] findBuildings(int[] heights) { | ||
| int mx = 0; | ||
| LinkedList<Integer> ans = new LinkedList<>(); | ||
| for (int i = heights.length - 1; i >= 0; --i) { | ||
| int v = heights[i]; | ||
| if (mx < v) { | ||
| ans.addFirst(i); | ||
| mx = v; | ||
| } | ||
| } | ||
| return ans.stream().mapToInt(i -> i).toArray(); | ||
| } | ||
| class Solution { | ||
| public int[] findBuildings(int[] heights) { | ||
| int n = heights.length; | ||
| List<Integer> ans = new ArrayList<>(); | ||
| int mx = 0; | ||
| for (int i = heights.length - 1; i >= 0; --i) { | ||
| if (heights[i] > mx) { | ||
| ans.add(i); | ||
| mx = heights[i]; | ||
| } | ||
| } | ||
| Collections.reverse(ans); | ||
| return ans.stream().mapToInt(Integer::intValue).toArray(); | ||
| } | ||
| } |
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
19 changes: 9 additions & 10 deletions
solution/1700-1799/1762.Buildings With an Ocean View/Solution.py
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,10 +1,9 @@ | ||
| class Solution: | ||
| def findBuildings(self, heights: List[int]) -> List[int]: | ||
| mx = 0 | ||
| ans = [] | ||
| for i in range(len(heights) - 1, -1, -1): | ||
| v = heights[i] | ||
| if mx < v: | ||
| ans.append(i) | ||
| mx = v | ||
| return ans[::-1] | ||
| class Solution: | ||
| def findBuildings(self, heights: List[int]) -> List[int]: | ||
| ans = [] | ||
| mx = 0 | ||
| for i in range(len(heights) - 1, -1, -1): | ||
| if heights[i] > mx: | ||
| ans.append(i) | ||
| mx = heights[i] | ||
| return ans[::-1] |
11 changes: 11 additions & 0 deletions
solution/1700-1799/1762.Buildings With an Ocean View/Solution.ts
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,11 @@ | ||
| function findBuildings(heights: number[]): number[] { | ||
| const ans: number[] = []; | ||
| let mx = 0; | ||
| for (let i = heights.length - 1; ~i; --i) { | ||
| if (heights[i] > mx) { | ||
| ans.push(i); | ||
| mx = heights[i]; | ||
| } | ||
| } | ||
| return ans.reverse(); | ||
| } |
Oops, something went wrong.
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.