Posts

Showing posts with the label 非最优

669. Coin Change

Code ( Language :C++) ( Judger :cloudjudge-cluster-4) Edit class Solution { public : /** * @param coins: a list of integer * @param amount: a total amount of money amount * @return: the fewest number of coins that you need to make up */ int coinChange ( vector < int > &coins, int amount) { // write your code here const int n = coins.size(); if (amount == 0 ){ return 0 ; } if (n <= 0 ){ return -1 ; } vector < vector < int >> dp(amount + 1 , vector < int >(n + 1 , amount + 1 )); dp[ 0 ][ 0 ] = 0 ; for ( int i = 1 ; i <= amount; i++){ for ( int j = 1 ; j <= n; j++){ dp[ 0 ][j] = 0 ; if (coins[j - 1 ] <= i){ dp[i][j] = min(dp[i - coins[j - 1 ]][j] + 1 , dp[i][j - 1 ]); } else { ...

704. Bulb Switcher II

lass Solution { public : /** * @param n: number of lights * @param m: number of operations * @return: the number of status */ int flipLights ( int n, int m) { // write your code here unordered_set < string > set ; if (n == 0 || m == 0 ){ return 1 ; } string s = "" ; for ( int i = 0 ; i < n; i++){ s += "1" ; } set .insert(s); for ( int i = 0 ; i < m; i++){ unordered_set < string > tmp; for ( auto str : set ){ string s1 = str, s2 = str, s3 = str, s4 = str; for ( int j = 0 ; j < n; j++){ s1[j] = s1[j] == '0' ? '1' : '0' ; if ( 2 * j < n){ s2[ 2 * j] = s2[ 2 * j] == '0' ? '1' : '0' ; } ...

623. K Edit Distance

Description 中文 English A game on an undirected graph is played by two players, Mouse and Cat, who alternate turns. The graph is given as follows: graph[a] is a list of all nodes b such that ab is an edge of the graph. Mouse starts at node 1 and goes first, Cat starts at node 2 and goes second, and there is a Hole at node 0. During each player's turn, they must travel along one edge of the graph that meets where they are. For example, if the Mouse is at node 1, it must travel to any node in graph[1]. Additionally, it is not allowed for the Cat to travel to the Hole (node 0.) Then, the game can end in 3 ways: If ever the Cat occupies the same node as the Mouse, the Cat wins. If ever the Mouse reaches the Hole, the Mouse wins. If ever a position is repeated (ie. the players are in the same position as a previous turn, and it is the same player's turn to move), the game is a draw. Given a graph, and assuming both players play optimally, return 1 if the...

1753. Doing Homework

ode ( Language :C++) Edit class Solution { public : /** * @param cost: the cost * @param val: the val * @return: the all cost */ long long doingHomework ( vector < int > &cost, vector < int > &val) { // Write your code here. long long res = 0 ; const int size = cost.size(); if (size == 0 ){ return res; } vector < int > prefixSum(size, 0 ); prefixSum[ 0 ] = cost[ 0 ]; for ( int i = 1 ; i < size; i++){ prefixSum[i] = prefixSum[i - 1 ] + cost[i]; } for ( int i = 0 ; i < val.size(); i++){ int j; for (j = 0 ; j < size; j++){ if (val[i] < prefixSum[j]){ res += (j == 0 ? 0 : prefixSum[j - 1 ]); break ; } } if (j == size){ res += prefixSum[size - ...

1512. Minimum Cost to Hire K Workers

There are  N  workers. The  i -th worker has a  quality[i]  and a minimum wage expectation  wage[i] . Now we want to hire exactly K workers to form a paid group. When hiring a group of K workers, we must pay them according to the following rules: Every worker in the paid group should be paid in the ratio of their quality compared to other workers in the paid group. Every worker in the paid group must be paid at least their minimum wage expectation. Return the least amount of money needed to form a paid group satisfying the above conditions. 1. 1 <= K <= N <= 10000 , where  N = quality.length = wage.length 2. 1 <= quality[i] <= 10000 3. 1 <= wage[i] <= 10000 4.Answers within  10^-5  of the correct answer will be considered correct. Have you met this question in a real interview?    Yes Problem Correction Example Example 1: Input: quality = [10,20,5], wage = [70,50,30],...

621. Maximum Subarray V

Code ( Language :C++) Edit class Solution { public : /** * @param nums: an array of integers * @param k1: An integer * @param k2: An integer * @return: the largest sum */ int maxSubarray5 ( vector < int > &nums, int k1, int k2) { // write your code here const int size = nums.size(); if (size < k1){ return 0 ; } // k1å’ŒK2 vector < int > prefixSum(size + 1 , 0 ); for ( int i = 0 ; i < size; i++){ prefixSum[i + 1 ] = prefixSum[i] + nums[i]; } int maxV = INT_MIN; for ( int i = 0 ; i <= size + 1 - k1; i++){ for ( int j = k1; j <= k2; j++){ if (i + j <= size){ maxV = max(maxV, prefixSum[i + j] - prefixSum[i]); } } } return maxV; } };

362. Sliding Window Maximum

Code ( Language :C++) Edit class Solution { public : /** * @param nums: A list of integers. * @param k: An integer * @return: The maximum number inside the window at each moving. */ vector < int > maxSlidingWindow( vector < int > &nums, int k) { // write your code here vector < int > res; const int size = nums.size(); if (size < k || k == 0 || size == 0 ){ return res; } for ( int i = 0 ; i <= size - k; i++){ int maxV = INT_MIN; for ( int j = 0 ; j < k; j++){ maxV = max(maxV, nums[i + j]); } res.push_back(maxV); } return res; } };

722. Maximum Subarray VI

Code ( Language :C++) Edit class Solution { public : /** * @param nums: the array * @return: the max xor sum of the subarray in a given array */ int maxXorSubarray ( vector < int > &nums) { // write code here const int size = nums.size(); if (size == 0 ){ return 0 ; } vector < int > prefix(size + 1 , 0 ); for ( int i = 0 ; i < size; i++){ prefix[i + 1 ] = prefix[i] ^ nums[i]; } int maxV = INT_MIN; for ( int i = 0 ; i <= size; i++){ for ( int j = i + 1 ; j <= size; j++){ maxV = max(maxV, prefix[i] ^ prefix[j]); } } return maxV; } };