This file contains hidden or 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
| // O(n) | |
| public class Solution { | |
| public void Merge(int[] nums1, int m, int[] nums2, int n) { | |
| int i = nums1.Length - 1; | |
| int p1 = m-1; | |
| int p2 = n-1; | |
| while (p2 >= 0) | |
| { | |
| if (p1 >= 0 && nums1[p1] > nums2[p2]) |
This file contains hidden or 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
| public class Solution { | |
| /* O(nlogn) + O(n) + O(n) = O(nlogn + 2n) => O(nlogn) */ | |
| public int[] SmallerNumbersThanCurrent(int[] nums) { | |
| List<int> result = new List<int>(); | |
| Dictionary<int, int> hash = new Dictionary<int, int>(); | |
| List<int> sorted = nums.ToList(); | |
| sorted.Sort(); | |
| for (int i=0; i<sorted.Count; i++) | |
| { |
This file contains hidden or 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
| public class Solution { | |
| public string MakeFancyString(string s) { | |
| string result = s[0].ToString(); | |
| int count = 1; | |
| char lastChar = s[0]; | |
| for (int i=1; i<s.Length; i++) | |
| { | |
| if (s[i] == lastChar) | |
| { |
This file contains hidden or 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
| # What is the output of this program? | |
| a = 6 | |
| b = 100 | |
| c = 12 | |
| d = 20 | |
| print(a + 4) | |
| print(b - 25) |
This file contains hidden or 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
| function gcdOfStrings(str1: string, str2: string): string { | |
| // Start with the entire substring and look for matches, decreasing by 1 character each time. | |
| for (let len=str2.length; len>0; len--) { | |
| let part = str2.substr(0, len); | |
| //console.log(`Trying ${part}`); | |
| // Check if the string is evenly divisible by the part. | |
| if (str1.length % part.length === 0) { | |
| // Check if both str1 and str2 are evenly divisible by the part. |
This file contains hidden or 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
| var pivotIndex = function(nums) { | |
| let leftSum = 0; | |
| let rightSum = nums.reduce((sum, value) => sum + value, 0); | |
| for (let i=0; i<nums.length; i++) { | |
| // Subtract this value from the rightSum, as we move towards the right. | |
| rightSum -= nums[i]; | |
| if (rightSum === leftSum) { | |
| // Found the middle point. |
This file contains hidden or 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
| var commonChars = function(words) { | |
| hash1 = {}; | |
| for (let i=0; i<words.length; i++) { | |
| const word = words[i]; | |
| const hash2 = {}; // stores the counts | |
| for (let ch of word) { | |
| // Increment the key for this letter within this word, used for multiple occurrences of the same letter in the word. | |
| hash2[ch] = hash2[ch] ? hash2[ch] + 1 : 1; |
This file contains hidden or 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 MyQueue: | |
| def __init__(self): | |
| # Initialize two stacks. | |
| self.stack1 = [] | |
| self.stack2 = [] | |
| def _flip(self, stack1, stack2, is_pop = False) -> int: | |
| # Flips stack1 into stack2, with the top value from stack1 becoming the bottom value in stack2. | |
| while (len(stack1) > 0): | |
| stack2.append(stack1.pop()) |
This file contains hidden or 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
| // | |
| // Solution using string splitting with fall-through switch statement. | |
| // | |
| public class Solution { | |
| public int DayOfYear(string date) | |
| { | |
| String[] split= date.Split("-"); | |
| int year= Convert.ToInt32(split[0]); | |
| int month= Convert.ToInt32(split[1]); |
This file contains hidden or 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
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <meta name="viewport" content="width=device-width"> | |
| <title>JS Bin</title> | |
| <script src="https://code.jquery.com/jquery.min.js"></script> | |
| <script crossorigin src="https://unpkg.com/react@17/umd/react.production.min.js"></script> | |
| <script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script> | |
| <script src='https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser-polyfill.min.js'></script> |