mirror of
https://github.com/prdlk/leetcode.git
synced 2026-09-16 23:16:26 +00:00
refactor(work): restructure solution directories with bucket level and add .gitkeep placeholders
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
"""
|
||||
1. Two Sum
|
||||
Difficulty: Easy
|
||||
https://leetcode.com/problems/two-sum/
|
||||
|
||||
──────────────────────────────────────────────────
|
||||
|
||||
You are given an array of integers nums and an integer target, return
|
||||
indices of the two numbers such that they add up to target.
|
||||
|
||||
You may assume that each input would have exactly one solution, and
|
||||
you may not use the same element twice.
|
||||
|
||||
You can return the answer in any order.
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
Input: nums = [2,7,11,15], target = 9
|
||||
Output: [0,1]
|
||||
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
|
||||
|
||||
Example 2:
|
||||
|
||||
Input: nums = [3,2,4], target = 6
|
||||
Output: [1,2]
|
||||
|
||||
Example 3:
|
||||
|
||||
Input: nums = [3,3], target = 6
|
||||
Output: [0,1]
|
||||
|
||||
|
||||
|
||||
Constraints:
|
||||
|
||||
• 2 <= nums.length <= 10^4
|
||||
|
||||
• -10^9 <= nums[i] <= 10^9
|
||||
|
||||
• -10^9 <= target <= 10^9
|
||||
|
||||
• Only one valid answer exists.
|
||||
|
||||
|
||||
|
||||
Follow-up: Can you come up with an algorithm that is less than O(n^2)
|
||||
time complexity?
|
||||
"""
|
||||
|
||||
class Solution:
|
||||
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
||||
seen = {}
|
||||
for i, num in enumerate(nums):
|
||||
complement = target - num
|
||||
if complement in seen:
|
||||
return [seen[complement], i]
|
||||
seen[num] = i
|
||||
return []
|
||||
@@ -0,0 +1,52 @@
|
||||
"""
|
||||
121. Best Time to Buy and Sell Stock
|
||||
Difficulty: Easy
|
||||
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
|
||||
|
||||
──────────────────────────────────────────────────
|
||||
|
||||
You are given an array prices where prices[i] is the price of a given
|
||||
stock on the i^th day.
|
||||
|
||||
You want to maximize your profit by choosing a single day to buy one
|
||||
stock and choosing a different day in the future to sell that stock.
|
||||
|
||||
Return the maximum profit you can achieve from this transaction. If
|
||||
you cannot achieve any profit, return 0.
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
Input: prices = [7,1,5,3,6,4]
|
||||
Output: 5
|
||||
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6),
|
||||
profit = 6-1 = 5.
|
||||
Note that buying on day 2 and selling on day 1 is not allowed because
|
||||
you must buy before you sell.
|
||||
|
||||
Example 2:
|
||||
|
||||
Input: prices = [7,6,4,3,1]
|
||||
Output: 0
|
||||
Explanation: In this case, no transactions are done and the max
|
||||
profit = 0.
|
||||
|
||||
|
||||
|
||||
Constraints:
|
||||
|
||||
• 1 <= prices.length <= 10^5
|
||||
|
||||
• 0 <= prices[i] <= 10^4
|
||||
"""
|
||||
|
||||
class Solution:
|
||||
def maxProfit(self, prices: List[int]) -> int:
|
||||
left = min(prices)
|
||||
for right in range(len(prices)):
|
||||
while curr > left:
|
||||
curr -= prices[left]
|
||||
left += 1
|
||||
ans = max(ans, curr)
|
||||
return ans
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* 1365. How Many Numbers Are Smaller Than the Current Number
|
||||
* Difficulty: Easy
|
||||
* https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/
|
||||
*
|
||||
* ──────────────────────────────────────────────────
|
||||
*
|
||||
* Given the array nums, for each nums[i] find out how many numbers in
|
||||
* the array are smaller than it. That is, for each nums[i] you have to
|
||||
* count the number of valid j's such that j != i and nums[j] < nums[i].
|
||||
*
|
||||
* Return the answer in an array.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Example 1:
|
||||
*
|
||||
* Input: nums = [8,1,2,2,3]
|
||||
* Output: [4,0,1,1,3]
|
||||
* Explanation:
|
||||
* For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and
|
||||
* 3).
|
||||
* For nums[1]=1 does not exist any smaller number than it.
|
||||
* For nums[2]=2 there exist one smaller number than it (1).
|
||||
* For nums[3]=2 there exist one smaller number than it (1).
|
||||
* For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).
|
||||
*
|
||||
* Example 2:
|
||||
*
|
||||
* Input: nums = [6,5,4,8]
|
||||
* Output: [2,1,0,3]
|
||||
*
|
||||
* Example 3:
|
||||
*
|
||||
* Input: nums = [7,7,7,7]
|
||||
* Output: [0,0,0,0]
|
||||
*
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* • 2 <= nums.length <= 500
|
||||
*
|
||||
* • 0 <= nums[i] <= 100
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {number[]} nums
|
||||
* @return {number[]}
|
||||
*/
|
||||
|
||||
var smallerNumbersThanCurrent = function (nums) {
|
||||
// Step 1: Begin by initializing a [Frequency Map]()
|
||||
const freq = {};
|
||||
for (let n of nums) freq[n] = (freq[n] || 0) + 1;
|
||||
|
||||
// Step 2: Sort the numbers by ascending order
|
||||
const sorted = Object.keys(freq).sort((a, b) => a - b);
|
||||
|
||||
// Step 3: Init a count of numbers smaller than the active number
|
||||
let count = 0;
|
||||
|
||||
// Step 4: Init a map to track number of values smaller for each number
|
||||
const smaller = {};
|
||||
|
||||
// Step 5: Iterate over the sorted list
|
||||
for (let num of sorted) {
|
||||
// Set count for active number
|
||||
smaller[num] = count;
|
||||
|
||||
// Update the count by frequency
|
||||
count += freq[num];
|
||||
}
|
||||
|
||||
// Step 6: Use original list and find number of smaller values than it
|
||||
return nums.map((n) => smaller[n]);
|
||||
};
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 1413. Minimum Value to Get Positive Step by Step Sum
|
||||
* Difficulty: Easy
|
||||
* https://leetcode.com/problems/minimum-value-to-get-positive-step-by-step-sum/
|
||||
*
|
||||
* ──────────────────────────────────────────────────
|
||||
*
|
||||
* Given an array of integers nums, you start with an initial positive
|
||||
* value startValue.
|
||||
*
|
||||
* In each iteration, you calculate the step by step sum of startValue
|
||||
* plus elements in nums (from left to right).
|
||||
*
|
||||
* Return the minimum positive value of startValue such that the step by
|
||||
* step sum is never less than 1.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Example 1:
|
||||
*
|
||||
* Input: nums = [-3,2,-3,4,2]
|
||||
* Output: 5
|
||||
* Explanation: If you choose startValue = 4, in the third iteration
|
||||
* your step by step sum is less than 1.
|
||||
* step by step sum
|
||||
* startValue = 4 | startValue = 5 | nums
|
||||
* (4 -3 ) = 1 | (5 -3 ) = 2 | -3
|
||||
* (1 +2 ) = 3 | (2 +2 ) = 4 | 2
|
||||
* (3 -3 ) = 0 | (4 -3 ) = 1 | -3
|
||||
* (0 +4 ) = 4 | (1 +4 ) = 5 | 4
|
||||
* (4 +2 ) = 6 | (5 +2 ) = 7 | 2
|
||||
*
|
||||
* Example 2:
|
||||
*
|
||||
* Input: nums = [1,2]
|
||||
* Output: 1
|
||||
* Explanation: Minimum start value should be positive.
|
||||
*
|
||||
* Example 3:
|
||||
*
|
||||
* Input: nums = [1,-2,-3]
|
||||
* Output: 5
|
||||
*
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* • 1 <= nums.length <= 100
|
||||
*
|
||||
* • -100 <= nums[i] <= 100
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {number[]} nums
|
||||
* @return {number}
|
||||
*/
|
||||
var minStartValue = function(nums) {
|
||||
let prefix = [nums[0]];
|
||||
// Make prefix sum start at 1 after initializing seed value
|
||||
for (let i = 1; i < nums.length; i++){
|
||||
prefix.push(prefix[i - 1] + nums[i]);
|
||||
}
|
||||
|
||||
let min = Math.min(...prefix);
|
||||
return Math.max(1, 1 - min);
|
||||
};
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 1426. Counting Elements
|
||||
* Difficulty: Easy
|
||||
* https://leetcode.com/problems/counting-elements/
|
||||
*
|
||||
* ──────────────────────────────────────────────────
|
||||
*
|
||||
* Given an integer array arr, count how many elements x there are, such
|
||||
* that x + 1 is also in arr. If there are duplicates in arr, count them
|
||||
* separately.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Example 1:
|
||||
*
|
||||
* Input: arr = [1,2,3]
|
||||
* Output: 2
|
||||
* Explanation: 1 and 2 are counted cause 2 and 3 are in arr.
|
||||
*
|
||||
* Example 2:
|
||||
*
|
||||
* Input: arr = [1,1,3,3,5,5,7,7]
|
||||
* Output: 0
|
||||
* Explanation: No numbers are counted, cause there is no 2, 4, 6, or 8
|
||||
* in arr.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* • 1 <= arr.length <= 1000
|
||||
*
|
||||
* • 0 <= arr[i] <= 1000
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {number[]} arr
|
||||
* @return {number}
|
||||
*/
|
||||
var countElements = function(arr) {
|
||||
let arrSet = new Set(arr);
|
||||
let count = 0;
|
||||
|
||||
for (let n of arr) {
|
||||
let sum = n + 1;
|
||||
if (arrSet.has(sum)) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
};
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 1480. Running Sum of 1d Array
|
||||
* Difficulty: Easy
|
||||
* https://leetcode.com/problems/running-sum-of-1d-array/
|
||||
*
|
||||
* ──────────────────────────────────────────────────
|
||||
*
|
||||
* Given an array nums. We define a running sum of an array as
|
||||
* runningSum[i] = sum(nums[0]…nums[i]).
|
||||
*
|
||||
* Return the running sum of nums.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Example 1:
|
||||
*
|
||||
* Input: nums = [1,2,3,4]
|
||||
* Output: [1,3,6,10]
|
||||
* Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3,
|
||||
* 1+2+3+4].
|
||||
*
|
||||
* Example 2:
|
||||
*
|
||||
* Input: nums = [1,1,1,1,1]
|
||||
* Output: [1,2,3,4,5]
|
||||
* Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1,
|
||||
* 1+1+1+1, 1+1+1+1+1].
|
||||
*
|
||||
* Example 3:
|
||||
*
|
||||
* Input: nums = [3,1,2,10,1]
|
||||
* Output: [3,4,6,16,17]
|
||||
*
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* • 1 <= nums.length <= 1000
|
||||
*
|
||||
* • -10^6 <= nums[i] <= 10^6
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {number[]} nums
|
||||
* @return {number[]}
|
||||
*/
|
||||
var runningSum = function(nums) {
|
||||
let prefix = [nums[0]];
|
||||
for (let i = 1; i < nums.length; i++){
|
||||
prefix.push(prefix[i - 1] + nums[i]);
|
||||
}
|
||||
return prefix;
|
||||
};
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 1636. Sort Array by Increasing Frequency
|
||||
* Difficulty: Easy
|
||||
* https://leetcode.com/problems/sort-array-by-increasing-frequency/
|
||||
*
|
||||
* ──────────────────────────────────────────────────
|
||||
*
|
||||
* Given an array of integers nums, sort the array in increasing order
|
||||
* based on the frequency of the values. If multiple values have the same
|
||||
* frequency, sort them in decreasing order.
|
||||
*
|
||||
* Return the sorted array.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Example 1:
|
||||
*
|
||||
* Input: nums = [1,1,2,2,2,3]
|
||||
* Output: [3,1,1,2,2,2]
|
||||
* Explanation: '3' has a frequency of 1, '1' has a frequency of 2, and
|
||||
* '2' has a frequency of 3.
|
||||
*
|
||||
* Example 2:
|
||||
*
|
||||
* Input: nums = [2,3,1,3,2]
|
||||
* Output: [1,3,3,2,2]
|
||||
* Explanation: '2' and '3' both have a frequency of 2, so they are
|
||||
* sorted in decreasing order.
|
||||
*
|
||||
* Example 3:
|
||||
*
|
||||
* Input: nums = [-1,1,-6,4,5,-6,1,4,1]
|
||||
* Output: [5,-1,4,4,-6,-6,1,1,1]
|
||||
*
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* • 1 <= nums.length <= 100
|
||||
*
|
||||
* • -100 <= nums[i] <= 100
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {number[]} nums
|
||||
* @return {number[]}
|
||||
*/
|
||||
var frequencySort = function (nums) {
|
||||
const freq = {};
|
||||
for (let n of nums) freq[n] = (freq[n] || 0) + 1;
|
||||
return nums.sort((a, b) => freq[a] - freq[b] || b - a);
|
||||
};
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 217. Contains Duplicate
|
||||
* Difficulty: Easy
|
||||
* https://leetcode.com/problems/contains-duplicate/
|
||||
*
|
||||
* ──────────────────────────────────────────────────
|
||||
*
|
||||
* Given an integer array nums, return true if any value appears at
|
||||
* least twice in the array, and return false if every element is
|
||||
* distinct.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Example 1:
|
||||
*
|
||||
* Input: nums = [1,2,3,1]
|
||||
*
|
||||
* Output: true
|
||||
*
|
||||
* Explanation:
|
||||
*
|
||||
* The element 1 occurs at the indices 0 and 3.
|
||||
*
|
||||
* Example 2:
|
||||
*
|
||||
* Input: nums = [1,2,3,4]
|
||||
*
|
||||
* Output: false
|
||||
*
|
||||
* Explanation:
|
||||
*
|
||||
* All elements are distinct.
|
||||
*
|
||||
* Example 3:
|
||||
*
|
||||
* Input: nums = [1,1,1,3,3,4,3,2,4,2]
|
||||
*
|
||||
* Output: true
|
||||
*
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* • 1 <= nums.length <= 10^5
|
||||
*
|
||||
* • -10^9 <= nums[i] <= 10^9
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {number[]} nums
|
||||
* @return {boolean}
|
||||
*/
|
||||
var containsDuplicate = function(nums) {
|
||||
const freq = {};
|
||||
for (let n of nums) {
|
||||
freq[n] = (freq[n] || 0) + 1;
|
||||
if (freq[n] >= 2) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* 268. Missing Number
|
||||
* Difficulty: Easy
|
||||
* https://leetcode.com/problems/missing-number/
|
||||
*
|
||||
* ──────────────────────────────────────────────────
|
||||
*
|
||||
* Given an array nums containing n distinct numbers in the range [0,
|
||||
* n], return the only number in the range that is missing from the
|
||||
* array.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Example 1:
|
||||
*
|
||||
* Input: nums = [3,0,1]
|
||||
*
|
||||
* Output: 2
|
||||
*
|
||||
* Explanation:
|
||||
*
|
||||
* n = 3 since there are 3 numbers, so all numbers are in the range
|
||||
* [0,3]. 2 is the missing number in the range since it does not appear
|
||||
* in nums.
|
||||
*
|
||||
* Example 2:
|
||||
*
|
||||
* Input: nums = [0,1]
|
||||
*
|
||||
* Output: 2
|
||||
*
|
||||
* Explanation:
|
||||
*
|
||||
* n = 2 since there are 2 numbers, so all numbers are in the range
|
||||
* [0,2]. 2 is the missing number in the range since it does not appear
|
||||
* in nums.
|
||||
*
|
||||
* Example 3:
|
||||
*
|
||||
* Input: nums = [9,6,4,2,3,5,7,0,1]
|
||||
*
|
||||
* Output: 8
|
||||
*
|
||||
* Explanation:
|
||||
*
|
||||
* n = 9 since there are 9 numbers, so all numbers are in the range
|
||||
* [0,9]. 8 is the missing number in the range since it does not appear
|
||||
* in nums.
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* • n == nums.length
|
||||
*
|
||||
* • 1 <= n <= 10^4
|
||||
*
|
||||
* • 0 <= nums[i] <= n
|
||||
*
|
||||
* • All the numbers of nums are unique.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Follow up: Could you implement a solution using only O(1) extra space
|
||||
* complexity and O(n) runtime complexity?
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {number[]} nums
|
||||
* @return {number}
|
||||
*/
|
||||
var missingNumber = function(nums) {
|
||||
const numSet = new Set(nums);
|
||||
const expectedCount = nums.length + 1;
|
||||
|
||||
for (let i = 0; i < expectedCount; i++){
|
||||
if(!numSet.has(i)){
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* 303. Range Sum Query - Immutable
|
||||
* Difficulty: Easy
|
||||
* https://leetcode.com/problems/range-sum-query-immutable/
|
||||
*
|
||||
* ──────────────────────────────────────────────────
|
||||
*
|
||||
* Given an integer array nums, handle multiple queries of the following
|
||||
* type:
|
||||
*
|
||||
* • Calculate the sum of the elements of nums between indices left and
|
||||
* right inclusive where left <= right.
|
||||
*
|
||||
* Implement the NumArray class:
|
||||
*
|
||||
* • NumArray(int[] nums) Initializes the object with the integer array
|
||||
* nums.
|
||||
*
|
||||
* • int sumRange(int left, int right) Returns the sum of the elements
|
||||
* of nums between indices left and right inclusive (i.e. nums[left] +
|
||||
* nums[left + 1] + ... + nums[right]).
|
||||
*
|
||||
*
|
||||
*
|
||||
* Example 1:
|
||||
*
|
||||
* Input
|
||||
* ["NumArray", "sumRange", "sumRange", "sumRange"]
|
||||
* [[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]
|
||||
* Output
|
||||
* [null, 1, -1, -3]
|
||||
*
|
||||
* Explanation
|
||||
* NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]);
|
||||
* numArray.sumRange(0, 2); // return (-2) + 0 + 3 = 1
|
||||
* numArray.sumRange(2, 5); // return 3 + (-5) + 2 + (-1) = -1
|
||||
* numArray.sumRange(0, 5); // return (-2) + 0 + 3 + (-5) + 2 + (-1) = -3
|
||||
*
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* • 1 <= nums.length <= 10^4
|
||||
*
|
||||
* • -10^5 <= nums[i] <= 10^5
|
||||
*
|
||||
* • 0 <= left <= right < nums.length
|
||||
*
|
||||
* • At most 10^4 calls will be made to sumRange.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {number[]} nums
|
||||
*/
|
||||
class NumArray {
|
||||
constructor(nums) {
|
||||
this.prefix = [0];
|
||||
for (let n of nums){
|
||||
this.prefix.push(this.prefix[this.prefix.length - 1] + n);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param {number} left
|
||||
* @param {number} right
|
||||
* @return {number}
|
||||
*/
|
||||
sumRange(left, right) {
|
||||
return this.prefix[right + 1] - this.prefix[left];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Your NumArray object will be instantiated and called as such:
|
||||
* var obj = new NumArray(nums)
|
||||
* var param_1 = obj.sumRange(left,right)
|
||||
*/
|
||||
@@ -0,0 +1,58 @@
|
||||
"""
|
||||
35. Search Insert Position
|
||||
Difficulty: Easy
|
||||
https://leetcode.com/problems/search-insert-position/
|
||||
|
||||
──────────────────────────────────────────────────
|
||||
|
||||
Given a sorted array of distinct integers and a target value, return
|
||||
the index if the target is found. If not, return the index where it
|
||||
would be if it were inserted in order.
|
||||
|
||||
You must write an algorithm with O(log n) runtime complexity.
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
Input: nums = [1,3,5,6], target = 5
|
||||
Output: 2
|
||||
|
||||
Example 2:
|
||||
|
||||
Input: nums = [1,3,5,6], target = 2
|
||||
Output: 1
|
||||
|
||||
Example 3:
|
||||
|
||||
Input: nums = [1,3,5,6], target = 7
|
||||
Output: 4
|
||||
|
||||
|
||||
|
||||
Constraints:
|
||||
|
||||
• 1 <= nums.length <= 10^4
|
||||
|
||||
• -10^4 <= nums[i] <= 10^4
|
||||
|
||||
• nums contains distinct values sorted in ascending order.
|
||||
|
||||
• -10^4 <= target <= 10^4
|
||||
"""
|
||||
|
||||
|
||||
class Solution:
|
||||
def searchInsert(self, nums: List[int], target: int) -> int:
|
||||
l, r = 0, len(nums) - 1
|
||||
|
||||
while l <= r:
|
||||
m = (l + r) // 2
|
||||
if nums[m] == target:
|
||||
return m
|
||||
elif nums[m] < target:
|
||||
l = m + 1
|
||||
elif nums[m] > target:
|
||||
r = m - 1
|
||||
|
||||
return (l + r) // 2 + 1
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 704. Binary Search
|
||||
* Difficulty: Easy
|
||||
* https://leetcode.com/problems/binary-search/
|
||||
*
|
||||
* ──────────────────────────────────────────────────
|
||||
*
|
||||
* Given an array of integers nums which is sorted in ascending order,
|
||||
* and an integer target, write a function to search target in nums. If
|
||||
* target exists, then return its index. Otherwise, return -1.
|
||||
*
|
||||
* You must write an algorithm with O(log n) runtime complexity.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Example 1:
|
||||
*
|
||||
* Input: nums = [-1,0,3,5,9,12], target = 9
|
||||
* Output: 4
|
||||
* Explanation: 9 exists in nums and its index is 4
|
||||
*
|
||||
* Example 2:
|
||||
*
|
||||
* Input: nums = [-1,0,3,5,9,12], target = 2
|
||||
* Output: -1
|
||||
* Explanation: 2 does not exist in nums so return -1
|
||||
*
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* • 1 <= nums.length <= 10^4
|
||||
*
|
||||
* • -10^4 < nums[i], target < 10^4
|
||||
*
|
||||
* • All the integers in nums are unique.
|
||||
*
|
||||
* • nums is sorted in ascending order.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {number[]} nums
|
||||
* @param {number} target
|
||||
* @return {number}
|
||||
*/
|
||||
var search = function(nums, target) {
|
||||
let left = 0;
|
||||
let right = nums.length - 1;
|
||||
|
||||
while(left <= right) {
|
||||
const mid = left + Math.floor((right - left) / 2);
|
||||
if (nums[mid] === target) {
|
||||
return mid;
|
||||
} else if(nums[mid] < target){
|
||||
left = mid + 1;
|
||||
} else if (nums[mid] > target) {
|
||||
right = mid - 1;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 977. Squares of a Sorted Array
|
||||
* Difficulty: Easy
|
||||
* https://leetcode.com/problems/squares-of-a-sorted-array/
|
||||
*
|
||||
* ──────────────────────────────────────────────────
|
||||
*
|
||||
* Given an integer array nums sorted in non-decreasing order, return an
|
||||
* array of the squares of each number sorted in non-decreasing order.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Example 1:
|
||||
*
|
||||
* Input: nums = [-4,-1,0,3,10]
|
||||
* Output: [0,1,9,16,100]
|
||||
* Explanation: After squaring, the array becomes [16,1,0,9,100].
|
||||
* After sorting, it becomes [0,1,9,16,100].
|
||||
*
|
||||
* Example 2:
|
||||
*
|
||||
* Input: nums = [-7,-3,2,3,11]
|
||||
* Output: [4,9,9,49,121]
|
||||
*
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* • 1 <= nums.length <= 10^4
|
||||
*
|
||||
* • -10^4 <= nums[i] <= 10^4
|
||||
*
|
||||
* • nums is sorted in non-decreasing order.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Follow up: Squaring each element and sorting the new array is very
|
||||
* trivial, could you find an O(n) solution using a different approach?
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {number[]} nums
|
||||
* @return {number[]}
|
||||
*/
|
||||
var sortedSquares = function(nums) {
|
||||
let n = nums.length;
|
||||
let ans = new Array(nums.length);
|
||||
let left = 0, right = nums.length - 1;
|
||||
|
||||
for (let i = n -1; i >= 0; i--){
|
||||
let square;
|
||||
if(Math.abs(nums[left]) < Math.abs(nums[right])){
|
||||
square = nums[right];
|
||||
right--;
|
||||
}else{
|
||||
square = nums[left];
|
||||
left++;
|
||||
}
|
||||
ans[i] = square*square;
|
||||
}
|
||||
return ans;
|
||||
};
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* 1832. Check if the Sentence Is Pangram
|
||||
* Difficulty: Easy
|
||||
* https://leetcode.com/problems/check-if-the-sentence-is-pangram/
|
||||
*
|
||||
* ──────────────────────────────────────────────────
|
||||
*
|
||||
* A pangram is a sentence where every letter of the English alphabet
|
||||
* appears at least once.
|
||||
*
|
||||
* Given a string sentence containing only lowercase English letters,
|
||||
* return true if sentence is a pangram, or false otherwise.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Example 1:
|
||||
*
|
||||
* Input: sentence = "thequickbrownfoxjumpsoverthelazydog"
|
||||
* Output: true
|
||||
* Explanation: sentence contains at least one of every letter of the
|
||||
* English alphabet.
|
||||
*
|
||||
* Example 2:
|
||||
*
|
||||
* Input: sentence = "leetcode"
|
||||
* Output: false
|
||||
*
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* • 1 <= sentence.length <= 1000
|
||||
*
|
||||
* • sentence consists of lowercase English letters.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {string} sentence
|
||||
* @return {boolean}
|
||||
*/
|
||||
var checkIfPangram = function(sentence) {
|
||||
const freq = {};
|
||||
for (let c of sentence) freq[c] = (freq[c] || 0) + 1;
|
||||
|
||||
if (Object.keys(freq).length === 26){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 242. Valid Anagram
|
||||
* Difficulty: Easy
|
||||
* https://leetcode.com/problems/valid-anagram/
|
||||
*
|
||||
* ──────────────────────────────────────────────────
|
||||
*
|
||||
* Given two strings s and t, return true if t is an anagram of s, and
|
||||
* false otherwise.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Example 1:
|
||||
*
|
||||
* Input: s = "anagram", t = "nagaram"
|
||||
*
|
||||
* Output: true
|
||||
*
|
||||
* Example 2:
|
||||
*
|
||||
* Input: s = "rat", t = "car"
|
||||
*
|
||||
* Output: false
|
||||
*
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* • 1 <= s.length, t.length <= 5 * 10^4
|
||||
*
|
||||
* • s and t consist of lowercase English letters.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Follow up: What if the inputs contain Unicode characters? How would
|
||||
* you adapt your solution to such a case?
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {string} s
|
||||
* @param {string} t
|
||||
* @return {boolean}
|
||||
*/
|
||||
var isAnagram = function(s, t) {
|
||||
let freq = {};
|
||||
for (let c of s) freq[c] = (freq[c] || 0) + 1;
|
||||
|
||||
for (let c of t) {
|
||||
if(!freq[c] || freq[c] === 0){
|
||||
return false;
|
||||
}
|
||||
freq[c] = freq[c] - 1;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 387. First Unique Character in a String
|
||||
* Difficulty: Easy
|
||||
* https://leetcode.com/problems/first-unique-character-in-a-string/
|
||||
*
|
||||
* ──────────────────────────────────────────────────
|
||||
*
|
||||
* Given a string s, find the first non-repeating character in it and
|
||||
* return its index. If it does not exist, return -1.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Example 1:
|
||||
*
|
||||
* Input: s = "leetcode"
|
||||
*
|
||||
* Output: 0
|
||||
*
|
||||
* Explanation:
|
||||
*
|
||||
* The character 'l' at index 0 is the first character that does not
|
||||
* occur at any other index.
|
||||
*
|
||||
* Example 2:
|
||||
*
|
||||
* Input: s = "loveleetcode"
|
||||
*
|
||||
* Output: 2
|
||||
*
|
||||
* Example 3:
|
||||
*
|
||||
* Input: s = "aabb"
|
||||
*
|
||||
* Output: -1
|
||||
*
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* • 1 <= s.length <= 10^5
|
||||
*
|
||||
* • s consists of only lowercase English letters.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {string} s
|
||||
* @return {number}
|
||||
*/
|
||||
var firstUniqChar = function (s) {
|
||||
const freq = {};
|
||||
for (let c of s) freq[c] = (freq[c] || 0) + 1;
|
||||
|
||||
for (let i = 0; i < s.length; i++) {
|
||||
if (freq[s[i]] === 1) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
@@ -0,0 +1,76 @@
|
||||
"""
|
||||
20. Valid Parentheses
|
||||
Difficulty: Easy
|
||||
https://leetcode.com/problems/valid-parentheses/
|
||||
|
||||
──────────────────────────────────────────────────
|
||||
|
||||
Given a string s containing just the characters '(', ')', '{', '}',
|
||||
'[' and ']', determine if the input string is valid.
|
||||
|
||||
An input string is valid if:
|
||||
|
||||
• Open brackets must be closed by the same type of brackets.
|
||||
|
||||
• Open brackets must be closed in the correct order.
|
||||
|
||||
• Every close bracket has a corresponding open bracket of the same
|
||||
type.
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
Input: s = "()"
|
||||
|
||||
Output: true
|
||||
|
||||
Example 2:
|
||||
|
||||
Input: s = "()[]{}"
|
||||
|
||||
Output: true
|
||||
|
||||
Example 3:
|
||||
|
||||
Input: s = "(]"
|
||||
|
||||
Output: false
|
||||
|
||||
Example 4:
|
||||
|
||||
Input: s = "([])"
|
||||
|
||||
Output: true
|
||||
|
||||
Example 5:
|
||||
|
||||
Input: s = "([)]"
|
||||
|
||||
Output: false
|
||||
|
||||
|
||||
|
||||
Constraints:
|
||||
|
||||
• 1 <= s.length <= 10^4
|
||||
|
||||
• s consists of parentheses only '()[]{}'.
|
||||
"""
|
||||
|
||||
|
||||
class Solution:
|
||||
def isValid(self, s: str) -> bool:
|
||||
stack = []
|
||||
pairs = {")": "(", "]": "[", "}": "{"}
|
||||
|
||||
for c in s:
|
||||
if c in pairs:
|
||||
if stack and pairs[c] == stack[-1]:
|
||||
stack.pop()
|
||||
else:
|
||||
return False
|
||||
else:
|
||||
stack.append(c)
|
||||
|
||||
return True if not stack else False
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* 125. Valid Palindrome
|
||||
* Difficulty: Easy
|
||||
* https://leetcode.com/problems/valid-palindrome/
|
||||
*
|
||||
* ──────────────────────────────────────────────────
|
||||
*
|
||||
* A phrase is a palindrome if, after converting all uppercase letters
|
||||
* into lowercase letters and removing all non-alphanumeric characters,
|
||||
* it reads the same forward and backward. Alphanumeric characters
|
||||
* include letters and numbers.
|
||||
*
|
||||
* Given a string s, return true if it is a palindrome, or false
|
||||
* otherwise.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Example 1:
|
||||
*
|
||||
* Input: s = "A man, a plan, a canal: Panama"
|
||||
* Output: true
|
||||
* Explanation: "amanaplanacanalpanama" is a palindrome.
|
||||
*
|
||||
* Example 2:
|
||||
*
|
||||
* Input: s = "race a car"
|
||||
* Output: false
|
||||
* Explanation: "raceacar" is not a palindrome.
|
||||
*
|
||||
* Example 3:
|
||||
*
|
||||
* Input: s = " "
|
||||
* Output: true
|
||||
* Explanation: s is an empty string "" after removing non-alphanumeric
|
||||
* characters.
|
||||
* Since an empty string reads the same forward and backward, it is a
|
||||
* palindrome.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* • 1 <= s.length <= 2 * 10^5
|
||||
*
|
||||
* • s consists only of printable ASCII characters.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {string} s
|
||||
* @return {boolean}
|
||||
*/
|
||||
var isPalindrome = function(s) {
|
||||
let normal = s.replace(/[^a-zA-Z0-9]/g, "").toLowerCase()
|
||||
let i = 0, j = normal.length - 1;
|
||||
|
||||
while (i < j) {
|
||||
if(normal[i] !== normal[j]) {
|
||||
return false;
|
||||
}
|
||||
i++;
|
||||
j--;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 344. Reverse String
|
||||
* Difficulty: Easy
|
||||
* https://leetcode.com/problems/reverse-string/
|
||||
*
|
||||
* ──────────────────────────────────────────────────
|
||||
*
|
||||
* Write a function that reverses a string. The input string is given as
|
||||
* an array of characters s.
|
||||
*
|
||||
* You must do this by modifying the input array in-place with O(1)
|
||||
* extra memory.
|
||||
*
|
||||
*
|
||||
*
|
||||
* Example 1:
|
||||
*
|
||||
* Input: s = ["h","e","l","l","o"]
|
||||
* Output: ["o","l","l","e","h"]
|
||||
*
|
||||
* Example 2:
|
||||
*
|
||||
* Input: s = ["H","a","n","n","a","h"]
|
||||
* Output: ["h","a","n","n","a","H"]
|
||||
*
|
||||
*
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* • 1 <= s.length <= 10^5
|
||||
*
|
||||
* • s[i] is a printable ascii character.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {character[]} s
|
||||
* @return {void} Do not return anything, modify s in-place instead.
|
||||
*/
|
||||
var reverseString = function(s) {
|
||||
let i = 0;
|
||||
let j = s.length - 1;
|
||||
|
||||
while (i < j) {
|
||||
[s[i], s[j]] = [s[j], s[i]];
|
||||
j--;
|
||||
i++;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,57 @@
|
||||
"""
|
||||
392. Is Subsequence
|
||||
Difficulty: Easy
|
||||
https://leetcode.com/problems/is-subsequence/
|
||||
|
||||
──────────────────────────────────────────────────
|
||||
|
||||
Given two strings s and t, return true if s is a subsequence of t, or
|
||||
false otherwise.
|
||||
|
||||
A subsequence of a string is a new string that is formed from the
|
||||
original string by deleting some (can be none) of the characters
|
||||
without disturbing the relative positions of the remaining characters.
|
||||
(i.e., "ace" is a subsequence of "abcde" while "aec" is not).
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
Input: s = "abc", t = "ahbgdc"
|
||||
Output: true
|
||||
|
||||
Example 2:
|
||||
|
||||
Input: s = "axc", t = "ahbgdc"
|
||||
Output: false
|
||||
|
||||
|
||||
|
||||
Constraints:
|
||||
|
||||
• 0 <= s.length <= 100
|
||||
|
||||
• 0 <= t.length <= 10^4
|
||||
|
||||
• s and t consist only of lowercase English letters.
|
||||
|
||||
|
||||
|
||||
Follow up: Suppose there are lots of incoming s, say s1, s2, ..., sk
|
||||
where k >= 10^9, and you want to check one by one to see if t has its
|
||||
subsequence. In this scenario, how would you change your code?
|
||||
"""
|
||||
|
||||
|
||||
class Solution:
|
||||
def isSubsequence(self, s: str, t: str) -> bool:
|
||||
if len(s) > len(t):
|
||||
return False
|
||||
|
||||
i, j = 0, 0
|
||||
while i < len(s) and j < len(t):
|
||||
if s[i] == t[j]:
|
||||
i += 1
|
||||
j += 1
|
||||
|
||||
return i == len(s)
|
||||
Reference in New Issue
Block a user