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
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
#include <algorithm> | |
#include <cassert> | |
#include <iostream> | |
#include <vector> | |
using namespace std; | |
// From https://leetcode.com/discuss/36807/c-8-ms-kmp-based-o-n-time-%26-o-n-memory-solution?show=40650#a40650 | |
// O(n^2), 4ms. | |
// May fail test cases such as "abababababababababababababababababababababab". |
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
# Do binary search for each row. O(mlgn). | |
# Run time: 224 ms | |
class Solution: | |
# @param {integer[][]} matrix | |
# @param {integer} target | |
# @return {boolean} | |
def searchMatrix(self, matrix, target): | |
m, n = len(matrix), len(matrix[0]) | |
# Edge case. | |
if m == 0 or 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
# Maintain a deque where the first element is the max | |
# of the window. | |
class Solution: | |
# @param {integer[]} nums | |
# @param {integer} k | |
# @return {integer[]} | |
def maxSlidingWindow(self, nums, k): | |
results = [] | |
# Element in the window should be (index, val). | |
window = collections.deque() |
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
// Simple recursive algorithm. Didn't use DP considering that | |
// it needs to store a list for every DP entry (n * n). | |
class Solution { | |
public: | |
vector<int> diffWaysToCompute(string input) { | |
tokens_ = parse(input); | |
return diffWaysToCompute(0, tokens_.size()); | |
} | |
private: |
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 singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode(int x) : val(x), next(NULL) {} | |
* }; | |
*/ | |
// Very clever algorithm, from |
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
#include <iostream> | |
#include <vector> | |
using namespace std; | |
// Given the info from the problem statement. | |
const int kInf = 10001; | |
struct Edge { | |
int src_; |
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
#include <cstdio> | |
#include <iostream> | |
#include <utility> | |
#include <vector> | |
using namespace std; | |
/* | |
Note all operations on arrays/vectors start from index 1. | |
*/ |
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: | |
int rob(vector<int>& nums) { | |
int nums_len = nums.size(); | |
if (nums_len == 0) { | |
return 0; | |
} | |
else if (nums_len == 1) { | |
return nums[0]; | |
} |