Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Adding _1081.java#166

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
sushant-sinha wants to merge10 commits intofishercoder1534:master
base:master
Choose a base branch
Loading
fromsushant-sinha:adding-1081
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletionREADME.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1031,7 +1031,7 @@ _If you like this project, please leave me a star._ ★
| 319 |[Bulb Switcher](https://leetcode.com/problems/bulb-switcher/)| [Solution](../master/src/main/java/com/fishercoder/solutions/_319.java) | |Medium| Brainteaser
| 318 |[Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/)| [Solution](../master/src/main/java/com/fishercoder/solutions/_318.java) | |Medium|
| 317 |[Shortest Distance from All Buildings](https://leetcode.com/problems/shortest-distance-from-all-buildings/)| [Solution](../master/src/main/java/com/fishercoder/solutions/_317.java) | |Hard|
| 316 |[Remove Duplicate Letters](https://leetcode.com/problems/remove-duplicate-letters/)| [Solution](../master/src/main/java/com/fishercoder/solutions/_316.java) | |Hard| Stack, Recursion, Greedy
| 316 |[Remove Duplicate Letters](https://leetcode.com/problems/remove-duplicate-letters/)| [Solution](../master/src/main/java/com/fishercoder/solutions/_316.java) | |Medium| Stack, Recursion, Greedy
| 315 |[Count of Smaller Numbers After Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/)| [Solution](../master/src/main/java/com/fishercoder/solutions/_315.java) | |Hard| Tree
| 314 |[Binary Tree Vertical Order Traversal](https://leetcode.com/problems/binary-tree-vertical-order-traversal/)| [Solution](../master/src/main/java/com/fishercoder/solutions/_314.java) | |Medium| HashMap, BFS
| 313 |[Super Ugly Number](https://leetcode.com/problems/super-ugly-number/)| [Solution](../master/src/main/java/com/fishercoder/solutions/_313.java) | |Medium|
Expand Down
44 changes: 44 additions & 0 deletions_1081.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
package com.fishercoder.solutions;

import java.util.*;

// same as 316. Remove Duplicate Letters
// from https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/_316.java

public class _1002 {
public class Solution1 {
public String smallestSubsequence(String s) {
int[] res = new int[26]; //will contain number of occurences of character (i+'a')
boolean[] visited =
new boolean[26]; //will contain if character (i+'a') is present in current result Stack
char[] ch = s.toCharArray();
for (char c : ch) { //count number of occurences of character
res[c - 'a']++;
}
Deque<Character> st = new ArrayDeque<>(); // answer stack
int index;
for (char c : ch) {
index = c - 'a';
res[index]--; //decrement number of characters remaining in the string to be analysed
if (visited[index]) {
//if character is already present in stack, dont bother
continue;
}
//if current character is smaller than last character in stack which occurs later in the string again
//it can be removed and added later e.g stack = bc remaining string abc then a can pop b and then c
while (!st.isEmpty() && c < st.peek() && res[st.peek() - 'a'] != 0) {
visited[st.pop() - 'a'] = false;
}
st.push(c); //add current character and mark it as visited
visited[index] = true;
}

StringBuilder sb = new StringBuilder();
//pop character from stack and build answer string from back
while (!st.isEmpty()) {
sb.insert(0, st.pop());
}
return sb.toString();
}
}
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
package com.fishercoder.solutions;

public class _1961{
public class Solution1 {
public boolean isPrefixString(String s, String[] words) {

String t="";
int i=0;

while(t.length()<s.length() && i<words.length){
t+=words[i++];

if(t.equals(s))
return true;
}

return false;

}
}
}
24 changes: 24 additions & 0 deletionssrc/main/java/com/fishercoder/solutions/_881.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -57,4 +57,28 @@ public int numRescueBoats(int[] people, int limit) {
return boats;
}
}

public static class Solution2 {
public int numRescueBoats(int[] people, int limit) {
Arrays.sort(people);
int end = people.length - 1;
int start = 0;
int boatcount = 0;
while (end >= start) {
if (people[end] == limit) {
end--;
boatcount++;
} else if (people[end] + people[start] <= limit) {
start++;
end--;
boatcount++;
} else {
end--;
boatcount++;
}
}
return boatcount;
}
}

}

[8]ページ先頭

©2009-2025 Movatter.jp