File tree Expand file tree Collapse file tree 5 files changed +139
-0
lines changed
best-time-to-buy-and-sell-stock
encode-and-decode-strings
implement-trie-prefix-tree Expand file tree Collapse file tree 5 files changed +139
-0
lines changed Original file line number Diff line number Diff line change 1+ func maxProfit (prices []int ) int {
2+ ans := 0
3+ bought := prices [0 ]
4+ for i := 1 ; i != len (prices ); i ++ {
5+ ans = max (ans , prices [i ] - bought )
6+ bought = min (bought , prices [i ])
7+ }
8+ return ans
9+ }
Original file line number Diff line number Diff line change 1+ public class Solution {
2+ /*
3+ * @param strs: a list of strings
4+ * @return: encodes a list of strings to a single string.
5+ */
6+ public String encode (List <String > strs ) {
7+ // write your code here
8+ return strs .parallelStream ()
9+ .map (s ->
10+ s .chars ()
11+ .parallel ()
12+ .mapToObj (c -> new StringBuilder (String .format ("%02x" , c )))
13+ .collect (
14+ StringBuilder ::new ,
15+ StringBuilder ::append ,
16+ (a , b ) -> a .append (b )
17+ )
18+ ).collect (
19+ StringBuilder ::new ,
20+ (a , b ) -> a .append (b )
21+ .append (' ' ),
22+ (a , b ) -> a .append (b )
23+ ).toString ();
24+ }
25+
26+ /*
27+ * @param str: A string
28+ * @return: decodes a single string to a list of strings
29+ */
30+ public List <String > decode (String str ) {
31+ // write your code here
32+ final List <String > ans = new ArrayList <>();
33+ final StringTokenizer st = new StringTokenizer (str );
34+ while (st .hasMoreTokens ()) {
35+ final String s = st .nextToken ();
36+ final StringBuilder sb = new StringBuilder ();
37+ for (int i = 0 ; i < s .length (); i += 2 ) {
38+ sb .append ((char ) Integer .parseInt (s .substring (i , i + 2 ), 16 ));
39+ }
40+ ans .add (sb .toString ());
41+ }
42+ return ans ;
43+ }
44+ }
Original file line number Diff line number Diff line change 1+ use itertools:: Itertools ;
2+
3+ impl Solution {
4+ pub fn group_anagrams ( strs : Vec < String > ) -> Vec < Vec < String > > {
5+ let sorted_strings: Vec < String > = strs
6+ . iter ( )
7+ . map ( |s| {
8+ s. chars ( )
9+ . sorted ( )
10+ . collect ( )
11+ } ) . collect ( ) ;
12+ let inverted_sorted_strings: Vec < usize > = ( 0 ..sorted_strings. len ( ) )
13+ . sorted_by_key ( |& i| & sorted_strings[ i] )
14+ . collect ( ) ;
15+ let mut ans: Vec < Vec < String > > = vec ! [ ] ;
16+ let mut i = 0 ;
17+ let mut j;
18+ while i != sorted_strings. len ( ) {
19+ let mut u: Vec < String > = vec ! [ ] ;
20+ j = i;
21+ while j == i || j != inverted_sorted_strings. len ( ) && sorted_strings[ inverted_sorted_strings[ i] ] == sorted_strings[ inverted_sorted_strings[ j] ] {
22+ u. push ( strs[ inverted_sorted_strings[ j] ] . clone ( ) ) ;
23+ j += 1 ;
24+ }
25+ ans. push ( u) ;
26+ i = j
27+ }
28+ ans
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ class Trie {
2+
3+ final int n;
4+ final children = < int , Trie > {};
5+ bool ended = false ;
6+
7+ Trie ([this .n = 0 ]);
8+
9+ void insert (String word) {
10+ Trie u = this ;
11+ for (int rune in word.runes) {
12+ u = u.children
13+ .putIfAbsent (rune, () => Trie (rune));
14+ }
15+ u.ended = true ;
16+ }
17+
18+ bool search (String word) => _search (word)? .ended ?? false ;
19+
20+ bool startsWith (String prefix) => _search (prefix) != null ;
21+
22+ Trie ? _search (String word) {
23+ Trie ? u = this ;
24+ for (int rune in word.runes) {
25+ u = u! .children[rune];
26+ if (u == null ) {
27+ return null ;
28+ }
29+ }
30+ return u;
31+ }
32+ }
33+
34+ /**
35+ * Your Trie object will be instantiated and called as such:
36+ * Trie obj = Trie();
37+ * obj.insert(word);
38+ * bool param2 = obj.search(word);
39+ * bool param3 = obj.startsWith(prefix);
40+ */
Original file line number Diff line number Diff line change 1+ object Solution {
2+ def wordBreak (s : String , wordDict : List [String ]): Boolean = {
3+ val dp = Array .fill(s.length + 1 )(false ) // S(s, wordDict, word) = O(s.length)
4+ dp(0 ) = true
5+ (0 to s.length - 1 ).exists { i =>
6+ if (! dp(i)) false
7+ else wordDict.exists { word =>
8+ val j = i + word.length
9+ if (j <= s.length && ! dp(j) && s.substring(i, j) == word) {
10+ dp(j) = true // T(s, wordDict, word) = O(s.length * wordDict.length * word.length)
11+ }
12+ dp(s.length)
13+ }
14+ }
15+ }
16+ }
You can’t perform that action at this time.
0 commit comments