Skip to content

Commit

Permalink
Add more Leetcode solutions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tzu Hsiang Lin committed Nov 3, 2019
1 parent 5f63989 commit 48ff67c
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ Something I have learned...
## [Algorithm](./algorithm/)
| Title | Solution |
| ----- | -------- |
| Array | <ol><li>[20.py](./algorithm/Array/20.py) Valid Parentheses _(elegant coding)_</li><li>[38.py](./algorithm/Array/38.py) Count and Say _(index and count)_</li><li>[42.py](./algorithm/Array/42.py) Trapping Rain Water _(two pointers)_</li><li>[45.py](./algorithm/Array/45.py) Jump Game II</li><li>[155.py](./algorithm/Array/155.py) Min Stack</li><li>[209.py](./algorithm/Array/209.py) Summary Ranges _(left pointer and accumulate)_</li><li>[217.py](./algorithm/Array/217.py) Contains Duplicate</li><li>[219.py](./algorithm/Array/219.py) Contains Duplicate II</li><li>[228.py](./algorithm/Array/228.py) Summary Ranges _(index and count)_</li><li>[238.py](./algorithm/Array/238.py) Product of Array Except Self _(left2right, right2left)_</li><li>[242.py](./algorithm/Array/242.py) Valid Anagram _(counter equality)_</li><li>[349.py](./algorithm/Array/349.py) Intersection of Two Arrays _(set & set)_</li><li>[350.py](./algorithm/Array/350.py) Intersection of Two Arrays II</li><li>[838.py](./algorithm/Array/838.py) Push Dominoes _(descending)_</li><li>[859.py](./algorithm/Array/859.py) Buddy Strings _(enumerate cases)_</li><li>[1002.py](./algorithm/Array/1002.py) Find Common Characters _(counter & counter)_</li><li>[1169.py](./algorithm/Array/1169.py) Invalid Transactions</li><li>[1170.py](./algorithm/Array/1170.py) Compare Strings by Frequency of the Smallest Character</li><li>[1172.py](./algorithm/Array/1172.py) Dinner Plate Stacks</li><li>[1224.py](./algorithm/Array/1224.py) Maximum Equal Frequency</li><li>[1237.py](./algorithm/Array/1237.py) Find Positive Integer Solution for a Given Equation</li><li>[1239.py](./algorithm/Array/1239.py) Maximum Length of a Concatenated String with Unique Characters _(set operation)_</li><li>[5181.py](./algorithm/Array/5181.py) Distance Between Bus Stops</li><li>[5222.py](./algorithm/Array/5222.py) Split a String in Balanced Strings</li></ol> |
| Array | <ol><li>[20.py](./algorithm/Array/20.py) Valid Parentheses _(elegant coding)_</li><li>[38.py](./algorithm/Array/38.py) Count and Say _(index and count)_</li><li>[42.py](./algorithm/Array/42.py) Trapping Rain Water _(two pointers)_</li><li>[45.py](./algorithm/Array/45.py) Jump Game II</li><li>[155.py](./algorithm/Array/155.py) Min Stack</li><li>[209.py](./algorithm/Array/209.py) Summary Ranges _(left pointer and accumulate)_</li><li>[217.py](./algorithm/Array/217.py) Contains Duplicate</li><li>[219.py](./algorithm/Array/219.py) Contains Duplicate II</li><li>[228.py](./algorithm/Array/228.py) Summary Ranges _(index and count)_</li><li>[238.py](./algorithm/Array/238.py) Product of Array Except Self _(left2right, right2left)_</li><li>[242.py](./algorithm/Array/242.py) Valid Anagram _(counter equality)_</li><li>[349.py](./algorithm/Array/349.py) Intersection of Two Arrays _(set & set)_</li><li>[350.py](./algorithm/Array/350.py) Intersection of Two Arrays II</li><li>[838.py](./algorithm/Array/838.py) Push Dominoes _(descending)_</li><li>[859.py](./algorithm/Array/859.py) Buddy Strings _(enumerate cases)_</li><li>[1002.py](./algorithm/Array/1002.py) Find Common Characters _(counter & counter)_</li><li>[1169.py](./algorithm/Array/1169.py) Invalid Transactions</li><li>[1170.py](./algorithm/Array/1170.py) Compare Strings by Frequency of the Smallest Character</li><li>[1172.py](./algorithm/Array/1172.py) Dinner Plate Stacks</li><li>[1224.py](./algorithm/Array/1224.py) Maximum Equal Frequency</li><li>[1237.py](./algorithm/Array/1237.py) Find Positive Integer Solution for a Given Equation</li><li>[1239.py](./algorithm/Array/1239.py) Maximum Length of a Concatenated String with Unique Characters _(set operation)_</li><li>[1247.py](./algorithm/Array/1247.py) Minimum Swaps to Make Strings Equal _(meaning of swap)_</li><li>[5181.py](./algorithm/Array/5181.py) Distance Between Bus Stops</li><li>[5222.py](./algorithm/Array/5222.py) Split a String in Balanced Strings</li></ol> |
| Sort | <ol><li>[Sorting.py](./algorithm/Sort/Sorting.py)</li><li>[15.py](./algorithm/Sort/15.py) 3Sum _(two pointers)_</li><li>[41.py](./algorithm/Sort/41.py) First Missing Positive</li><li>[49.py](./algorithm/Sort/49.py) Group Anagrams</li><li>[56.py](./algorithm/Sort/56.py) Merge Intervals _(lambda sort key)_</li><li>[75.py](./algorithm/Sort/75.py) Sort Colors</li><li>[220.py](./algorithm/Sort/220.py) Contains Duplicate III</li><li>[253.py](./algorithm/Sort/253.py) Meeting Rooms II</li><li>[295.py](./algorithm/Sort/295.py) Find Median from Data Stream</li><li>[539.py](./algorithm/Sort/539.py) Minimum Time Difference</li><li>[953.py](./algorithm/Sort/953.py) Verifying an Alien Dictionary</li><li>[1051.py](./algorithm/Sort/1051.py) Height Checker</li></ol> |
| Tree | <ol><li>[Traversal.py](./algorithm/Tree/Traversal.py) / [BinarySearchTree.py](./algorithm/Tree/BinarySearchTree.py) / [BinaryTreeProperty.py](./algorithm/Tree/BinaryTreeProperty.py)</li><li>[98.py](./algorithm/Tree/98.py) Validate Binary Search Tree</li><li>[102.py](./algorithm/Tree/102.py) Binary Tree Level Order Traversal</li><li>[104.py](./algorithm/Tree/104.py) Maximum Depth of Binary Tree</li><li>[105.py](./algorithm/Tree/105.py) Construct Binary Tree from Preorder and Inorder Traversal</li><li>[108.py](./algorithm/Tree/108.py) Convert Sorted Array to Binary Search Tree</li><li>[226.py](./algorithm/Tree/226.py) Invert Binary Tree</li><li>[250.py](./algorithm/Tree/250.py) Count Univalue Subtrees</li><li>[333.py](./algorithm/Tree/333.py) Largest BST Subtree</li><li>[863.py](./algorithm/Tree/863.py) All Nodes Distance K in Binary Tree</li></ol> |
| Graph | <ol><li>[Graph.py](./algorithm/Graph/Graph.py)</li><li>[200.py](./algorithm/Graph/200.py) Number of Islands</li><li>[1222.py](./algorithm/Graph/1222.py) Queens That Can Attack the King</li></ol> |
| LinkedList | <ol><li>[19.py](./algorithm/LinkedList/19.py) Remove Nth Node From End of List</li><li>[141.py](./algorithm/LinkedList/141.py) Linked List Cycle _(slow/fast pointers)_</li><li>[206.py](./algorithm/LinkedList/206.py) Reverse Linked List _(prev/curr pointers)_</li><li>[160.py](./algorithm/LinkedList/160.py) Intersection of Two Linked Lists</li><li>[1171.py](./algorithm/LinkedList/1171.py) Remove Zero Sum Consecutive Nodes from Linked List</li></ol> |
| Dynamic Programming | <ol><li>[55.py](./algorithm/DP/55.py) Jump Game</li><li>[62.py](./algorithm/DP/62.py) Unique Paths</li><li>[70.py](./algorithm/DP/70.py) Climbing Stairs</li><li>[72.py](./algorithm/DP/72.py) Edit Distance</li><li>[1223.py](./algorithm/DP/1223.py) Dice Roll Simulation</li></ol> |
| Sub Array<br>Sub String | <ol><li>[53.py](./algorithm/Sub/53.py) Maximum Subarray _(add or new sum)_</li><li>[121.py](./algorithm/Sub/121.py) Best Time to Buy and Sell Stock _(just cares last)_</li><li>[152.py](./algorithm/Sub/152.py) Maximum Product Subarray _(min * negative)_</li><li>[386.py](./algorithm/Sub/386.py) Longest Substring with At Most K Distinct Characters _(just cares last)_</li><li>[1233.py](./algorithm/Sub/1233.py) Remove Sub-Folders from the Filesystem _(rfind, set lookup)_</li></ol> |
| Sub Array<br>Sub String | <ol><li>[53.py](./algorithm/Sub/53.py) Maximum Subarray _(add or new sum)_</li><li>[121.py](./algorithm/Sub/121.py) Best Time to Buy and Sell Stock _(just cares last)_</li><li>[152.py](./algorithm/Sub/152.py) Maximum Product Subarray _(min * negative)_</li><li>[386.py](./algorithm/Sub/386.py) Longest Substring with At Most K Distinct Characters _(just cares last, idx-left+1)_</li><li>[1233.py](./algorithm/Sub/1233.py) Remove Sub-Folders from the Filesystem _(rfind, set lookup)_</li><li>[1248.py](./algorithm/Sub/1248.py) Count Number of Nice Subarrays _(idx-left+1)_</li></ol> |
| Binary Search | <ol><li>[BinarySearchVariants.py](./algorithm/BinarySearch/BinarySearchVariants.py)</li><li>[33.py](./algorithm/BinarySearch/33.py) Search in Rotated Sorted Array _(check sorted part)_</li></ol> |
| Math | <ol><li>[The Aggregate Magic Algorithms](http://aggregate.org/MAGIC/)</li><li>[1.py](./algorithm/Math/1.py) Two Sum</li><li>[7.py](./algorithm/Math/7.py) Reverse Integer</li><li>[172.py](./algorithm/Math/172.py) Factorial Trailing Zeroes</li><li>[224.py](./algorithm/Math/224.py) Basic Calculator</li><li>[1071.py](./algorithm/Math/1071.py) Greatest Common Divisor of Strings</li><li>[1232.py](./algorithm/Math/1232.py) Check If It Is a Straight Line</li><li>[1238.py](./algorithm/Math/1238.py) Circular Permutation in Binary Representation _(gray code)_</li><li>[5183.py](./algorithm/Math/5183.py) Day of the Week</li></ol> |

Expand Down
139 changes: 139 additions & 0 deletions algorithm/Array/1247.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
'''
1247. Minimum Swaps to Make Strings Equal
https://leetcode.com/contest/weekly-contest-161/problems/minimum-swaps-to-make-strings-equal/
You are given two strings s1 and s2 of equal length
consisting of letters "x" and "y" only.
Your task is to make these two strings equal to each other.
You can swap any two characters that belong to different strings,
which means: swap s1[i] and s2[j].
Return the minimum number of swaps required to make s1 and s2 equal,
or return -1 if it is impossible to do so.
Example 1:
Input: s1 = "xx", s2 = "yy"
Output: 1
Explanation:
Swap s1[0] and s2[1], s1 = "yx", s2 = "yx".
Example 2:
Input: s1 = "xy", s2 = "yx"
Output: 2
Explanation:
Swap s1[0] and s2[0], s1 = "yy", s2 = "xx".
Swap s1[0] and s2[1], s1 = "xy", s2 = "xy".
Note that you can't swap s1[0] and s1[1] to make s1 equal to "yx",
cause we can only swap chars in different strings.
Example 3:
Input: s1 = "xx", s2 = "xy"
Output: -1
Example 4:
Input: s1 = "xxyyxyxyxx", s2 = "xyyxyxxxyx"
Output: 4
Constraints:
1 <= s1.length, s2.length <= 1000
s1, s2 only contain 'x' or 'y'.
'''
from typing import *

class Solution:
def minimumSwap(self, s1: str, s2: str) -> int:
xy, yx = 0, 0

for c1, c2 in zip(s1, s2):
if c1=='x' and c2=='y':
xy += 1
elif c1=='y' and c2=='x':
yx += 1

# if number of 'x' or 'y' is odd, we can not make s1 equals to s2
if (xy + yx) % 2 != 0:
return -1

# Cases to do 1 swap:
# "xx" => xy // 2 => how many pairs of 'x' we have ?
# "yy" => yx // 2 => how many pairs of 'y' we have ?
#
# Cases to do 2 swaps:
# "xy" or "yx" => xy % 2
return xy // 2 + yx // 2 + (xy % 2) * 2

import unittest

class MinSwapTest(unittest.TestCase):

def testCase1Swap(self):
self.assertEqual(Solution().minimumSwap(s1 = "xx", s2 = "yy"), 1)
# s1=xx
# s2=yy
# ↓↓ 0↑(yx) 2↓(xy)
#1
self.assertEqual(Solution().minimumSwap(s1="yy", s2="xx"), 1)
# s1=yy
# s2=xx
# ↑↑ 2↑(yx) 0↓(xy)
#1
self.assertEqual(Solution().minimumSwap(s1="xxxx", s2="yyyy"), 2)
# s1=xxxx
# s2=yyyy
# ↓↓↓↓ 0↑(yx) 4↓(xy)
# 2

def testCase2Swap(self):
self.assertEqual(Solution().minimumSwap(s1 = "xy", s2 = "yx"), 2)
# s1=xy
# s2=yx
# ↓↑ 1↑(yx) 1↓(xy)
#2

def testMixedCase(self):
self.assertEqual(Solution().minimumSwap(s1 = "xyxx", s2 = "yxyy"), 3)
# s1=xyxx
# s2=yxyy
# ↓↑↓↓ 1↑(yx) 3↓(xy)
#3
self.assertEqual(Solution().minimumSwap(s1 = "xxyxy", s2 = "yyyyx"), 3)
# s1=xxyxy
# s2=yyyyx
# ↓↓ ↓↑ 1↑(yx) 3↓(xy)
# 3
self.assertEqual(Solution().minimumSwap(s1="xxyyxyxyxx", s2="xyyxyxxxyx"), 4)
# s1=xxyyxyxyxx
# s2=xyyxyxxxyx
# ↓ ↑↓↑ ↑↓ 3↑(yx) 3↓(xy)
# 4

def testInvalidCase(self):
self.assertEqual(Solution().minimumSwap(s1="xx", s2="xy"), -1)
# s1=xx
# s2=xy
# ↓ 0↑(yx) 1↓(xy)
#-1
self.assertEqual(Solution().minimumSwap(s1 = "xyxx", s2 = "yyyy"), -1)
# s1=xyxx
# s2=yyyy
# ↓ ↓↓ 0↑(yx) 3↓(xy)
#-1

if __name__ == "__main__":
unittest.main()

#2,2,2,1,2,2,1
#2,2,2,1,2,2,1,2
#2,2,2,1,2,2,1,2,2
#2,2,2,1,2,2,1,2,2,2
# 2,2,1,2,2,1,2,2,2
# 2,1,2,2,1,2,2,2
# 1,2,2,1,2,2,2
# 1,2,2,1,2,2
# 1,2,2,1,2
# 1,2,2,1
# 2,1,2,2,1
# 2,1,2,2,1,2
# 2,1,2,2,1,2,2
# 2,2,1,2,2,1
# 2,2,1,2,2,1,2
# 2,2,1,2,2,1,2,2
64 changes: 64 additions & 0 deletions algorithm/Sub/1248.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'''
1248. Count Number of Nice Subarrays
https://leetcode.com/problems/count-number-of-nice-subarrays/
Given an array of integers nums and an integer k.
A subarray is called nice if there are k odd numbers on it.
Return the number of nice sub-arrays.
Example 1:
Input: nums = [1,1,2,1,1], k = 3
Output: 2
Explanation:
The only sub-arrays with 3 odd numbers are [1,1,2,1] and [1,2,1,1].
Example 2:
Input: nums = [2,4,6], k = 1
Output: 0
Explanation: There is no odd numbers in the array.
Example 3:
Input: nums = [2,2,2,1,2,2,1,2,2,2], k = 2
Output: 16
Constraints:
1 <= nums.length <= 50000
1 <= nums[i] <= 10^5
1 <= k <= nums.length
'''
from typing import *

class Solution:
def numberOfSubarrays(self, nums: List[int], k: int) -> int:
def atMost(numOdd):
numSubArr = left = 0
for idx in range(len(nums)):
numOdd -= nums[idx] % 2
while numOdd < 0:
numOdd += nums[left] % 2
left += 1
numSubArr += idx - left + 1
return numSubArr

return atMost(k) - atMost(k - 1)

#print(Solution().numberOfSubarrays(nums = [1,1,2,1,1], k = 3))
#2
print(Solution().numberOfSubarrays(nums = [2,2,2,1,2,2,1,2,2,2], k = 2))
#16
#2,2,2,1,2,2,1
#2,2,2,1,2,2,1,2
#2,2,2,1,2,2,1,2,2
#2,2,2,1,2,2,1,2,2,2
# 2,2,1,2,2,1,2,2,2
# 2,1,2,2,1,2,2,2
# 1,2,2,1,2,2,2
# 1,2,2,1,2,2
# 1,2,2,1,2
# 1,2,2,1
# 2,1,2,2,1
# 2,1,2,2,1,2
# 2,1,2,2,1,2,2
# 2,2,1,2,2,1
# 2,2,1,2,2,1,2
# 2,2,1,2,2,1,2,2

0 comments on commit 48ff67c

Please sign in to comment.