Skip to content

Commit fcc775a

Browse files
committed
add some questions
1 parent f5f5684 commit fcc775a

8 files changed

Lines changed: 240 additions & 0 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package backtracking
2+
3+
import "sort"
4+
5+
/*
6+
A happy string is a string that:
7+
8+
consists only of letters of the set ['a', 'b', 'c'].
9+
s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed).
10+
For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings.
11+
12+
Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order.
13+
14+
Return the kth string of this list or return an empty string if there are less than k happy strings of length n.
15+
16+
17+
18+
Example 1:
19+
20+
Input: n = 1, k = 3
21+
Output: "c"
22+
Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c".
23+
*/
24+
25+
func getHappyString(n int, k int) string {
26+
happyStrings := getHappyStringUtil(0, n, "", []string{})
27+
if len(happyStrings) < k {
28+
return ""
29+
}
30+
sort.Strings(happyStrings)
31+
return happyStrings[k-1]
32+
}
33+
34+
func getHappyStringUtil(start, n int, result string, combinations []string) []string {
35+
if len(result) == n {
36+
combinations = append(combinations, result)
37+
return combinations
38+
}
39+
40+
for _, ch := range []string{"a", "b", "c"} {
41+
if start > 0 && string(result[start-1]) == ch {
42+
continue
43+
}
44+
combinations = getHappyStringUtil(start+1, n, result+ch, combinations)
45+
}
46+
47+
return combinations
48+
}
49+
50+
func getHappyString1(n int, k int) string {
51+
count := 0
52+
result := ""
53+
generateHappyString(0, n, "", k, &count, &result)
54+
return result
55+
}
56+
57+
func generateHappyString(start, n int, current string, k int, count *int, result *string) {
58+
if len(current) == n {
59+
*count++
60+
if *count == k {
61+
*result = current
62+
}
63+
return
64+
}
65+
66+
for _, ch := range []byte{'a', 'b', 'c'} {
67+
if start > 0 && current[start-1] == ch {
68+
continue
69+
}
70+
generateHappyString(start+1, n, current+string(ch), k, count, result)
71+
if *result != "" { // Stop early when k-th happy string is found
72+
return
73+
}
74+
}
75+
}

array/recursion/print_all_subsets.go renamed to array/backtracking/recursion/print_all_subsets.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@ func subsets(nums []int) [][]int {
2525
return res
2626
}
2727

28+
// time to generate all subsets would be O(2^n) but copy operation will take O(n) time so total time complexity would be O(n*2^n)
29+
func generateAllSubsets(start int, nums, subset []int, subsets [][]int) [][]int {
30+
if start == len(nums) {
31+
tmpArr := make([]int, len(subset))
32+
copy(tmpArr, subset)
33+
subsets = append(subsets, tmpArr)
34+
return subsets
35+
}
36+
37+
// include the current element
38+
subset = append(subset, nums[start])
39+
subsets = generateAllSubsets(start+1, nums, subset, subsets)
40+
41+
// exclude the current element
42+
subset = subset[:len(subset)-1]
43+
subsets = generateAllSubsets(start+1, nums, subset, subsets)
44+
45+
return subsets
46+
}
47+
2848
/*
2949
[]
3050
/ \
File renamed without changes.
File renamed without changes.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package backtracking
2+
3+
import (
4+
"fmt"
5+
"math"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
/*
11+
Given a pattern containing I for increasing and D for decreasing, you need to find the smallest number following that pattern.
12+
You are given a 0-indexed string pattern of length n consisting of the characters 'I' meaning increasing and 'D' meaning decreasing.
13+
14+
A 0-indexed string num of length n + 1 is created using the following conditions:
15+
16+
num consists of the digits '1' to '9', where each digit is used at most once.
17+
If pattern[i] == 'I', then num[i] < num[i + 1].
18+
If pattern[i] == 'D', then num[i] > num[i + 1].
19+
Return the lexicographically smallest possible string num that meets the conditions.
20+
21+
Example 1:
22+
23+
Input: pattern = "IIIDIDDD"
24+
Output: "123549876"
25+
Explanation:
26+
At indices 0, 1, 2, and 4 we must have that num[i] < num[i+1].
27+
At indices 3, 5, 6, and 7 we must have that num[i] > num[i+1].
28+
Some possible values of num are "245639871", "135749862", and "123849765".
29+
It can be proven that "123549876" is the smallest possible num that meets the conditions.
30+
Note that "123414321" is not possible because the digit '1' is used more than once.
31+
*/
32+
func smallestNumber(pattern string) string {
33+
comb := smallestNumberUtil(0, pattern, "", math.MaxInt)
34+
return fmt.Sprintf("%d", comb)
35+
}
36+
37+
func smallestNumberUtil(start int, pattern, result string, minNum int) int {
38+
fmt.Println(start, result, pattern, len(pattern), minNum)
39+
40+
if start == len(pattern)+1 {
41+
num, _ := strconv.Atoi(result) // Convert string to int
42+
minNum = min(minNum, num)
43+
return minNum
44+
}
45+
46+
for i := 1; i <= 9; i++ {
47+
if strings.Contains(result, fmt.Sprintf("%d", i)) {
48+
continue
49+
}
50+
if start > 0 && pattern[start-1] == 'I' && int(result[start-1]-'0') > i {
51+
continue
52+
}
53+
if start > 0 && pattern[start-1] == 'D' && int(result[start-1]-'0') < i {
54+
continue
55+
}
56+
minNum = smallestNumberUtil(start+1, pattern, result+fmt.Sprintf("%d", i), minNum)
57+
}
58+
59+
return minNum
60+
}

array/kadane_algorithm.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,53 @@ func maxSubArrayIndices(nums []int) (maxLeft int, maxRight int) {
6262

6363
return maxLeft, maxRight
6464
}
65+
66+
// maximum-absolute-sum-of-any-subarray Ex: nums = [2,-5,1,-4,3,-2] -> 8
67+
// time complexity: O(2n), space complexity: O(1)
68+
func maxAbsoluteSum1(nums []int) int {
69+
maxSum, minSum, currSum := 0, 0, 0
70+
71+
// maximum positive sum
72+
for _, num := range nums {
73+
currSum += num
74+
maxSum = max(maxSum, currSum)
75+
if currSum < 0 {
76+
currSum = 0
77+
}
78+
}
79+
80+
// maximum negative sum
81+
currSum = 0
82+
for _, num := range nums {
83+
currSum += num
84+
minSum = min(minSum, currSum)
85+
if currSum > 0 {
86+
currSum = 0
87+
}
88+
}
89+
90+
return max(maxSum, -minSum)
91+
}
92+
93+
// Time complexity: O(n), Space complexity: O(1)
94+
func maxAbsoluteSum2(nums []int) int {
95+
maxSum, minSum := 0, 0
96+
currMax, currMin := 0, 0
97+
98+
for _, num := range nums {
99+
currMax += num
100+
currMin += num
101+
102+
maxSum = max(maxSum, currMax)
103+
minSum = min(minSum, currMin)
104+
105+
if currMax < 0 {
106+
currMax = 0
107+
}
108+
if currMin > 0 {
109+
currMin = 0
110+
}
111+
}
112+
113+
return max(maxSum, -minSum)
114+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package searching
2+
3+
import "math"
4+
5+
func findMin(nums []int) int {
6+
n := len(nums)
7+
if n == 0 {
8+
return -1 // Handle empty array case
9+
}
10+
11+
low, high := 0, n-1
12+
minNum := math.MaxInt
13+
14+
for low <= high {
15+
mid := (low + high) / 2
16+
17+
// Always update the minimum value found
18+
minNum = min(minNum, nums[mid])
19+
20+
// If the left half is sorted
21+
if nums[low] <= nums[mid] {
22+
// The minimum could be nums[low], so update minNum
23+
minNum = min(minNum, nums[low])
24+
// Discard the left half
25+
low = mid + 1
26+
} else { // Otherwise, right half is sorted
27+
// The minimum could be nums[mid + 1], so update minNum
28+
minNum = min(minNum, nums[mid+1])
29+
// Discard the right half
30+
high = mid - 1
31+
}
32+
}
33+
34+
return minNum
35+
}

0 commit comments

Comments
 (0)