temp
.
This way, we will make the greatest triplet that is smaller than equal to the target triplet, which will be the closest we can make to target.
func mergeTriplets(triplets [][]int, target []int) bool {
var temp [3]int
// Loop through all triplets
for _, i := range triplets {
// If the triplet is smaller than or equal to the target
if i[0] <= target[0] && i[1] <= target[1] && i[2] <= target[2] {
// If the triplet is smaller than or equal to the target, merge it into temp
temp = [3]int{max(i[0],temp[0]), max(i[1],temp[1]), max(i[2], temp[2])}
}
}
// If temp is equal to target, return true
return temp[0] == target[0] && temp[1] == target[1] && temp[2] == target[2]
}
// Helper function to get the max of two numbers
func max (i, j int) int {
if i > j {
return i
} else {
return j
}
}
Looking at this problem, we can recursively brute force all possible solutions by getting every single move we can make on the grid.
First Solution: Brute Force Solution TLE
func uniquePaths(m int, n int) int {
if m == 1 || n == 1{ return 1 }
return uniquePaths(m - 1, n) + uniquePaths(m, n - 1)
}
Explanation of First Solution
m == 1
or n == 1
m
making us move down and we subtract from n
to move right.Since the first solution, Time Limit Exceed, we can implement memoization to store results from a specific position in the matrix.
Second Solution: Memoized Solution
func uniquePaths(m int, n int) int {
return pathFinder(m,n, map[[2]int]int{})
}
func pathFinder(m int, n int, dp map[[2]int]int) int {
var pos = [2]int{ m, n }
if (m == 1 || n == 1 ) || dp[pos] > 0 {
return dp[pos] + 1
}
dp[pos] = pathFinder(m - 1, n, dp) + pathFinder(m, n - 1, dp) - 1
return dp[pos] + 1
}
Explanation of Second Solution
pos
is the key for the dp
map.(m == 1 || n == 1 ) || dp[pos] > 0
is for when either we have hit the base case of m
or n
equaling 1
or dp[pos] > 0
meaning that we have already stored the result of dp[pos]
.dp[pos] = pathFinder(m - 1, n, dp) + pathFinder(m, n - 1, dp) - 1
is when we add the recursive calls of pathFinder, one of the calls being when we subtract from m
essentially moving down, and one of the calls being when we subtract from n
making us move to the right]]>Upvote if This Solution helps
Intiution
func findFinalValue(nums []int, original int) int {
for i := 0 ; i < len(nums) ; i++{
if nums[i] == original {
original *= 2
i = -1
}
}
return original
}
How the Two Pointer Solution Works
In a nutshell, the two-pointer solution is just a sped-up version of the brute force solution. All we do is have a fast and slow pointer. We map the points we have visited with the slow pointer, and the fast pointer jumps ahead and checks if we have already seen that point.
Brute Force O(n) time
func detectCycle(head *ListNode) *ListNode {
m := make(map[*ListNode]bool)
curr := head
for curr != nil{
if m[curr] == true{
return curr
}
m[curr] = true
curr = curr.Next
}
return nil
}
Two Pointer Solution O(n/2) time
func detectCycle(head *ListNode) *ListNode {
slow,fast := head,head
visited := map[*ListNode]bool{}
for fast != nil{
visited[slow] = true
slow = slow.Next
fast = fast.Next
if visited[fast] {
return fast
}
if fast != nil{
fast = fast.Next
}
if visited[fast] {
return fast
}
}
return nil
}
https://leetcode.com/neilnaveen/
In this problem, we focus on that teemo’s attack duration is reset when teemo attacks again.
The code finds if teemo’s attacks overlap with the next attack. If it does, add the duration that the attack lasts before it is reset - 1
. If not, add the duration to the result.
func findPoisonedDuration(timeSeries []int, duration int) int {
res := 0
for i := 0 ; i < len(timeSeries)-1 ; i++{
if timeSeries[i] + duration-1 >= timeSeries[i+1]{
res += timeSeries[i+1] - timeSeries[i]
}else{
res += duration
}
}
res += duration
return res
}
The idea of the v2 solution is to find the smallest number in the step-by-step sums if your start num is 1
. In the end, we add the smallest number we can to make all the steps positive
v2 solution O(n) time and O(1) space
func minStartValue(nums []int) int {
sum := 1
min := 100000000
for _,j := range nums{
sum += j
if sum <= min{
min = sum
}
}
if min > 0 {
return 1
}
return (min*-1)+2
}
v1 solution O(m*n) time and O(1) space
This is a brute force solution where we test from 0
to a 10000000000
for the start num in m
time and see if the step by step addition is greater than 0
func minStartValue(nums []int) int {
sum := 0
for i := 1 ; i < 10000000000 ; i++{
sum = i
flag := true
for _,j := range nums{
sum += j
if sum <= 0{
flag = false
break
}
}
if flag{
return i
}
}
return 0
}
This problem is about taking a number, let’s say9669
, and making it the largest number it could be by flipping one digit from a 6
to a 9
.
The solution is made up of five parts.
We split the number into its digits.
We reverse the array of digits because it reverses the order when you split a number as we did.
Then we find the first 6
and change it to a 9
.
Just before adding all the numbers together in a sum, we had to figure out how much should the digits be multiplied by to get into their correct form and not just the digits added up
Last of all, in code, the digits are summed up
In the first part, we add the last digit to the array and dividing the number by ten to take the last digit off. Now we have an array of the digits of the number reversed
for num > 0{
arr = append(arr,num % 10)
num/=10
}
This is the part for reversing the array. We have pointers on either side of the array. We then flip the digit on the left pointer digit with the right pointer digit and vice versa.
l , r := 0 , len(arr)-1
for l < r{
arr[l] , arr[r] = arr[r] , arr[l]
l++
r--
}
This part finds the first digit that is a 6
and changes it to a 9
by finding the first occurrence of a 6
, changing it to a 9, and breaking out of the loop, so it doesn’t change more than one digit
for i2,i := range arr{
if i == 6{
arr[i2] = 9
break
}
}
This part gets the number that we have to multiply to the last digit to get its place value. It does this by multiplying one by 10 for how long the number is -1.
nummultiplyer := 1
for i := 0 ; i < len(arr)-1 ; i++{
nummultiplyer *= 10
}
This is the last part where we add all the digits to a sum. If we add the digits all by itself, the sum will not be like the given number with one digit flipped. We need to add the numbers times the nummultiplyer
so it is at the correct place value and then divide the nummultiplyer
by ten to the correct place value for the next number.
for _,i := range arr{
sum += i*nummultiplyer
nummultiplyer/=10
}
return sum
End Code
func maximum69Number(num int) int {
sum := 0
arr := []int{}
// dividing up the number into a list of its digits
for num > 0{
arr = append(arr,num%10)
num/=10
}
// reversing the list of digits , becasue when we made a list of all of the digits it was reversed
l , r := 0 , len(arr)-1
for l < r{
arr[l] , arr[r] = arr[r] , arr[l]
l++
r--
}
// this is for finding the first digits that is a 6 and changing it to a nine
for i2,i := range arr{
if i == 6{
arr[i2] = 9
break
}
}
// this is for finding how many zeros should the first number be multiplyed by
nummultiplyer := 1
for i := 0 ; i < len(arr)-1 ; i++{
nummultiplyer *= 10
}
//and this is for remaking the list back into a array
for _,i := range arr{
sum += i*nummultiplyer
nummultiplyer/=10
}
return sum
}
func mergeAlternately(word1 string, word2 string) string {
mergestringres := ""
for i, i2 := range word1 {
mergestringres += string(i2)
if len(word2) > i {
mergestringres += string(word2[i])
}
}
if len(word1) < len(word2) {
mergestringres += word2[len(word1):]
}
return mergestringres
}
]]>