Leetcode - The K Weakest Rows in a Matrix

https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/

Given a m * n matrix mat of ones (representing soldiers) and zeros (representing civilians), return the indexes of the k weakest rows in the matrix ordered from the weakest to the strongest.

A row i is weaker than row j, if the number of soldiers in row i is less than the number of soldiers in row j, or they have the same number of soldiers but i is less than j. Soldiers are always stand in the frontier of a row, that is, always ones may appear first and then zeros.

Example 1:

Input: mat = 
[[1,1,0,0,0],
 [1,1,1,1,0],
 [1,0,0,0,0],
 [1,1,0,0,0],
 [1,1,1,1,1]], 
k = 3
Output: [2,0,3]
Explanation: 
The number of soldiers for each row is: 
row 0 -> 2 
row 1 -> 4 
row 2 -> 1 
row 3 -> 2 
row 4 -> 5 
Rows ordered from the weakest to the strongest are [2,0,3,1,4]

Leetcode - find-n-unique-integers-sum-up-to-zero

https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/

Given an integer n, return any array containing n unique integers such that they add up to 0.

Example 1:

Input: n = 5
Output: [-7,-1,1,3,4]
Explanation: These arrays also are accepted [-5,-1,1,2,3] , [-3,-1,2,-2,4].

Leetcode - find-pivot-index

https://leetcode.com/problems/find-pivot-index

Given an array of integers nums, write a method that returns the "pivot" index of this array.

We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.

If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.

Example 1:

Input: 
nums = [1, 7, 3, 6, 5, 6]
Output: 3
Explanation: 
The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.
Also, 3 is the first index where this occurs.

Leetcode - subarray-sum-equals-k

https://leetcode.com/problems/subarray-sum-equals-k/

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.

Example 1:

Input:nums = [1,1,1], k = 2
Output: 2

Note:

  1. The length of the array is in range [1, 20,000].
  2. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].

Leetcode - 3sum-closest

Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

Example:

Given array nums = [-1, 2, 1, -4], and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

Array- CountTripletsSmallerThanSum

统计数组中总和小于sum的三元组的数量

给定一个没有重复元素的数组,和一个sum值。统计数组中总和小于sum的三元组的数量。

例如对于数组nums[] = {-2, 0, 1, 3},sum = 2,共有2个三元组的总和小于sum:(-2, 0, 1)和(-2, 0, 3)

Leetcode - deepest-leaves-sum

https://leetcode.com/problems/deepest-leaves-sum

Given a binary tree, return the sum of values of its deepest leaves.

Array - Array Rotation

问题:将一个长度为n的数组中的元素右移k个位置。例如当n=5, k=3时:[1, 2, 3, 4, 5] => [3, 4, 5, 1, 2]。

要求O(1)的空间复杂度。

 

Array - AlternateElementsFromSortedArrays

从两个有序数组中轮流取元素,计算所有可能的有序数组

给定两个有序数组A和B,轮流从A和B中取元素,最后生成一个新的有序数组。数组的最后一个元素应当是B中的元素。计算所有这样的数组。

func alternateElement(a []int, b []int, i, j int, flag bool, arr *[]int, result *[]int)

i表示从i到len(A)-1的范围可以取,j表示从j到len(B)-1范围可以取,flag true表示从A取,flag false 表示从B取,result则存储所有结果的集合。