Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1fdde9a
Showing
12 changed files
with
398 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from collections import defaultdict | ||
class Solution: | ||
# @param {string[]} strs | ||
# @return {string[]} | ||
def anagrams(self, strs): | ||
dic = defaultdict(list) | ||
for s in strs: | ||
d = defaultdict(int) | ||
for c in s: | ||
d[c]+=1 | ||
keystr = ''.join([k+str(d[k]) for k in sorted(d.keys)]) | ||
dic[keystr].append(s) | ||
result = [] | ||
for k, v in dic.items: | ||
result.append(' '.join(v)) | ||
return result |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
select Name from Customers where id not in (select customerId from Orders) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
delete from Person where id not in | ||
(select minid from | ||
(select min(Id) as minid, count(*) as cnt from Person group by Email) | ||
as temp) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from collections import defaultdict | ||
class Solution: | ||
# @param {string} s1 | ||
# @param {string} s2 | ||
# @param {string} s3 | ||
# @return {boolean} | ||
def isInterleave(self, s1, s2, s3): | ||
# first they need the same composition | ||
counter = defaultdict(int) | ||
for c in s1+s2: | ||
counter[c]+=1 | ||
for c in s3: | ||
counter[c]-=1 | ||
for c in counter: | ||
if counter[c] != 0: | ||
return False | ||
self.memo = {} | ||
return self._isInterleave(s1, s2, s3) | ||
|
||
def _isInterleave(self, s1, s2, s3): | ||
if (s1, s2, s3) in self.memo: | ||
return self.memo[(s1, s2, s3)] | ||
|
||
if not s1: | ||
ret = (s2 == s3) | ||
self.memo[(s1, s2, s3)]=ret | ||
return ret | ||
if not s2: | ||
ret = (s1 == s3) | ||
self.memo[(s1, s2, s3)]=ret | ||
return ret | ||
|
||
ret = False | ||
if s3[0] == s1[0]: | ||
ret = ret or self._isInterleave(s1[1:], s2, s3[1:]) | ||
if s3[0] == s2[0]: | ||
ret = ret or self._isInterleave(s1, s2[1:], s3[1:]) | ||
self.memo[(s1, s2, s3)] = ret | ||
return ret |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from collections import defaultdict | ||
class Solution: | ||
# @param {string} s1 | ||
# @param {string} s2 | ||
# @param {string} s3 | ||
# @return {boolean} | ||
def isInterleave(self, s1, s2, s3): | ||
# first they need the same composition | ||
counter = defaultdict(int) | ||
for c in s1+s2: | ||
counter[c]+=1 | ||
for c in s3: | ||
counter[c]-=1 | ||
for c in counter: | ||
if counter[c] != 0: | ||
return False | ||
|
||
for perm in self.pick_permutation([], len(s1), len(s2)): | ||
s = s1 | ||
for i,pos in enumerate(perm): | ||
s = s[:pos+i]+s2[i]+s[pos+i:] | ||
if s==s3: | ||
return True | ||
return False | ||
|
||
def pick_permutation(self, current, m, n): | ||
if len(current)==n: | ||
yield current | ||
else: | ||
if not current: | ||
start = 0 | ||
else: | ||
start = current[-1] | ||
for i in range(start, m+1): | ||
new_current = current[:] | ||
new_current.append(i) | ||
for perm in self.pick_permutation(new_current,m,n): | ||
yield perm | ||
|
||
s1 = "aabcc" | ||
s2 = "dbbca" | ||
s3 = "aadbbcbcac" | ||
print Solution().isInterleave(s1, s2, s3) | ||
|
||
s1 = "abaaacbacaab" | ||
s2 = "bcccababccc" | ||
s3 ="bcccabaaaaabccaccbacabb" | ||
print Solution().isInterleave(s1, s2, s3) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from collections import defaultdict | ||
class Solution: | ||
# @param {string} s1 | ||
# @param {string} s2 | ||
# @param {string} s3 | ||
# @return {boolean} | ||
def isInterleave(self, s1, s2, s3): | ||
# first they need the same composition | ||
counter = defaultdict(int) | ||
for c in s1+s2: | ||
counter[c]+=1 | ||
for c in s3: | ||
counter[c]-=1 | ||
for c in counter: | ||
if counter[c] != 0: | ||
return False | ||
if not self.computeIndexes(s1, s3): | ||
return False | ||
return self.consume(s1, s2, "", s3, len(s1)) | ||
|
||
def computeIndexes(self, s1, s3): | ||
self.indexs = [] | ||
s3 = s3[::-1] | ||
s1 = s1[::-1] | ||
l = len(s3) | ||
for c in s1: | ||
idx = -1 if not self.indexs else self.indexs[-1] | ||
idx = s3.find(c, idx+1) | ||
if idx <0: | ||
return False | ||
self.indexs.append(idx) | ||
#print self.indexs | ||
self.indexs = [l-idx for idx in self.indexs] | ||
return True | ||
|
||
|
||
def consume(self, s1, s2, front, end, l): | ||
if not s2.startswith(front): | ||
return False | ||
if not s1: | ||
if s2==front+end: | ||
return True | ||
else: | ||
return False | ||
else: | ||
c = s1[0] | ||
limit = self.indexs[l-1] | ||
idx = -1 | ||
while True: | ||
idx = end.find(c, idx+1) | ||
if idx < 0 or idx>limit: | ||
break | ||
|
||
if self.consume(s1[1:], s2, front+end[:idx], end[idx+1:], l-1): | ||
return True | ||
return False | ||
|
||
|
||
|
||
|
||
print Solution().isInterleave("aa", "ab", "aaba") | ||
print Solution().isInterleave("cbaccccacbcaaaccccaacbccabba", "babbacacbaabbcccabcaca", "baabbacbacccacacbcabcaaabbaccccccccacabcbccabacaba") | ||
print Solution().isInterleave("ab", "bc", "bbac") | ||
print Solution().isInterleave("cacbbbaaabbacbbbbabbcaccccabaaacacbcaacababbaabbaccacbaabac", "cbcccabbbbaaacbaccbabaabbccbbbabacbaacccbbcaabaabbbcbcbab", "ccbcccacbabbbbbbaaaaabbaaccbabbbbacbcbcbaacccbacabbaccbaaabcacbbcabaabacbbcaacaccbbacaabababaabbbaccbbcacbbacabbaacb") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Solution: | ||
# @param {integer[]} nums | ||
# @return {string} | ||
def largestNumber(self, nums): | ||
def ncmp(s1, s2): | ||
if s1+s2>s2+s1: | ||
return 1 | ||
elif s1+s2==s2+s1: | ||
return 0 | ||
else: | ||
return -1 | ||
nums = sorted([str(num) for num in nums], cmp=ncmp) | ||
result = "" | ||
while nums: | ||
num = nums.pop() | ||
result += num | ||
|
||
if result[0] == '0': | ||
result = '0' | ||
return result |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
class Solution: | ||
# @param {integer[]} nums | ||
# @return {string} | ||
def largestNumber(self, nums): | ||
nums = sorted([str(num) for num in nums]) | ||
result = "" | ||
while nums: | ||
num = self.pickone(nums, None) | ||
result += num | ||
nums.remove(num) | ||
if result[0] == '0': | ||
result = '0' | ||
return result | ||
|
||
def pickone(self, nums, memo): | ||
if len(nums) == 1: | ||
return nums[0] | ||
|
||
candidates = [] | ||
c = nums[-1][0] | ||
idx = len(nums)-1 | ||
while nums[idx][0]==c and idx>=0: | ||
candidates.append(nums[idx]) | ||
idx -= 1 | ||
|
||
if len(candidates) == 1: | ||
return candidates[0] | ||
else: | ||
if not memo: | ||
memo = c | ||
candidates = sorted([candidate[1:] for candidate in candidates]) | ||
if '' in candidates: | ||
while '' in candidates: | ||
candidates.remove('') | ||
if not candidates: | ||
return c | ||
max = self.pickone(candidates, memo) | ||
if memo > max: | ||
return c | ||
else: | ||
return c+max | ||
else: | ||
return c + self.pickone(candidates, memo) | ||
|
||
|
||
solution = Solution() | ||
print solution.largestNumber([4993,9779,9200]) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
class LRUCache: | ||
# @param capacity, an integer | ||
def __init__(self, capacity): | ||
self.cache = {} | ||
self.cap = capacity | ||
self.len = 0 | ||
self.lru = [] | ||
|
||
# @return an integer | ||
def get(self, key): | ||
val = self.cache.get(key, -1) | ||
if val > 0: | ||
self.lru.remove(key) | ||
self.lru.append(key) | ||
return val | ||
|
||
# @param key, an integer | ||
# @param value, an integer | ||
# @return nothing | ||
def set(self, key, value): | ||
if key in self.cache: | ||
self.cache[key] = value | ||
self.lru.remove(key) | ||
self.lru.append(key) | ||
else: | ||
if self.len >= self.cap: | ||
dkey = self.lru[0] | ||
del self.cache[dkey] | ||
del self.lru[0] | ||
else: | ||
self.len += 1 | ||
self.lru.append(key) | ||
self.cache[key] = value | ||
|
||
def test(): | ||
cache = LRUCache(2) | ||
print cache.get(1) | ||
cache.set(1, 2) | ||
print cache.cache, cache.len, cache.lru | ||
cache.set(2, 3) | ||
print cache.cache, cache.len, cache.lru | ||
cache.set(1, 4) | ||
print cache.cache, cache.len, cache.lru | ||
cache.set(3, 5) | ||
print cache.cache, cache.len, cache.lru | ||
print cache.get(2) | ||
print cache.get(1) | ||
print cache.cache, cache.len, cache.lru | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import bisect | ||
class Solution: | ||
# @param {integer[]} nums | ||
# @param {integer} target | ||
# @return {integer} | ||
def search(self, nums, target): | ||
if nums[0]<nums[-1]: | ||
min_index = 0 | ||
else: | ||
first = nums[0] | ||
l = 0 | ||
r = len(nums)-1 | ||
while l<r: | ||
j = (l+r)/2 | ||
if nums[j] < first: | ||
r = j | ||
else: | ||
l = j+1 | ||
min_index = r | ||
sorted_nums = nums[min_index:] + nums[:min_index] | ||
print sorted_nums, min_index | ||
pos = bisect.bisect_left(sorted_nums, target) | ||
if pos<len(nums) and sorted_nums[pos]==target: | ||
return (min_index+pos)%len(nums) | ||
else: | ||
return -1 | ||
|
||
print Solution().search([3,0,1], 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
select max(salary) from Employee where salary !=(select max(salary) from Employee); |
Oops, something went wrong.