/* 625. Minimum Factorization My SubmissionsBack To Contest User Accepted: 541 User Tried: 732 Total Accepted: 551 Total Submissions: 2508 Difficulty: Medium Given a positive integer a, find the smallest positive integer b whose multiplication of each digit equals to a. If there is no answer or the answer is not fit in 32-bit signed integer, then return 0. Example 1 Input: 48 Output: 68 Example 2 Input: 15 Output: 35 1. prime factor 2*2*2*3*7 = 3*7*8 2. enumarate from 9 to 2 */ func smallestFactorization(a int) int { if a < 10 { return a } b := 0 k := 1 for i:=9 ; i>1; i-- { for { if a%i !=0 { break } a = a/i b += i*k k*=10 if b > math.MaxInt32 { return 0 } } } if a != 1 { return 0 } return b }
/* 3. Longest Substring Without Repeating Characters DescriptionHintsSubmissionsSolutions Total Accepted: 293905 Total Submissions: 1217456 Difficulty: Medium Contributor: LeetCode Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1. Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring. Subscribe to see which companies asked this question. Sliding Window */ func lengthOfLongestSubstring(s string) int { /* "abba" "abcabcbb" "dvdf" "aaaaaa" */ vmap := make(map[uint8]int) max := 0 validBeg := 0 for idx, _ := range s { if dupIdx, ok := vmap[s[idx]]; true == ok { if dupIdx+1 > validBeg { validBeg = dupIdx+1 } } vmap[s[idx]] = idx if idx - validBeg + 1 > max { max = idx - validBeg + 1 } } return max }
/* 518. Coin Change 2 DescriptionHintsSubmissionsSolutions Total Accepted: 4181 Total Submissions: 13730 Difficulty: Medium Contributors: vchernoy You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin. Note: You can assume that 0 <= amount <= 5000 1 <= coin <= 5000 the number of coins is less than 500 the answer is guaranteed to fit into signed 32-bit integer Example 1: Input: amount = 5, coins = [1, 2, 5] Output: 4 Explanation: there are four ways to make up the amount: 5=5 5=2+2+1 5=2+1+1+1 5=1+1+1+1+1 Example 2: Input: amount = 3, coins = [2] Output: 0 Explanation: the amount of 3 cannot be made up just with coins of 2. Example 3: Input: amount = 10, coins = [10] Output: 1 package DP Your input 5 [1,2,5] 5 [5,2,1] Your stdout 1: [1 1 1 1 1 1] 2: [1 1 2 2 3 3] 5: [1 1 2 2 3 4] 5: [1 0 0 0 0 1] 2: [1 0 1 0 1 1] 1: [1 1 2 2 3 4] */ func change(amount int, coins []int) int { dp := make([]int, amount+1) dp[0] = 1 for _, coin := range coins { for i := 0 ; i <= amount - coin; i++ { dp[i+coin] = dp[i]+dp[i+coin] } fmt.Printf("%v: %v\n", coin, dp) } return dp[amount] }