Skip to content

Instantly share code, notes, and snippets.

@Desolve
Desolve / 0784 Letter Case Permutation.py
Created January 17, 2021 08:27
0784 Letter Case Permutation
class Solution:
def letterCasePermutation(self, S: str) -> List[str]:
res = []
def helper(a, pos):
if pos == len(a):
res.append(''.join(a))
else:
helper(a, pos + 1)
if a[pos].isalpha():
a[pos] = a[pos].upper()
@Desolve
Desolve / 0784 Letter Case Permutation.java
Last active January 17, 2021 08:20
0784 Letter Case Permutation
class Solution {
public List<String> letterCasePermutation(String S) {
List<String> res = new ArrayList<>();
char[] a = S.toLowerCase().toCharArray();
helper(a, 0, res);
return res;
}
void helper(char[] a, int pos, List<String> res){
if(pos == a.length){
@Desolve
Desolve / 0881 Boats to Save People.java
Last active January 13, 2021 13:26
0881 Boats to Save People
// O(nlogn) solution
class Solution {
public int numRescueBoats(int[] people, int limit) {
Arrays.sort(people);
int l = 0, r = people.length - 1, res = 0;
while (l <= r) {
if (people[l] + people[r] <= limit) ++l;
--r;
++res;
}
@Desolve
Desolve / 0881 Boats to Save People.py
Last active January 13, 2021 13:26
0881 Boats to Save People
class Solution:
def numRescueBoats(self, people: List[int], limit: int) -> int:
people.sort()
l, r = 0, len(people) - 1
res = 0
while l <= r:
if people[l] + people[r] <= limit: l += 1
r -= 1
res += 1
@Desolve
Desolve / 1640 Check Array Formation Through Concatenation.java
Created January 2, 2021 13:53
1640 Check Array Formation Through Concatenation
class Solution {
public boolean canFormArray(int[] arr, int[][] pieces) {
HashMap idx = new HashMap<Integer, Integer>();
for (int i = 0; i < pieces.length; ++i) {
idx.put(pieces[i][0], i);
}
int cnt = 0;
while (cnt < arr.length) {
if (!idx.containsKey(arr[cnt])) return false;
int p = (int) idx.get(arr[cnt]);
@Desolve
Desolve / 1640 Check Array Formation Through Concatenation.py
Created January 2, 2021 13:53
1640 Check Array Formation Through Concatenation
class Solution:
def canFormArray(self, arr: List[int], pieces: List[List[int]]) -> bool:
idx = {}
for i in range(len(pieces)):
idx[pieces[i][0]] = i
cnt = 0
while cnt < len(arr):
if arr[cnt] not in idx:
return False
p = idx[arr[cnt]]
@Desolve
Desolve / 1379 Find a Corresponding Node of a Binary Tree in a Clone of That Tree.java
Created January 2, 2021 13:03
1379 Find a Corresponding Node of a Binary Tree in a Clone of That Tree
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
@Desolve
Desolve / 1379 Find a Corresponding Node of a Binary Tree in a Clone of That Tree.py
Created January 2, 2021 13:00
1379 Find a Corresponding Node of a Binary Tree in a Clone of That Tree
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode:
def helper(n: TreeNode, v: int) -> TreeNode:
@Desolve
Desolve / 1544 Make The String Great.py
Created December 12, 2020 06:08
1544 Make The String Great
class Solution:
def makeGood(self, s: str) -> str:
if len(s) < 2: return s
st = []
for c in s:
if st and abs(ord(st[-1]) - ord(c)) == 32:
st.pop()
else:
st.append(c)
return ''.join(st)
@Desolve
Desolve / 1544 Make The String Great.java
Created December 12, 2020 06:08
1544 Make The String Great
class Solution {
public String makeGood(String s) {
Stack<Character> st = new Stack<Character>();
StringBuilder res = new StringBuilder();
for (char c : s.toCharArray()) {
if (!st.isEmpty() && Math.abs(st.peek() - c) == 32) {
st.pop();
} else {
st.push(c);
}