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
class Solution(object): | |
def generateParenthesis(self, n): | |
""" | |
:type n: int | |
:rtype: List[str] | |
""" | |
self.valid_parens = [] | |
self.n = n | |
# 0 open, 0 closed, recursively build valid strings. | |
self.buildParenString(0, 0, '') |
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
import sys | |
def find_min_candies(ratings): | |
"""ratings: A list of ratings""" | |
sz = len(ratings) | |
# Records number of continuous elements less than | |
# the current rating from left and right. | |
less_from_left, less_from_right = [0] * sz, [0] * sz | |
for i in range(1, sz): |
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
def max_score(bricks): | |
"""bricks: A list of bricks.""" | |
sz = len(bricks) | |
# DP algorithm. See the editorial. | |
# https://www.hackerrank.com/challenges/play-game/editorial | |
max_score_from = [0] * sz | |
max_score_from[-1] = bricks[-1] | |
max_score_from[-2] = max_score_from[-1] + bricks[-2] | |
max_score_from[-3] = max_score_from[-2] + bricks[-3] |
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
/** | |
* Definition for binary tree | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
* }; | |
*/ | |
class Solution { |
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
class Solution { | |
public: | |
void merge(int A[], int m, int B[], int n) { | |
vector<int> sorted; | |
int i=0, j=0; | |
while (i < m && j < n) { | |
if (A[i] < B[j]) | |
sorted.push_back(A[i++]); | |
else | |
sorted.push_back(B[j++]); |
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
/**** | |
* from leetcode discussion on http://oj.leetcode.com/discuss/77/restore-ip-addresses?show=295#a295 | |
* answered by mitchelllc | |
* using 3 for loops | |
****/ | |
vector<string> restoreIpAddress(string s) { | |
vector<string> res; | |
if (s.size() > 12 || s.size() < 4) | |
return res; |
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
/** | |
* AC in one try! | |
**/ | |
class Solution { | |
public: | |
vector<vector<string>> partition(string s) { | |
str = s; | |
result.clear(); | |
sz = s.size(); |
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
class Solution: | |
# @param num, a list of integer | |
# @return an integer | |
def init(self): | |
self.endDict = {} | |
def combine(self, left, right): | |
leftEnd = self.endDict[left] | |
rightEnd = self.endDict[right] | |
self.endDict[left], self.endDict[right] = left, right |
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
class Solution { | |
public: | |
double pow(double x, int n) { | |
double result = 1; | |
if (n < 0) { | |
x = 1/x; | |
result *= x; | |
n = -(n+1); | |
} | |
if (n == 0) |
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
/** | |
* Definition for binary tree | |
* public class TreeNode { | |
* int val; | |
* TreeNode left; | |
* TreeNode right; | |
* TreeNode(int x) { val = x; } | |
* } | |
*/ | |
public class Solution { |
OlderNewer