From e00c0f6094f1a835cdbc48c4a0587f373830d76d Mon Sep 17 00:00:00 2001 From: Ektedar Alam Date: Mon, 31 Jan 2022 03:05:11 -0500 Subject: [PATCH 1/5] Adding Swim In Rising Water Problem LeetCode #776 (#269) --- README.md | 1 + .../SwimInRisingWater/swim_in_rising_water.py | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 algorithms/python/SwimInRisingWater/swim_in_rising_water.py diff --git a/README.md b/README.md index 0ab766d0f..4cbdb6ebd 100644 --- a/README.md +++ b/README.md @@ -196,6 +196,7 @@ LeetCode |830|[Positions of Large Groups](https://leetcode.com/problems/positions-of-large-groups/) | [Python](./algorithms/python/PositionsOfLargeGroups/largeGroupPositions.py)|Easy| |820|[Short Encoding of Words](https://leetcode.com/problems/short-encoding-of-words/) | [C++](./algorithms/cpp/shortEncodingOfWords/ShortEncodingOfWords.cpp)|Medium| |804|[Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/description/) | [C++](./algorithms/cpp/uniqueMorseCodeWords/UniqueMorseCodeWords.cpp)|Easy| +|776|[Swim In Rising Water](https://leetcode.com/problems/swim-in-rising-water/description/) | [Python](./algorithms/python/SwimInRisingWater/swim_in_rising_water.py)|Hard| |771|[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/description) | [C++](./algorithms/cpp/jewelsAndStones/JewelsAndStones.cpp)|Easy| |747|[Largest Number At Least Twice of Others](https://leetcode.com/problems/largest-number-at-least-twice-of-others/) | [Python](./algorithms/python/LargestNumberAtLeastTwiceOfOthers/dominantIndex.py)|Easy| |746|[Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/) | [C++](./algorithms/cpp/minCostClimbingStairs/MinCostClimbingStairs.cpp), [Python](./algorithms/python/MinCostClimbingStairs/minCostClimbingStairs.py)|Easy| diff --git a/algorithms/python/SwimInRisingWater/swim_in_rising_water.py b/algorithms/python/SwimInRisingWater/swim_in_rising_water.py new file mode 100644 index 000000000..0b689ab6b --- /dev/null +++ b/algorithms/python/SwimInRisingWater/swim_in_rising_water.py @@ -0,0 +1,41 @@ +""" +Solution to LeetCode problem Swim in Rising Water (#778) +Difficulty: Hard + +Explanation: +We are approaching the problem through Djikstras Algorith. Essentially a BFS but with a minimum heap. The neighbors that you are adding into the queue, you are only popping the minimum value neighbor. Amongst those popped value you are checking if that value is the max value seen so far. If it is, then you have found the solution. +""" +from heapq import heappush, heappop +from typing import List + +class Solution: + def swimInWater(self, grid: List[List[int]]) -> int: + src, dst = grid[0][0], grid[len(grid)-1][len(grid)-1] + visited = set() + heap_queue = [(src, 0, 0)] # src, row, col + output = 0 + directions = [(0, 1), (1, 0), (-1, 0), (0, -1)] + while heap_queue: + current, row, col = heappop(heap_queue) + output = max(current, output) + + # if we already hit the destination, break out of the loop + if current == dst: + break + + for x, y in directions: + dx, dy = row+x, col+y + if self.check_bounds(dx, dy, grid) and (dx, dy) not in visited: + heappush(heap_queue, (grid[dx][dy], dx, dy)) + visited.add((dx, dy)) + return output + + def check_bounds(self, r, c, grid) -> bool: + if 0 <= r < len(grid[0]) and 0 <= c < len(grid): + return True + return False + + +if __name__ == "__main__": + grid = [[0, 2], [1, 3]] + print(Solution().swimInWater(grid)) \ No newline at end of file From 637015090a62063ce21e3b42f4aeb662f0edcacc Mon Sep 17 00:00:00 2001 From: Will <73602111+willmccarten@users.noreply.github.com> Date: Mon, 31 Jan 2022 03:06:01 -0500 Subject: [PATCH 2/5] New java solution to problem 1946 and updated readme (#268) * Create Java solution for problem 1946 ran fine on leetcode, but was having some potential issues with unwanted non printable ascii characters * update readme for java solution on 1946 --- README.md | 2 +- .../largestNumberAfterMutatingSubstring.java | 81 +++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 algorithms/java/src/LargestNumberAfterMutatingSubstring/largestNumberAfterMutatingSubstring.java diff --git a/README.md b/README.md index 4cbdb6ebd..d3d78a84d 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ LeetCode | # | Title | Solution | Difficulty | |---| ----- | -------- | ---------- | -|1946|[Largest Number After Mutating Substring](https://leetcode.com/problems/largest-number-after-mutating-substring/) | [C++](./algorithms/cpp/largestNumberAfterMutatingSubstring/LargestNumberAfterMutatingSubstring.cpp)|Medium| +|1946|[Largest Number After Mutating Substring](https://leetcode.com/problems/largest-number-after-mutating-substring/) | [C++](./algorithms/cpp/largestNumberAfterMutatingSubstring/LargestNumberAfterMutatingSubstring.cpp), [Java](./algorithms/java/src/LargestNumberAfterMutatingSubtring/largestNumberAfterMutatingSubstring.java)|Medium| |1945|[Sum of Digits of String After Convert](https://leetcode.com/problems/sum-of-digits-of-string-after-convert/) | [C++](./algorithms/cpp/leetcode/sumOfDigitsOfStringAfterConvert/SumOfDigitsOfStringAfterConvert.cpp)|Easy| |1935|[Maximum Number of Words You Can Type](https://leetcode.com/problems/maximum-number-of-words-you-can-type/) | [C++](./algorithms/cpp/maximumNumberOfWordsYouCanType/MaximumNumberOfWordsYouCanType.cpp)|Easy| |1884|[Egg Drop With 2 Eggs and N Floors](https://leetcode.com/problems/egg-drop-with-2-eggs-and-n-floors/) | [C++](./algorithms/cpp/eggDropWith2EggsAndNFloors/EggDropWith2EggsAndNFloors.cpp)|Medium| diff --git a/algorithms/java/src/LargestNumberAfterMutatingSubstring/largestNumberAfterMutatingSubstring.java b/algorithms/java/src/LargestNumberAfterMutatingSubstring/largestNumberAfterMutatingSubstring.java new file mode 100644 index 000000000..ae4d53dff --- /dev/null +++ b/algorithms/java/src/LargestNumberAfterMutatingSubstring/largestNumberAfterMutatingSubstring.java @@ -0,0 +1,81 @@ +/***************************************************************************************************** + * + * You are given a string num, which represents a large integer. You are also given a 0-indexed + * integer array change of length 10 that maps each digit 0-9 to another digit. More formally, digit d + * maps to digit change[d]. + * + * You may choose to mutate a single substring of num. To mutate a substring, replace each digit + * num[i] with the digit it maps to in change (i.e. replace num[i] with change[num[i]]). + * + * Return a string representing the largest possible integer after mutating (or choosing not to) a + * single substring of num. + * + * A substring is a contiguous sequence of characters within the string. + * + * Example 1: + * + * Input: num = "132", change = [9,8,5,0,3,6,4,2,6,8] + * Output: "832" + * Explanation: Replace the substring "1": + * - 1 maps to change[1] = 8. + * Thus, "132" becomes "832". + * "832" is the largest number that can be created, so return it. + * + * Example 2: + * + * Input: num = "021", change = [9,4,3,5,7,2,1,9,0,6] + * Output: "934" + * Explanation: Replace the substring "021": + * - 0 maps to change[0] = 9. + * - 2 maps to change[2] = 3. + * - 1 maps to change[1] = 4. + * Thus, "021" becomes "934". + * "934" is the largest number that can be created, so return it. + * + * Example 3: + * + * Input: num = "5", change = [1,4,7,5,3,2,5,6,9,4] + * Output: "5" + * Explanation: "5" is already the largest number that can be created, so return it. + * + * Constraints: + * + * 1 <= num.length <= 10^5 + * num consists of only digits 0-9. + * change.length == 10 + * 0 <= change[d] <= 9 + ******************************************************************************************************/ + +class Solution { + public String maximumNumber(String num, int[] change) { + + //make flag for checking replacement + boolean shouldReplace = false; + + //work through substring + for(int i = 0; i < num.length(); i++){ + + //subtract zero to get correct ascii conversion + char a = num.charAt(i); + char curr = (char)(a - '0'); + + if(curr < change[curr]){ + + char c = (char)(change[curr] + '0'); + + num = num.replace(num.charAt(i), c); + + shouldReplace = true; + + }else if((curr > change[curr]) && (shouldReplace)){ + + break; + + } + } + + return num; + } +} + + From d2ce5c665d3ddb9b48ae4726e1a83a3f65eb5662 Mon Sep 17 00:00:00 2001 From: Will <73602111+willmccarten@users.noreply.github.com> Date: Mon, 31 Jan 2022 03:07:11 -0500 Subject: [PATCH 3/5] Added a java solution to problem 48 and updated the readme for it (#267) * Creat RotateImage.java for problem #48 solution and explanation to problem #48 * Create solution to problem 48, Rotating Image I needed to remake the commit because i was unaware the hashtag symbol would create a link to the issue number lol sorry * update Readme for java solution on problem 48 adding a java solution to the readme for problem 48 * Update Readme...I added an extra space added an extra space after [Java] and that made it look wrong and I forgot to preview it. Fixed now --- README.md | 2 +- .../java/src/RotateImage/rotateImage.java | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 algorithms/java/src/RotateImage/rotateImage.java diff --git a/README.md b/README.md index d3d78a84d..9976304ca 100644 --- a/README.md +++ b/README.md @@ -514,7 +514,7 @@ LeetCode |51|[N-Queens](https://leetcode.com/problems/n-queens/)| [C++](./algorithms/cpp/nQueens/nQueuens.cpp)|Hard| |50|["Pow(x, n)"](https://leetcode.com/problems/powx-n/)| [C++](./algorithms/cpp/pow/pow.cpp), [Java](./algorithms/java/src/powXn/PowXn.java)|Medium| |49|[Group Anagrams](https://leetcode.com/problems/anagrams/)| [C++](./algorithms/cpp/anagrams/GroupAnagrams.cpp)|Medium| -|48|[Rotate Image](https://leetcode.com/problems/rotate-image/)| [C++](./algorithms/cpp/rotateImage/rotateImage.cpp)|Medium| +|48|[Rotate Image](https://leetcode.com/problems/rotate-image/)| [C++](./algorithms/cpp/rotateImage/rotateImage.cpp), [Java](./algorithms/java/src/RotateImage/rotateImage.java)|Medium| |47|[Permutations II](https://leetcode.com/problems/permutations-ii/)| [C++](./algorithms/cpp/permutations/permutations.II.cpp)|Hard| |46|[Permutations](https://leetcode.com/problems/permutations/)| [C++](./algorithms/cpp/permutations/permutations.cpp)|Medium| |45|[Jump Game II](https://leetcode.com/problems/jump-game-ii/)| [C++](./algorithms/cpp/jumpGame/jumpGame.II.cpp)|Hard| diff --git a/algorithms/java/src/RotateImage/rotateImage.java b/algorithms/java/src/RotateImage/rotateImage.java new file mode 100644 index 000000000..2bba2be71 --- /dev/null +++ b/algorithms/java/src/RotateImage/rotateImage.java @@ -0,0 +1,53 @@ +/* + +You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise). + +You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation. + + Example 1: +Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] +Output: [[7,4,1],[8,5,2],[9,6,3]] + +Example 2: +Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]] +Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]] + +Constraints: + +n == matrix.length == matrix[i].length +1 <= n <= 20 +-1000 <= matrix[i][j] <= 1000 + +*/ + + +//when looking for a pattern, we notice that there numbers in groups of four that change positions with one another +//approach: find the positions of those four and swamp them with their right neighbor to make the 90 degree turn + +class Solution { + public void rotate(int[][] matrix) { + + int n = matrix.length; //given + + //adding one and dividing with int division accounts for both odd and even length of n + for (int i = 0; i < ((n + 1) / 2); i ++) { + + for (int j = 0; j < (n / 2); j++) { + + //Starts with far edges and works its way back to the left + //hold first of group, use to replace in final swap + int temp = matrix[n - j - 1][i]; + + //keep replacing with group member on next side of the square + matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1]; + matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1]; + matrix[j][n - i - 1] = matrix[i][j]; + + //finally replace last spot with first spot held in temp + matrix[i][j] = temp; + + //move to next group with index incrementation + } + } + } +} From 505198061b0c3fca66ef4b47005f233422f17df7 Mon Sep 17 00:00:00 2001 From: Jordan Kirkland <73541159+jkirkland722@users.noreply.github.com> Date: Mon, 31 Jan 2022 03:09:02 -0500 Subject: [PATCH 4/5] Added python solutions and corrected 128 to medium (#263) --- README.md | 4 ++-- .../LongestConsecutive.py | 18 ++++++++++++++++++ .../python/SingleNumberII/SingleNumberII.py | 15 +++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 algorithms/python/LongestConsecutiveSequence/LongestConsecutive.py create mode 100644 algorithms/python/SingleNumberII/SingleNumberII.py diff --git a/README.md b/README.md index 9976304ca..a146bd02a 100644 --- a/README.md +++ b/README.md @@ -425,7 +425,7 @@ LeetCode |140|[Word Break II](https://leetcode.com/problems/word-break-ii/)| [C++](./algorithms/cpp/wordBreak/wordBreak.II.cpp)|Hard| |139|[Word Break](https://leetcode.com/problems/word-break/)| [C++](./algorithms/cpp/wordBreak/wordBreak.cpp)|Medium| |138|[Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/)| [C++](./algorithms/cpp/copyListWithRandomPointer/copyListWithRandomPointer.cpp), [Python](./algorithms/python/CopyListWithRandomPointer/copyRandomList.py)|Hard| -|137|[Single Number II](https://leetcode.com/problems/single-number-ii/)| [C++](./algorithms/cpp/singleNumber/singleNumber.II.cpp)|Medium| +|137|[Single Number II](https://leetcode.com/problems/single-number-ii/)| [C++](./algorithms/cpp/singleNumber/singleNumber.II.cpp), [Python](./algorithms/python/SingleNumberII/SingleNumberII.py)|Medium| |136|[Single Number](https://leetcode.com/problems/single-number/)| [C++](./algorithms/cpp/singleNumber/singleNumber.cpp)|Medium| |135|[Candy](https://leetcode.com/problems/candy/)| [C++](./algorithms/cpp/candy/candy.cpp)|Hard| |134|[Gas Station](https://leetcode.com/problems/gas-station/)| [C++](./algorithms/cpp/gasStation/gasStation.cpp)|Medium| @@ -434,7 +434,7 @@ LeetCode |131|[Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/)| [C++](./algorithms/cpp/palindromePartitioning/palindromePartitioning.cpp)|Medium| |130|[Surrounded Regions](https://leetcode.com/problems/surrounded-regions/)| [C++](./algorithms/cpp/surroundedRegions/surroundedRegions.cpp)|Medium| |129|[Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/)| [C++](./algorithms/cpp/sumRootToLeafNumber/sumRootToLeafNumber.cpp), [Python](./algorithms/python/SumRootToLeafNumbers/sumNumbers.py)|Medium| -|128|[Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)| [C++](./algorithms/cpp/longestConsecutiveSequence/longestConsecutiveSequence.cpp)|Hard| +|128|[Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)| [C++](./algorithms/cpp/longestConsecutiveSequence/longestConsecutiveSequence.cpp), [Python](./algorithms/python/LongestConsecutiveSequence/LongestConsecutive.py)|Medium| |127|[Word Ladder](https://leetcode.com/problems/word-ladder/)| [C++](./algorithms/cpp/wordLadder/wordLadder.cpp)|Medium| |126|[Word Ladder II](https://leetcode.com/problems/word-ladder-ii/)| [C++](./algorithms/cpp/wordLadder/wordLadder.II.cpp)|Hard| |125|[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)| [C++](./algorithms/cpp/validPalindrome/validPalindrome.cpp), [Java](./algorithms/java/src/validPalindrome/ValidPalindrome.java)|Easy| diff --git a/algorithms/python/LongestConsecutiveSequence/LongestConsecutive.py b/algorithms/python/LongestConsecutiveSequence/LongestConsecutive.py new file mode 100644 index 000000000..2da021df2 --- /dev/null +++ b/algorithms/python/LongestConsecutiveSequence/LongestConsecutive.py @@ -0,0 +1,18 @@ +def longestConsecutive(self, nums): + + #first, create a hashmap + hm = set(nums) + longest = 0 + #find the longest consecutive sequence from each number in the new + #hashmap (set) + for num in hm: + if (num - 1) not in hm: + current = num + 1 + cons = 1 + while current in hm: + current += 1 + cons += 1 + if cons > longest: + longest = cons + + return longest \ No newline at end of file diff --git a/algorithms/python/SingleNumberII/SingleNumberII.py b/algorithms/python/SingleNumberII/SingleNumberII.py new file mode 100644 index 000000000..85a5fa39f --- /dev/null +++ b/algorithms/python/SingleNumberII/SingleNumberII.py @@ -0,0 +1,15 @@ +def singleNumber(self, nums): + + #1. make hashmap + #2. while traversing list, add each element + #3. remove it once it has been seen 3 times + #4. finally, the only value left is the one we're looking for + + hashmap = defaultdict(int) + for num in nums: + hashmap[num] += 1 + if hashmap[num] == 3: + del hashmap[num] + + singleNum = hashmap.keys()[0] + return singleNum From 125c4d8d7e3a4700d7b2dc5b6e7137f84da884e3 Mon Sep 17 00:00:00 2001 From: Emdadul Haque Date: Sat, 9 Apr 2022 11:30:24 +0600 Subject: [PATCH 5/5] Update README.md (#271) --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index a146bd02a..2bf93af3b 100644 --- a/README.md +++ b/README.md @@ -563,7 +563,6 @@ LeetCode |2|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/)| [C++](./algorithms/cpp/addTwoNumbers/addTwoNumbers.cpp)|Medium| |1|[Two Sum](https://leetcode.com/problems/two-sum/)| [C++](./algorithms/cpp/twoSum/twoSum.cpp), [Go](./algorithms/golang/twoSum/twoSum.go)|Easy| -l ### LeetCode Shell